value

package module
v0.0.0-...-a882f1d Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2022 License: MIT Imports: 8 Imported by: 3

README

Value

This module uses Protobufs to serialise values for multi-language transfers.

NewValue will create an instance Value from an instance of interface{}, and ParseValue performs the reverse transformation to recover the interface{}.

Types supported:

  • Basic types: bool, int32/64, uint32/64, float32/64, string
  • time.Time and time.Duration
  • Pointers of the basic types and the time types
  • map[string]interface{}, where interface{} can be any of the basic types, pointers, slices or another map
  • slices of the above types and interface{}, to an arbitrary depth (i.e [][][][]int64 etc. are supported)

An error is returned if the type is not supported.

Proto

The Value message uses oneof, ensuring that only a single value is included in each instance.

ValueList provides an list of Value, of length zero of more.

ValueMap provides a mapping map<string, Value>.

Value can contain a ValueList or a ValueMap, allowing complex structures to be modelled (and hence serialised) within a single Value.

Parsing slices

When parsing a ValueList, the ValueListType enumeration stored within it allows the parser to attempt to return a strongly typed slice, but will default to []interface{} if this is not possible.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ValueListType_name = map[int32]string{
		0:   "UnknownValueListType",
		999: "Interface",
		1:   "Bool",
		2:   "Bytes",
		3:   "Double",
		4:   "Float",
		5:   "Int32",
		6:   "Int64",
		7:   "UInt32",
		8:   "UInt64",
		9:   "SInt32",
		10:  "SInt64",
		11:  "Fixed32",
		12:  "Fixed64",
		13:  "Time",
		14:  "Duration",
		15:  "ValueMap",
		16:  "ValueList",
		17:  "PtrBool",
		18:  "PtrDouble",
		19:  "PtrFloat",
		20:  "PtrInt32",
		21:  "PtrInt64",
		22:  "PtrUInt32",
		23:  "PtrUInt64",
		24:  "PtrSInt32",
		25:  "PtrSInt64",
		26:  "PtrFixed32",
		27:  "PtrFixed64",
		28:  "PtrTime",
		29:  "PtrDuration",
		30:  "String",
		31:  "PtrString",
	}
	ValueListType_value = map[string]int32{
		"UnknownValueListType": 0,
		"Interface":            999,
		"Bool":                 1,
		"Bytes":                2,
		"Double":               3,
		"Float":                4,
		"Int32":                5,
		"Int64":                6,
		"UInt32":               7,
		"UInt64":               8,
		"SInt32":               9,
		"SInt64":               10,
		"Fixed32":              11,
		"Fixed64":              12,
		"Time":                 13,
		"Duration":             14,
		"ValueMap":             15,
		"ValueList":            16,
		"PtrBool":              17,
		"PtrDouble":            18,
		"PtrFloat":             19,
		"PtrInt32":             20,
		"PtrInt64":             21,
		"PtrUInt32":            22,
		"PtrUInt64":            23,
		"PtrSInt32":            24,
		"PtrSInt64":            25,
		"PtrFixed32":           26,
		"PtrFixed64":           27,
		"PtrTime":              28,
		"PtrDuration":          29,
		"String":               30,
		"PtrString":            31,
	}
)

Enum value maps for ValueListType.

View Source
var File_value_proto protoreflect.FileDescriptor

Functions

func ParseValue

func ParseValue(v *Value) (interface{}, error)

ParseValue creates an instance of ValueParser that extracts go types

Example
createFloat64 := func(f float64) *float64 {
	p := new(float64)
	*p = f
	return p
}

slicePtrToString := func(i interface{}) string {
	v := reflect.ValueOf(i)
	s := ""
	for i := 0; i < v.Len(); i++ {
		if v.Index(i).IsNil() {
			s = s + "<nil> "
		} else {
			s = s + fmt.Sprintf("%v ", v.Index(i).Elem())
		}
	}
	return fmt.Sprintf("[%v]", strings.TrimSpace(s))
}

createValueAndParse := func(i interface{}) interface{} {
	v, _ := NewValue(i)
	p, _ := ParseValue(v)
	return p
}

i1 := createValueAndParse([]int64{1, 2, 3, 4})
i2 := createValueAndParse([]*float64{createFloat64(1.99), createFloat64(-2.12), nil})

fmt.Printf("%v %v, %v %v", i1, reflect.TypeOf(i1), slicePtrToString(i2), reflect.TypeOf(i2))
Output:

[1 2 3 4] []int64, [1.99 -2.12 <nil>] []*float64

Types

type Value

