utils

package
v0.0.0-...-788542e Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package utils provides utility functions for protobuf type conversions.

This package offers bidirectional conversion between Protocol Buffer types (proto.Value, proto.Struct, proto.ListValue) and Go's native interface{}, map[string]interface{}, and []interface{} types.

The conversions support the full range of JSON-compatible types including:

  • Primitive types (string, bool, numbers)
  • Null values
  • Nested structures (maps and lists)
  • Binary data (encoded as base64 strings)

These utilities are particularly useful when working with dynamic data that needs to be passed between Go code and workflow engines (Cadence/Temporal) or other systems that use Protocol Buffers for serialization.

Usage:

// Convert proto.Value to Go interface{}
goValue, err := MapProtoValueToInterface(protoValue)

// Convert Go interface{} to proto.Value
protoValue, err := NewValue(goValue)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapProtoListValueToInterface

func MapProtoListValueToInterface(x *types.ListValue) ([]interface{}, error)

MapProtoListValueToInterface converts a proto.ListValue to a Go []interface{}.

This function recursively converts a Protocol Buffer ListValue (similar to a JSON array) to a Go slice. Each element is converted using MapProtoValueToInterface.

Returns an error if any element conversion fails.

func MapProtoStructToInterface

func MapProtoStructToInterface(v *types.Struct) (map[string]interface{}, error)

MapProtoStructToInterface converts a proto.Struct to a Go map[string]interface{}.

This function recursively converts a Protocol Buffer Struct (similar to a JSON object) to a Go map. Each field value is converted using MapProtoValueToInterface.

Returns an error if any field value conversion fails.

func MapProtoValueToInterface

func MapProtoValueToInterface(v *types.Value) (interface{}, error)

MapProtoValueToInterface converts a proto.Value to a Go interface{}.

This function recursively converts Protocol Buffer Value types to their corresponding Go representations:

  • StringValue → string
  • BoolValue → bool
  • NumberValue → float64
  • NullValue → nil
  • StructValue → map[string]interface{} (recursive)
  • ListValue → []interface{} (recursive)

Returns an error if the proto.Value contains an unexpected or unsupported kind.

func NewBoolValue

func NewBoolValue(v bool) *types.Value

NewBoolValue constructs a proto.Value representing a boolean.

func NewList

func NewList(v []interface{}) (*types.ListValue, error)

NewList constructs a proto.ListValue from a Go []interface{}.

The slice elements are recursively converted using NewValue.

Returns an error if any element conversion fails.

func NewListMap

func NewListMap(v []map[string]interface{}) (*types.ListValue, error)

NewListMap constructs a proto.ListValue from a Go []map[string]interface{}.

This is a specialized version of NewList for slices of maps. Each map is converted to a proto.Struct using NewStruct.

Returns an error if any map conversion fails.

func NewListValue

func NewListValue(v *types.ListValue) *types.Value

NewListValue constructs a proto.Value wrapping a proto.ListValue.

func NewNullValue

func NewNullValue() *types.Value

NewNullValue constructs a proto.Value representing a null value.

This is equivalent to JSON's null value.

func NewNumberValue

func NewNumberValue(v float64) *types.Value

NewNumberValue constructs a proto.Value representing a number.

All numeric types are stored as float64 in Protocol Buffers.

func NewStringList

func NewStringList(v []string) (*types.ListValue, error)

NewStringList constructs a proto.ListValue from a Go []string.

This is a specialized version of NewList optimized for string slices. Each string is converted to a proto.Value using NewStringValue.

func NewStringValue

func NewStringValue(v string) *types.Value

NewStringValue constructs a proto.Value representing a string.

func NewStruct

func NewStruct(v map[string]interface{}) (*types.Struct, error)

NewStruct constructs a proto.Struct from a Go map[string]interface{}.

The map keys must be valid UTF-8 strings. The map values are recursively converted using NewValue.

Returns an error if any key is invalid UTF-8 or any value conversion fails.

func NewStructValue

func NewStructValue(v *types.Struct) *types.Value

NewStructValue constructs a proto.Value wrapping a proto.Struct.

func NewValue

func NewValue(v interface{}) (*types.Value, error)

NewValue constructs a Value from a general-purpose Go interface.

	╔════════════════════════╤════════════════════════════════════════════╗
	║ Go type                │ Conversion                                 ║
	╠════════════════════════╪════════════════════════════════════════════╣
	║ nil                    │ stored as NullValue                        ║
	║ bool                   │ stored as BoolValue                        ║
	║ int, int32, int64      │ stored as NumberValue                      ║
	║ uint, uint32, uint64   │ stored as NumberValue                      ║
	║ float32, float64       │ stored as NumberValue                      ║
	║ string                 │ stored as StringValue; must be valid UTF-8 ║
	║ []byte                 │ stored as StringValue; base64-encoded      ║
	║ map[string]interface{} │ stored as StructValue                      ║
	║ []interface{}          │ stored as ListValue                        ║
 ║ []map[string]interface{} | stored as ListValue					  ║
	╚════════════════════════╧════════════════════════════════════════════╝

When converting an int64 or uint64 to a NumberValue, numeric precision loss is possible since they are stored as a float64.

NewValue converts a Go interface{} to a proto.Value.

This function performs type-based conversion from Go types to Protocol Buffer Value types. It supports all primitive types, maps, slices, and byte arrays.

Supported conversions are documented in the type table above. The function recursively converts nested structures and validates UTF-8 encoding for strings.

Returns an error if:

  • The input type is not supported
  • String values contain invalid UTF-8
  • Nested structure conversion fails

Types

This section is empty.

Jump to

Keyboard shortcuts

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