What is componentToStruct? π€
The componentToStruct
statement in Uniface is a powerful tool that converts component data into a structured format (Struct). Think of it as a bridge between your component's data and a hierarchical data representation that can be easily manipulated, serialized, or passed between different parts of your application.
Syntax & Basic Usage π
componentToStruct {/mod} {/one} {/reconnecttags} {/firetriggers} StructTarget {, EntityName}
Example:
componentToStruct /mod /reconnecttags /firetriggers vStruct, EMPLOYEE.ORG
Key Qualifiers Explained π§
/mod - Modified Data Only
π Includes only modified occurrences and their ancestors, providing context for changed data while reducing overhead.
/one - Single Occurrence
π― Focuses on the current occurrence of the named entity. Perfect when you need to work with specific data records.
/reconnecttags - Metadata for Reconnection
π Adds special tags for reconnect processing and includes deleted occurrences. Essential for data synchronization scenarios.
/firetriggers - Event Processing
β‘ Fires preSerialize and postSerialize triggers, allowing custom processing during conversion.
Real-World Example: Order Management System π
Let's look at a practical example using an order management component:
operation exec
; component variable $vStruct$ is struct
retrieve
componentToStruct/one $vStruct$, "ORDER.SALES"
OUTPUT = $vStruct$->$dbgstring
edit
end; exec
This code converts the current ORDER.SALES occurrence to a Struct, including all related ORDERLINE data.
[image:1]
Understanding the Generated Structure ποΈ
The componentToStruct creates a hierarchical structure with specific annotations:
- π’ Component β Named Struct with component name
- π Entity β Named Struct with fully qualified entity name
- π Occurrence β Named Struct called "OCC"
- π€ Field β Named Struct with field name
Sample Output Structure π
[ORDER.SALES]
[$tags]
[u_type] = "entity"
[OCC]
[$tags]
[u_type] = "occurrence"
[ORDER_ID] = "23"
[$tags]
[u_type] = "field"
[ORDERLINE.SALES]
[$tags]
[u_type] = "entity"
[OCC]
[$tags]
[u_type] = "occurrence"
[ITEM_NAME] = "tulips"
[$tags]
[u_type] = "field"
[UNIT_PRICE] = "2.22"
[$tags]
[u_type] = "field"
[QUANTITY] = "7"
[$tags]
[u_type] = "field"
[image:2]
Advanced Features π―
Metadata Tags for Reconnection
When using /reconnecttags
, additional metadata is included:
- u_id: Uniface-generated occurrence identifier π
- u_crc: CRC checksum for data integrity β
- u_status: Modification status (est/mod/new/del) π
Trigger Integration
With /firetriggers
, you can hook into the conversion process:
- preSerialize: Execute logic before struct generation π
- postSerialize: Execute logic after struct generation β¨
Best Practices & Tips π‘
- π― Use /one for specific data: When working with individual records, use the /one qualifier to improve performance
- π Monitor return values: Always check $status for error handling (0 = success, <0 = error)
- π Leverage reconnect tags: For data synchronization scenarios, use /reconnecttags to maintain data integrity
- β‘ Optimize with /mod: In large datasets, use /mod to process only changed data
Common Use Cases π
- Data Export/Import: Converting component data for external systems π€
- API Integration: Preparing data for REST services π
- Data Caching: Creating snapshots of component state πΎ
- Debugging: Inspecting component data structure π
Conclusion π
The componentToStruct
function is an essential tool in the Uniface developer's toolkit. It provides a flexible and powerful way to convert component data into structured formats, enabling seamless data manipulation and integration scenarios. Whether you're building APIs, implementing data synchronization, or simply need to inspect your component's data structure, componentToStruct has you covered!
Happy coding! π
This article is based on the official Uniface 10.4 documentation and was created with AI assistance to help fellow developers understand this powerful feature.
Top comments (0)