type Value struct {

	// Types that are assignable to V:
	//	*Value_IsNull
	//	*Value_X
	//	*Value_B
	//	*Value_Pb
	//	*Value_S
	//	*Value_Ps
	//	*Value_D
	//	*Value_Pd
	//	*Value_F
	//	*Value_Pf
	//	*Value_I32
	//	*Value_Pi32
	//	*Value_I64
	//	*Value_Pi64
	//	*Value_U32
	//	*Value_Pu32
	//	*Value_U64
	//	*Value_Pu64
	//	*Value_S32
	//	*Value_Ps32
	//	*Value_S64
	//	*Value_Ps64
	//	*Value_F32
	//	*Value_Pf32
	//	*Value_F64
	//	*Value_Pf64
	//	*Value_T
	//	*Value_Pt
	//	*Value_Dur
	//	*Value_Pdur
	//	*Value_M
	//	*Value_L
	V isValue_V `protobuf_oneof:"v"`
	// contains filtered or unexported fields
}

func NewValue

func NewValue(i interface{}) (*Value, error)

NewValue creates an instance of Value holding the specified value

Example
createAndGetMarshalledLength := func(i interface{}) int {
	v, _ := NewValue(i)
	b, _ := proto.Marshal(v)
	return len(b)
}

data := []interface{}{
	"Ultimate Question of Life, the Universe, and Everything",
	int64(42),
	float64(1e80),
	map[string]interface{}{
		"q": "Ultimate Question of Life, the Universe, and Everything",
		"a": int64(42),
		"n": float64(1e80),
	},
	[]int32{1, 2, 3},
	[]interface{}{int32(1), int32(2), int32(3)},
}
lengths := make([]int, len(data))

for i, d := range data {
	lengths[i] = createAndGetMarshalledLength(d)
}

fmt.Println(lengths)
Output:

[57 2 9 92 17 18]

func (*Value) Descriptor deprecated

func (*Value) Descriptor() ([]byte, []int)

Deprecated: Use Value.ProtoReflect.Descriptor instead.

func (*Value) GetB

func (x *Value) GetB() bool

func (*Value) GetD

func (x *Value) GetD() float64

func (*Value) GetDur

func (x *Value) GetDur() int64

func (*Value) GetF

func (x *Value) GetF() float32

func (*Value) GetF32

func (x *Value) GetF32() uint32

func (*Value) GetF64

func (x *Value) GetF64() uint64

func (*Value) GetI32

func (x *Value) GetI32() int32

func (*Value) GetI64

func (x *Value) GetI64() int64

func (*Value) GetIsNull

func (x *Value) GetIsNull() bool

func (*Value) GetL

func (x *Value) GetL() *Value_ValueList

func (*Value) GetM

func (x *Value) GetM() *Value_ValueMap

func (*Value) GetPb

func (x *Value) GetPb() bool

func (*Value) GetPd

func (x *Value) GetPd() float64

func (*Value) GetPdur

func (x *Value) GetPdur() int64

func (*Value) GetPf

func (x *Value) GetPf() float32

func (*Value) GetPf32

func (x *Value) GetPf32() uint32

func (*Value) GetPf64

func (x *Value) GetPf64() uint64

func (*Value) GetPi32

func (x *Value) GetPi32() int32

func (*Value) GetPi64

func (x *Value) GetPi64() int64

func (*Value) GetPs

func (x *Value) GetPs() string

func (*Value) GetPs32

func (x *Value) GetPs32() int32

func (*Value) GetPs64

func (x *Value) GetPs64() int64

func (*Value) GetPt

func (x *Value) GetPt() *timestamppb.Timestamp

func (*Value) GetPu32

func (x *Value) GetPu32() uint32

func (*Value) GetPu64

func (x *Value) GetPu64() uint64

func (*Value) GetS

func (x *Value) GetS() string

func (*Value) GetS32

func (x *Value) GetS32() int32

func (*Value) GetS64

func (x *Value) GetS64() int64

func (*Value) GetT

func (x *Value) GetT() *timestamppb.Timestamp

func (*Value) GetU32

func (x *Value) GetU32() uint32

func (*Value) GetU64

func (x *Value) GetU64() uint64

func (*Value) GetV

func (m *Value) GetV() isValue_V

func (*Value) GetX

func (x *Value) GetX() []byte

func (*Value) ProtoMessage

func (*Value) ProtoMessage()

func (*Value) ProtoReflect

func (x *Value) ProtoReflect() protoreflect.Message

func (*Value) Reset

func (x *Value) Reset()

func (*Value) String

