summaryrefslogtreecommitdiff
path: root/plugins/temporal/protocol/converter.go
blob: 406e70f4d193296106d9ee1c0d89ed39bc10c8fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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)
}