|
| 1 | +# Data Converter Sample |
| 2 | + |
| 3 | +This sample workflow demonstrates how to use custom data converters in Cadence workflows. The data converter is responsible for serializing and deserializing workflow inputs, outputs, and activity parameters. |
| 4 | + |
| 5 | +## Sample Description |
| 6 | + |
| 7 | +The sample implements a custom JSON data converter that: |
| 8 | +- Serializes workflow inputs and activity parameters to JSON format |
| 9 | +- Deserializes workflow outputs and activity results from JSON format |
| 10 | +- Provides better control over data serialization compared to the default data converter |
| 11 | +- Can be extended to support custom serialization formats (e.g., Protocol Buffers, MessagePack) |
| 12 | + |
| 13 | +The workflow takes a `MyPayload` struct as input, processes it through an activity, and returns the modified payload. |
| 14 | + |
| 15 | +## Key Components |
| 16 | + |
| 17 | +- **Custom Data Converter**: `jsonDataConverter` implements the `encoded.DataConverter` interface |
| 18 | +- **Workflow**: `dataConverterWorkflow` demonstrates using custom data types with the converter |
| 19 | +- **Activity**: `dataConverterActivity` processes the input and returns modified data |
| 20 | +- **Test**: Includes unit tests to verify the data converter functionality |
| 21 | + |
| 22 | +## Steps to Run Sample |
| 23 | + |
| 24 | +1. You need a cadence service running. See details in cmd/samples/README.md |
| 25 | + |
| 26 | +2. Run the following command to start the worker: |
| 27 | + ``` |
| 28 | + ./bin/dataconverter -m worker |
| 29 | + ``` |
| 30 | + |
| 31 | +3. Run the following command to execute the workflow: |
| 32 | + ``` |
| 33 | + ./bin/dataconverter -m trigger |
| 34 | + ``` |
| 35 | + |
| 36 | +You should see logs showing the workflow input being processed through the activity and the final result being returned. |
| 37 | + |
| 38 | +## Customization |
| 39 | + |
| 40 | +To use a different serialization format, you can implement your own data converter by: |
| 41 | +1. Creating a struct that implements the `encoded.DataConverter` interface |
| 42 | +2. Implementing the `ToData` method for serialization |
| 43 | +3. Implementing the `FromData` method for deserialization |
| 44 | +4. Registering the converter in the worker options |
| 45 | + |
| 46 | +This pattern is useful when you need to: |
| 47 | +- Use specific serialization formats for performance or compatibility |
| 48 | +- Add encryption/decryption to workflow data |
| 49 | +- Implement custom compression for large payloads |
| 50 | +- Support legacy data formats |
0 commit comments