func (x *Value) String() string

type ValueListType

type ValueListType int32
const (
	ValueListType_UnknownValueListType ValueListType = 0
	ValueListType_Interface            ValueListType = 999
	ValueListType_Bool                 ValueListType = 1
	ValueListType_Bytes                ValueListType = 2
	ValueListType_Double               ValueListType = 3
	ValueListType_Float                ValueListType = 4
	ValueListType_Int32                ValueListType = 5
	ValueListType_Int64                ValueListType = 6
	ValueListType_UInt32               ValueListType = 7
	ValueListType_UInt64               ValueListType = 8
	ValueListType_SInt32               ValueListType = 9
	ValueListType_SInt64               ValueListType = 10
	ValueListType_Fixed32              ValueListType = 11
	ValueListType_Fixed64              ValueListType = 12
	ValueListType_Time                 ValueListType = 13
	ValueListType_Duration             ValueListType = 14
	ValueListType_ValueMap             ValueListType = 15
	ValueListType_ValueList            ValueListType = 16
	ValueListType_PtrBool              ValueListType = 17
	ValueListType_PtrDouble            ValueListType = 18
	ValueListType_PtrFloat             ValueListType = 19
	ValueListType_PtrInt32             ValueListType = 20
	ValueListType_PtrInt64             ValueListType = 21
	ValueListType_PtrUInt32            ValueListType = 22
	ValueListType_PtrUInt64            ValueListType = 23
	ValueListType_PtrSInt32            ValueListType = 24
	ValueListType_PtrSInt64            ValueListType = 25
	ValueListType_PtrFixed32           ValueListType = 26
	ValueListType_PtrFixed64           ValueListType = 27
	ValueListType_PtrTime              ValueListType = 28
	ValueListType_PtrDuration          ValueListType = 29
	ValueListType_String               ValueListType = 30
	ValueListType_PtrString            ValueListType = 31
)

func (ValueListType) Descriptor

func (ValueListType) Enum

func (x ValueListType) Enum() *ValueListType

func (ValueListType) EnumDescriptor deprecated

func (ValueListType) EnumDescriptor() ([]byte, []int)

Deprecated: Use ValueListType.Descriptor instead.

func (ValueListType) Number

func (ValueListType) String

func (x ValueListType) String() string

func (ValueListType) Type

type Value_B

type Value_B struct {
	B bool `protobuf:"varint,3,opt,name=b,proto3,oneof"`
}

type Value_D

type Value_D struct {
	D float64 `protobuf:"fixed64,7,opt,name=d,proto3,oneof"`
}

type Value_Dur

type Value_Dur struct {
	Dur int64 `protobuf:"varint,29,opt,name=dur,proto3,oneof"`
}

type Value_F

type Value_F struct {
	F float32 `protobuf:"fixed32,9,opt,name=f,proto3,oneof"`
}

type Value_F32

type Value_F32 struct {
	F32 uint32 `protobuf:"fixed32,23,opt,name=f32,proto3,oneof"`
}

type Value_F64

type Value_F64 struct {
	F64 uint64 `protobuf:"fixed64,25,opt,name=f64,proto3,oneof"`
}

type Value_I32

type Value_I32 struct {
	I32 int32 `protobuf:"varint,11,opt,name=i32,proto3,oneof"`
}

type Value_I64

type Value_I64 struct {
	I64 int64 `protobuf:"varint,13,opt,name=i64,proto3,oneof"`
}

type Value_IsNull

type Value_IsNull struct {
	IsNull bool `protobuf:"varint,1,opt,name=isNull,proto3,oneof"`
}

type Value_L

type Value_L struct {
	L *Value_ValueList `protobuf:"bytes,32,opt,name=l,proto3,oneof"`
}

type Value_M

type Value_M struct {
	M *Value_ValueMap `protobuf:"bytes,31,opt,name=m,proto3,oneof"`
}

type Value_Pb

type Value_Pb struct {
	Pb bool `protobuf:"varint,4,opt,name=pb,proto3,oneof"`
}

type Value_Pd

type Value_Pd struct {
	Pd float64 `protobuf:"fixed64,8,opt,name=pd,proto3,oneof"`
}

type Value_Pdur

type Value_Pdur struct {
	Pdur int64 `protobuf:"varint,30,opt,name=pdur,proto3,oneof"`
}

type Value_Pf

type Value_Pf struct {
	Pf float32 `protobuf:"fixed32,10,opt,name=pf,proto3,oneof"`
}

type Value_Pf32

type Value_Pf32 struct {
	Pf32 uint32 `protobuf:"fixed32,24,opt,name=pf32,proto3,oneof"`
}

