DEV Community

Peter + AI
Peter + AI

Posted on

Understanding Uniface componentToStruct: Converting Component Data to Structures πŸš€

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}
Enter fullscreen mode Exit fullscreen mode

Example:

componentToStruct /mod /reconnecttags /firetriggers vStruct, EMPLOYEE.ORG
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

[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 πŸ’‘

  1. 🎯 Use /one for specific data: When working with individual records, use the /one qualifier to improve performance
  2. πŸ“Š Monitor return values: Always check $status for error handling (0 = success, <0 = error)
  3. πŸ”— Leverage reconnect tags: For data synchronization scenarios, use /reconnecttags to maintain data integrity
  4. ⚑ 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)