converter

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2021 License: MIT Imports: 13 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	// MetadataEncoding is "encoding"
	MetadataEncoding = "encoding"
	// MetadataEncodingBinary is "binary/plain"
	MetadataEncodingBinary = "binary/plain"
	// MetadataEncodingJSON is "json/plain"
	MetadataEncodingJSON = "json/plain"
	// MetadataEncodingNil is "binary/null"
	MetadataEncodingNil = "binary/null"
	// MetadataEncodingProtoJSON is "json/protobuf"
	MetadataEncodingProtoJSON = "json/protobuf"
	// MetadataEncodingProto is "binary/protobuf"
	MetadataEncodingProto = "binary/protobuf"
)

Variables

View Source
var (
	// ErrMetadataIsNotSet is returned when metadata is not set.
	ErrMetadataIsNotSet = errors.New("metadata is not set")
	// ErrEncodingIsNotSet is returned when payload encoding metadata is not set.
	ErrEncodingIsNotSet = errors.New("payload encoding metadata is not set")
	// ErrEncodingIsNotSupported is returned when payload encoding is not supported.
	ErrEncodingIsNotSupported = errors.New("payload encoding is not supported")
	// ErrUnableToEncode is returned when unable to encode.
	ErrUnableToEncode = errors.New("unable to encode")
	// ErrUnableToDecode is returned when unable to decode.
	ErrUnableToDecode = errors.New("unable to decode")
	// ErrUnableToSetValue is returned when unable to set value.
	ErrUnableToSetValue = errors.New("unable to set value")
	// ErrUnableToFindConverter is returned when unable to find converter.
	ErrUnableToFindConverter = errors.New("unable to find converter")
	// ErrTypeNotImplementProtoMessage is returned when value doesn't implement proto.Message.
	ErrTypeNotImplementProtoMessage = errors.New("type doesn't implement proto.Message")
	// ErrValuePtrIsNotPointer is returned when proto value is not a pointer.
	ErrValuePtrIsNotPointer = errors.New("not a pointer type")
	// ErrValuePtrMustConcreteType is returned when proto value is of interface type.
	ErrValuePtrMustConcreteType = errors.New("must be a concrete type, not interface")
	// ErrTypeIsNotByteSlice is returned when value is not of *[]byte type.
	ErrTypeIsNotByteSlice = errors.New("type is not *[]byte")
)

Functions

This section is empty.

Types

type ByteSlicePayloadConverter

type ByteSlicePayloadConverter struct {
}

ByteSlicePayloadConverter pass through []byte to Data field in payload.

func NewByteSlicePayloadConverter

func NewByteSlicePayloadConverter() *ByteSlicePayloadConverter

NewByteSlicePayloadConverter creates new instance of ByteSlicePayloadConverter.

func (*ByteSlicePayloadConverter) Encoding

func (c *ByteSlicePayloadConverter) Encoding() string

Encoding returns MetadataEncodingBinary.

func (*ByteSlicePayloadConverter) FromPayload

func (c *ByteSlicePayloadConverter) FromPayload(payload *commonpb.Payload, valuePtr interface{}) error

FromPayload converts single []byte value from payload.

func (*ByteSlicePayloadConverter) ToPayload

func (c *ByteSlicePayloadConverter) ToPayload(value interface{}) (*commonpb.Payload, error)

ToPayload converts single []byte value to payload.

func (*ByteSlicePayloadConverter) ToString

func (c *ByteSlicePayloadConverter) ToString(payload *commonpb.Payload) string

ToString converts payload object into human readable string.

type CompositeDataConverter

type CompositeDataConverter struct {
	// contains filtered or unexported fields
}

CompositeDataConverter applies PayloadConverters in specified order.

func (*CompositeDataConverter) FromPayload

func (dc *CompositeDataConverter) FromPayload(payload *commonpb.Payload, valuePtr interface{}) error

FromPayload converts single value from payload.

func (*CompositeDataConverter) FromPayloads

func (dc *CompositeDataConverter) FromPayloads(payloads *commonpb.Payloads, valuePtrs ...interface{}) error

FromPayloads converts to a list of values of different types.

func (*CompositeDataConverter) ToPayload

func (dc *CompositeDataConverter) ToPayload(value interface{}) (*commonpb.Payload, error)

ToPayload converts single value to payload.

func (*CompositeDataConverter) ToPayloads