type Value_Pf64

type Value_Pf64 struct {
	Pf64 uint64 `protobuf:"fixed64,26,opt,name=pf64,proto3,oneof"`
}

type Value_Pi32

type Value_Pi32 struct {
	Pi32 int32 `protobuf:"varint,12,opt,name=pi32,proto3,oneof"`
}

type Value_Pi64

type Value_Pi64 struct {
	Pi64 int64 `protobuf:"varint,14,opt,name=pi64,proto3,oneof"`
}

type Value_Ps

type Value_Ps struct {
	Ps string `protobuf:"bytes,6,opt,name=ps,proto3,oneof"`
}

type Value_Ps32

type Value_Ps32 struct {
	Ps32 int32 `protobuf:"zigzag32,20,opt,name=ps32,proto3,oneof"`
}

type Value_Ps64

type Value_Ps64 struct {
	Ps64 int64 `protobuf:"zigzag64,22,opt,name=ps64,proto3,oneof"`
}

type Value_Pt

type Value_Pt struct {
	Pt *timestamppb.Timestamp `protobuf:"bytes,28,opt,name=pt,proto3,oneof"`
}

type Value_Pu32

type Value_Pu32 struct {
	Pu32 uint32 `protobuf:"varint,16,opt,name=pu32,proto3,oneof"`
}

type Value_Pu64

type Value_Pu64 struct {
	Pu64 uint64 `protobuf:"varint,18,opt,name=pu64,proto3,oneof"`
}

type Value_S

type Value_S struct {
	S string `protobuf:"bytes,5,opt,name=s,proto3,oneof"`
}

type Value_S32

type Value_S32 struct {
	S32 int32 `protobuf:"zigzag32,19,opt,name=s32,proto3,oneof"`
}

type Value_S64

type Value_S64 struct {
	S64 int64 `protobuf:"zigzag64,21,opt,name=s64,proto3,oneof"`
}

type Value_T

type Value_T struct {
	T *timestamppb.Timestamp `protobuf:"bytes,27,opt,name=t,proto3,oneof"`
}

type Value_U32

type Value_U32 struct {
	U32 uint32 `protobuf:"varint,15,opt,name=u32,proto3,oneof"`
}

type Value_U64

type Value_U64 struct {
	U64 uint64 `protobuf:"varint,17,opt,name=u64,proto3,oneof"`
}

type Value_ValueList

type Value_ValueList struct {
	V []*Value      `protobuf:"bytes,1,rep,name=v,proto3" json:"v,omitempty"`
	T ValueListType `protobuf:"varint,2,opt,name=t,proto3,enum=ValueListType" json:"t,omitempty"`
	// contains filtered or unexported fields
}

func (*Value_ValueList) Descriptor deprecated

func (*Value_ValueList) Descriptor() ([]byte, []int)

Deprecated: Use Value_ValueList.ProtoReflect.Descriptor instead.

func (*Value_ValueList) GetT

func (x *Value_ValueList) GetT() ValueListType

func (*Value_ValueList) GetV

func (x *Value_ValueList) GetV() []*Value

func (*Value_ValueList) ProtoMessage

func (*Value_ValueList) ProtoMessage()

func (*Value_ValueList) ProtoReflect

func (x *Value_ValueList) ProtoReflect() protoreflect.Message

func (*Value_ValueList) Reset

func (x *Value_ValueList) Reset()

func (*Value_ValueList) String

func (x *Value_ValueList) String() string

type Value_ValueMap

type Value_ValueMap struct {
	M map[string]*Value `` /* 143-byte string literal not displayed */
	// contains filtered or unexported fields
}

func (*Value_ValueMap) Descriptor deprecated

func (*Value_ValueMap) Descriptor() ([]byte, []int)

Deprecated: Use Value_ValueMap.ProtoReflect.Descriptor instead.

func (*Value_ValueMap) GetM

func (x *Value_ValueMap) GetM() map[string]*Value

func (*Value_ValueMap) ProtoMessage

func (*Value_ValueMap) ProtoMessage()

func (*Value_ValueMap) ProtoReflect

func (x *Value_ValueMap) ProtoReflect() protoreflect.Message

func (*Value_ValueMap) Reset

func (x *Value_ValueMap) Reset()

func (*Value_ValueMap) String

func (x *Value_ValueMap) String() string

type Value_X

type Value_X struct {
	X []byte `protobuf:"bytes,2,opt,name=x,proto3,oneof"`
}

Jump to

Keyboard shortcuts

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