Documentation
¶
Overview ¶
Package protoconv provides helper functions to convert between protobuf values and standard Go types.
Working with protobuf Values often requires a fair amount of boilerplate and this package hopes to simplify that.
For instance, if one wants to get an int from a struct.Value, one would do something like this:
// val is a struct.Value pointer
v, ok := val.Kind.(*structpb.Value_NumberValue)
if !ok {
// deal with the type being wrong
}
i := int(v.NumberValue)
protobuf simplifies that to
i := protoconv.Int(val)
Note that for simplicity sake, all conversion functions return the empty value for the types if the provided Value is not of the correct type. This mimicks the behaviour of protobuf's own functions.
Creating a new value is also simplified. For instance, to create a value from an int, one normally needs to do this:
i := 42
val := &structpb.Value{
Kind: &structpb.Value_NumberValue{
NumberValue: float64(i),
},
}
And protoconv simplifies this to
val := protoconv.IntVal(42)
For lists this is even more important. For instance, to create list with a few integers, you'd need to do this
listVal := &structpb.ListValue
for i := 0; i < 2; i++ {
v := &structpb.Value{
Kind: &structpb.Value_NumberValue{
NumberValue: float64(i),
},
}
listVal.Values = append(listVal.Values, v)
}
val := &structpb.Value{
Kind: &structpb.Value_ListValue{
ListValue: listVal,
},
}
But with protoconv
list := protoconv.NewList()
for i := 0; i < 2; i++ {
list.Append(protoconv.IntVal(i))
}
val := list.Value()
Iterating through a list of Value containing integers so we can append them to a slice would be
listVal := val.Kind.(*structpb.Value_ListValue)
for _, v := range listVal.ListValue.Values {
nv, ok := v.Kind.(*structpb.Value_NumberValue)
if ok {
ilist = append(ilist, int(nv.NumberValue))
}
}
This becomes either this
for _, v := range list.Values() {
ilist = append(ilist, protoconv.Int(v))
}
Or alternatively
list.Traverse(func (v *structpb.Value) error {
ilist = append(ilist, protoconv.Int(v))
return nil
})
Index ¶
- func AppendValue(lv *structpb.Value, val *structpb.Value) error
- func AsString(value *structpb.Value) string
- func Bool(value *structpb.Value) bool
- func BoolVal(val bool) *structpb.Value
- func Empty() *empty.Empty
- func Float32(value *structpb.Value) float32
- func Float32Val(val float32) *structpb.Value
- func Float64(value *structpb.Value) float64
- func Float64Val(val float64) *structpb.Value
- func Int(value *structpb.Value) int
- func Int32(value *structpb.Value) int32
- func Int32Val(val int32) *structpb.Value
- func Int64(value *structpb.Value) int64
- func Int64Val(val int64) *structpb.Value
- func IntVal(val int) *structpb.Value
- func IsBool(value *structpb.Value) bool
- func IsList(value *structpb.Value) bool
- func IsNull(value *structpb.Value) bool
- func IsNumber(value *structpb.Value) bool
- func IsString(value *structpb.Value) bool
- func IsStruct(value *structpb.Value) bool
- func ListValue() *structpb.Value
- func NullVal() *structpb.Value
- func StringVal(val string) *structpb.Value
- func StructVal() *structpb.Value
- func TraverseListValue(lv *structpb.Value, fn func(*structpb.Value) error) error
- func Uint(value *structpb.Value) uint
- func Uint32(value *structpb.Value) uint32
- func Uint32Val(val uint32) *structpb.Value
- func Uint64(value *structpb.Value) uint64
- func Uint64Val(val uint64) *structpb.Value
- func UintVal(val uint) *structpb.Value
- type Boolean
- type List
- func (lv *List) Append(val *structpb.Value)
- func (lv List) Len() int
- func (lv List) ListValue() *structpb.Value_ListValue
- func (lv *List) SetValue(value *structpb.Value) error
- func (lv List) Traverse(fn func(val *structpb.Value) error) error
- func (lv List) Value() *structpb.Value
- func (lv *List) Values() []*structpb.Value
- type Null
- type Number
- func (n Number) Float32() float32
- func (n Number) Float64() float64
- func (n Number) Int() int
- func (n Number) Int32() int32
- func (n Number) Int64() int64
- func (n *Number) SetFloat32(val float32) *Number
- func (n *Number) SetFloat64(val float64) *Number
- func (n *Number) SetInt(val int) *Number
- func (n *Number) SetInt32(val int32) *Number
- func (n *Number) SetInt64(val int64) *Number
- func (n *Number) SetUint(val uint) *Number
- func (n *Number) SetUint32(val uint32) *Number
- func (n *Number) SetUint64(val uint64) *Number
- func (n *Number) SetValue(value *structpb.Value) error
- func (n Number) Uint() uint
- func (n Number) Uint32() uint32
- func (n Number) Uint64() uint64
- func (n Number) Value() *structpb.Value
- type ProtoValuer
- type String
- type Struct
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendValue ¶
AppendValue appends a value to the a list
func AsString ¶
AsString returns value as a string. If value does not hold a string, returns the empty string
func Float32Val ¶
Float32Val creates a new Value with val
func Float64Val ¶
Float64Val creates a new Value with val
func TraverseListValue ¶
TraverseListValue traverses a ListValue calling fn for each value within. If fn returns an error, the traverse is stopped and the errors is returned
Types ¶
type Boolean ¶
type Boolean struct {
// contains filtered or unexported fields
}
Boolean holds a boolean Value
type List ¶
type List struct {
// contains filtered or unexported fields
}
List is a Value holding a list of Values
func (List) ListValue ¶
func (lv List) ListValue() *structpb.Value_ListValue
ListValue returns the List as a Value_ListValue
func (List) Traverse ¶
Traverse will execute fn for each Value contained in the List. If fn returns an error, the traverse is aborted and the error is returned.
Example ¶
package main
import (
"log"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/robteix/protoconv"
)
func main() {
// populate a list with numbers
list := protoconv.NewList()
for i := 0; i < 10; i++ {
list.Append(protoconv.IntVal(i))
}
// traverse the list and calculate the sum of all
// numbers in it
sum := 0
err := list.Traverse(func(val *structpb.Value) error {
sum += protoconv.Int(val)
return nil
})
if err != nil {
panic(err)
}
log.Println(sum)
}
Output:
type Null ¶
type Null struct {
// contains filtered or unexported fields
}
Null holds a struct.Value of type NullValue
type Number ¶
type Number struct {
structpb.Value_NumberValue
}
Number holds a numeric Value
func (*Number) SetFloat32 ¶
SetFloat32 sets the value of the Number to the float32 value
func (*Number) SetFloat64 ¶
SetFloat64 sets the value of the Number to the float64 value
type ProtoValuer ¶
ProtoValuer is an interface that provides values
type String ¶
type String string
String holds a string Value
func (String) StringValue ¶
func (s String) StringValue() *structpb.Value_StringValue
StringValue returns the String as a Value_StringValue
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
Struct holds a struct.Value of type StructValue
func (*Struct) Get ¶
Get returns the value of key. If key is not found, the returned value is a NullValue
func (*Struct) Remove ¶
Remove removes the item indexed by key from the Struct. If key does not exists, this does nothing