func (dc *CompositeDataConverter) ToPayloads(values ...interface{}) (*commonpb.Payloads, error)

ToPayloads converts a list of values.

func (*CompositeDataConverter) ToString

func (dc *CompositeDataConverter) ToString(payload *commonpb.Payload) string

ToString converts payload object into human readable string.

func (*CompositeDataConverter) ToStrings

func (dc *CompositeDataConverter) ToStrings(payloads *commonpb.Payloads) []string

ToStrings converts payloads object into human readable strings.

type DataConverter

type DataConverter interface {
	// ToPayload converts single value to payload.
	ToPayload(value interface{}) (*commonpb.Payload, error)
	// FromPayload converts single value from payload.
	FromPayload(payload *commonpb.Payload, valuePtr interface{}) error

	// ToPayloads converts a list of values.
	ToPayloads(value ...interface{}) (*commonpb.Payloads, error)
	// FromPayloads converts to a list of values of different types.
	// Useful for deserializing arguments of function invocations.
	FromPayloads(payloads *commonpb.Payloads, valuePtrs ...interface{}) error

	// ToString converts payload object into human readable string.
	ToString(input *commonpb.Payload) string
	// ToStrings converts payloads object into human readable strings.
	ToStrings(input *commonpb.Payloads) []string
}

DataConverter is used by the framework to serialize/deserialize input and output of activity/workflow that need to be sent over the wire. To encode/decode workflow arguments, set DataConverter in client, through client.Options. To override DataConverter for specific activity or child workflow use workflow.WithDataConverter to create new Context, and pass that context to ExecuteActivity/ExecuteChildWorkflow calls. Temporal support using different DataConverters for different activity/childWorkflow in same workflow.

func GetDefaultDataConverter

func GetDefaultDataConverter() DataConverter

GetDefaultDataConverter returns default data converter used by Temporal worker.

func NewCompositeDataConverter

func NewCompositeDataConverter(payloadConverters ...PayloadConverter) DataConverter

NewCompositeDataConverter creates new instance of CompositeDataConverter from ordered list of PayloadConverters. Order is important here because during serialization DataConverter will try PayloadsConverters in that order until PayloadConverter returns non nil payload. Last PayloadConverter should always serialize the value (JSONPayloadConverter is good candidate for it).

type EncodedValue

type EncodedValue interface {
	// HasValue return whether there is value encoded.
	HasValue() bool
	// Get extract the encoded value into strong typed value pointer.
	Get(valuePtr interface{}) error
}

EncodedValue is used to encapsulate/extract encoded value from workflow/activity.

type EncodedValues

type EncodedValues interface {
	// HasValues return whether there are values encoded.
	HasValues() bool
	// Get extract the encoded values into strong typed value pointers.
	Get(valuePtr ...interface{}) error
}

EncodedValues is used to encapsulate/extract encoded one or more values from workflow/activity.

type EncodingDataConverter

type EncodingDataConverter struct {
	// contains filtered or unexported fields
}

EncodingDataConverter is a DataConverter that wraps an underlying data converter and supports chained encoding of just the payload without regard for serialization to/from actual types.

Example (Compression)
package main

import (
	"fmt"
	"strings"

	"github.com/spiral/sdk-go/converter"
)

func main() {
	defaultConv := converter.GetDefaultDataConverter()
	// Create Zlib compression converter
	zlibConv := converter.NewEncodingDataConverter(
		defaultConv,
		converter.NewZlibEncoder(converter.ZlibEncoderOptions{}),
	)

	// Create payloads with both
	bigString := strings.Repeat("aabbcc", 200)
	uncompPayload, _ := defaultConv.ToPayload(bigString)
	compPayload, _ := zlibConv.ToPayload(bigString)

	// The zlib payload is smaller
	fmt.Printf("Uncompressed payload size: %v (encoding: %s)\n",
		len(uncompPayload.Data), uncompPayload.Metadata[converter.MetadataEncoding])
	fmt.Printf("Compressed payload size: %v (encoding: %s)\n",
		len(compPayload.Data), compPayload.Metadata[converter.MetadataEncoding])

	// Convert from payload and confirm the same string. This uses the same
	// compression converter because the converter does not do anything to
	// payloads it didn't previously convert.
	var uncompValue, compValue string
	_ = zlibConv.FromPayload(uncompPayload, &uncompValue)
	_ = zlibConv.FromPayload(compPayload, &compValue)
	fmt.Printf("Uncompressed payload back to original? %v\n", uncompValue == bigString)
	fmt.Printf("Compressed payload back to original? %v\n", compValue == bigString)

}
Output:

Uncompressed payload size: 1202 (encoding: json/plain)
Compressed payload size: 57 (encoding: binary/zlib)
Uncompressed payload back to original? true
Compressed payload back to original? true

func NewEncodingDataConverter

func NewEncodingDataConverter(parent DataConverter, encoders ...PayloadEncoder) *EncodingDataConverter

NewEncodingDataConverter wraps the given parent DataConverter and performs encoding/decoding on the payload via the given encoders. When encoding for ToPayload(s), the encoders are applied last to first meaning the earlier encoders wrap the later ones. When decoding for FromPayload(s) and ToString(s), the encoders are applied first to last to reverse the effect.

func (*EncodingDataConverter) FromPayload

func (e *EncodingDataConverter) FromPayload(payload *commonpb.Payload, valuePtr interface{}) error

FromPayload implements DataConverter.FromPayload performing decoding on the given payload before sending to the parent FromPayload.

func (*EncodingDataConverter) FromPayloads

func (e *EncodingDataConverter) FromPayloads(payloads *commonpb.Payloads, valuePtrs ...interface{}) error

FromPayloads implements DataConverter.FromPayloads performing decoding on the given payloads before sending to the parent FromPayloads.

func (*EncodingDataConverter) ToPayload

func (e *EncodingDataConverter) ToPayload(value interface{}) (*commonpb.Payload, error)

ToPayload implements DataConverter.ToPayload performing encoding on the result of the parent's ToPayload call.

func (*EncodingDataConverter) ToPayloads

func (e *EncodingDataConverter) ToPayloads(value ...interface{}) (*commonpb.Payloads, error)

ToPayloads implements DataConverter.ToPayloads performing encoding on the result of the parent's ToPayloads call.

func (*EncodingDataConverter) ToString

func (e *EncodingDataConverter) ToString(input *commonpb.Payload) string

ToString implements DataConverter.ToString performing decoding on the given payload before sending to the parent ToString.

func (*EncodingDataConverter) ToStrings

func (e *EncodingDataConverter) ToStrings(input *commonpb.Payloads) []string

ToStrings implements DataConverter.ToStrings using ToString for each value.

type JSONPayloadConverter

type JSONPayloadConverter struct {
}

JSONPayloadConverter converts to/from JSON.

func NewJSONPayloadConverter

func NewJSONPayloadConverter() *JSONPayloadConverter

NewJSONPayloadConverter creates new instance of JSONPayloadConverter.

func (*JSONPayloadConverter) Encoding

func (c *JSONPayloadConverter) Encoding() string

Encoding returns MetadataEncodingJSON.

func (*JSONPayloadConverter) FromPayload

func (c *JSONPayloadConverter) FromPayload(payload *commonpb.Payload, valuePtr interface{}) error

FromPayload converts single value from payload.

func (*JSONPayloadConverter) ToPayload

func (c *JSONPayloadConverter) ToPayload(value interface{}) (*commonpb.Payload, error)

ToPayload converts single value to payload.

func (*JSONPayloadConverter) ToString

func (c *JSONPayloadConverter) ToString(payload *commonpb.Payload) string

ToString converts payload object into human readable string.

type NilPayloadConverter

type NilPayloadConverter struct {
}

NilPayloadConverter doesn't set Data field in payload.

func NewNilPayloadConverter

func NewNilPayloadConverter() *NilPayloadConverter

NewNilPayloadConverter creates new instance of NilPayloadConverter.

func (*NilPayloadConverter) Encoding

func (c *NilPayloadConverter) Encoding() string

Encoding returns MetadataEncodingNil.

func (*NilPayloadConverter) FromPayload

func (c *NilPayloadConverter) FromPayload(_ *commonpb.Payload, valuePtr interface{}) error

FromPayload converts single nil value from payload.

func (*NilPayloadConverter) ToPayload

func (c *NilPayloadConverter) ToPayload(value interface{}) (*commonpb.Payload, error)

ToPayload converts single nil value to payload.

func (*NilPayloadConverter) ToString

ToString converts payload object into human readable string.

type PayloadConverter

