summaryrefslogtreecommitdiff
path: root/plugins/temporal/protocol/converter.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-01-26 11:52:03 +0300
committerGitHub <[email protected]>2021-01-26 11:52:03 +0300
commite2266b80db47444ba5858c736833a8a81b1361ad (patch)
tree37e06810352752f88032f7d0eadb554fa18b98da /plugins/temporal/protocol/converter.go
parentfae4711e3548bfd2e34f13aabfaab6a5b4e317c6 (diff)
parenta392d962508e1bc9e497c8c4ef021425bc2c67c2 (diff)
Merge pull request #502 from spiral/plugin/temporalv2.0.0-beta12
plugin(temporal): Add temporal plugins set to the RR2
Diffstat (limited to 'plugins/temporal/protocol/converter.go')
-rw-r--r--plugins/temporal/protocol/converter.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/plugins/temporal/protocol/converter.go b/plugins/temporal/protocol/converter.go
new file mode 100644
index 00000000..406e70f4
--- /dev/null
+++ b/plugins/temporal/protocol/converter.go
@@ -0,0 +1,76 @@
+package protocol
+
+import (
+ commonpb "go.temporal.io/api/common/v1"
+ "go.temporal.io/sdk/converter"
+)
+
+type (
+ // DataConverter wraps Temporal data converter to enable direct access to the payloads.
+ DataConverter struct {
+ fallback converter.DataConverter
+ }
+)
+
+// NewDataConverter creates new data converter.
+func NewDataConverter(fallback converter.DataConverter) converter.DataConverter {
+ return &DataConverter{fallback: fallback}
+}
+
+// ToPayloads converts a list of values.
+func (r *DataConverter) ToPayloads(values ...interface{}) (*commonpb.Payloads, error) {
+ for _, v := range values {
+ if aggregated, ok := v.(*commonpb.Payloads); ok {
+ // bypassing
+ return aggregated, nil
+ }
+ }
+
+ return r.fallback.ToPayloads(values...)
+}
+
+// ToPayload converts single value to payload.
+func (r *DataConverter) ToPayload(value interface{}) (*commonpb.Payload, error) {
+ return r.fallback.ToPayload(value)
+}
+
+// FromPayloads converts to a list of values of different types.
+// Useful for deserializing arguments of function invocations.
+func (r *DataConverter) FromPayloads(payloads *commonpb.Payloads, valuePtrs ...interface{}) error {
+ if payloads == nil {
+ return nil
+ }
+
+ if len(valuePtrs) == 1 {
+ // input proxying
+ if input, ok := valuePtrs[0].(**commonpb.Payloads); ok {
+ *input = &commonpb.Payloads{}
+ (*input).Payloads = payloads.Payloads
+ return nil
+ }
+ }
+
+ for i := 0; i < len(payloads.Payloads); i++ {
+ err := r.FromPayload(payloads.Payloads[i], valuePtrs[i])
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// FromPayload converts single value from payload.
+func (r *DataConverter) FromPayload(payload *commonpb.Payload, valuePtr interface{}) error {
+ return r.fallback.FromPayload(payload, valuePtr)
+}
+
+// ToString converts payload object into human readable string.
+func (r *DataConverter) ToString(input *commonpb.Payload) string {
+ return r.fallback.ToString(input)
+}
+
+// ToStrings converts payloads object into human readable strings.
+func (r *DataConverter) ToStrings(input *commonpb.Payloads) []string {
+ return r.fallback.ToStrings(input)
+}