type PayloadConverter interface {
	// ToPayload converts single value to payload. It should return nil if the PayloadConveter can not convert passed value (i.e. type is unknown).
	ToPayload(value interface{}) (*commonpb.Payload, error)
	// FromPayload converts single value from payload. valuePtr should be a reference to the variable of the type that is corresponding for payload encoding.
	// Otherwise it should return error.
	FromPayload(payload *commonpb.Payload, valuePtr interface{}) error
	// ToString converts payload object into human readable string.
	ToString(*commonpb.Payload) string

	// Encoding returns encoding supported by PayloadConverter.
	Encoding() string
}

PayloadConverter is an interface to convert a single payload.

type PayloadEncoder

type PayloadEncoder interface {
	// Encode optionally encodes the given payload which is guaranteed to never
	// be nil. The byte slices of the payload's metadata or data should never be
	// mutated directly, but they can be referenced or replaced.
	Encode(*commonpb.Payload) error

	// Decode optionally decodes the given payload which is guaranteed to never
	// be nil. The byte slices of the payload's metadata or data should never be
	// mutated directly, but they can be referenced or replaced.
	//
	// For compatibility reasons, implementers should take care not to decode
	// payloads that were not previously encoded.
	Decode(*commonpb.Payload) error
}

PayloadEncoder is an encoder that encodes or decodes the given payload.

These can be used (and even chained) in NewEncodingDataConverter. For example, NewZlibEncoder returns a PayloadEncoder that can be used for compression.

func NewZlibEncoder

func NewZlibEncoder(options ZlibEncoderOptions) PayloadEncoder

NewZlibEncoder creates a PayloadEncoder for use in NewEncodingDataConverter to support zlib payload compression.

While this serves as a reasonable example of a compression encoder, callers may prefer alternative compression algorithms for lots of small payloads.

type ProtoJSONPayloadConverter

type ProtoJSONPayloadConverter struct {
	// contains filtered or unexported fields
}

ProtoJSONPayloadConverter converts proto objects to/from JSON.

func NewProtoJSONPayloadConverter

func NewProtoJSONPayloadConverter() *ProtoJSONPayloadConverter

NewProtoJSONPayloadConverter creates new instance of ProtoJSONPayloadConverter.

func (*ProtoJSONPayloadConverter) Encoding

func (c *ProtoJSONPayloadConverter) Encoding() string

Encoding returns MetadataEncodingProtoJSON.

func (*ProtoJSONPayloadConverter) FromPayload

func (c *ProtoJSONPayloadConverter) FromPayload(payload *commonpb.Payload, valuePtr interface{}) error

FromPayload converts single proto value from payload.

func (*ProtoJSONPayloadConverter) ToPayload

func (c *ProtoJSONPayloadConverter) ToPayload(value interface{}) (*commonpb.Payload, error)

ToPayload converts single proto value to payload.

func (*ProtoJSONPayloadConverter) ToString

func (c *ProtoJSONPayloadConverter) ToString(payload *commonpb.Payload) string

ToString converts payload object into human readable string.

type ProtoPayloadConverter

type ProtoPayloadConverter struct {
}

ProtoPayloadConverter converts proto objects to protobuf binary format.

func NewProtoPayloadConverter

func NewProtoPayloadConverter() *ProtoPayloadConverter

NewProtoPayloadConverter creates new instance of ProtoPayloadConverter.

func (*ProtoPayloadConverter) Encoding

func (c *ProtoPayloadConverter) Encoding() string

Encoding returns MetadataEncodingProto.

func (*ProtoPayloadConverter) FromPayload

func (c *ProtoPayloadConverter) FromPayload(payload *commonpb.Payload, valuePtr interface{}) error

FromPayload converts single proto value from payload.

func (*ProtoPayloadConverter) ToPayload

func (c *ProtoPayloadConverter) ToPayload(value interface{}) (*commonpb.Payload, error)

ToPayload converts single proto value to payload.

func (*ProtoPayloadConverter) ToString

func (c *ProtoPayloadConverter) ToString(payload *commonpb.Payload) string

ToString converts payload object into human readable string.

type ZlibEncoderOptions

type ZlibEncoderOptions struct {
	// If true, the zlib encoder will encode the contents even if there is no size
	// benefit. Otherwise, the zlib encoder will only use the encoded value if it
	// is smaller.
	AlwaysEncode bool
}

ZlibEncoderOptions are options for NewZlibEncoder. All fields are optional.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL