protoconv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2018 License: MIT Imports: 3 Imported by: 0

README

protoconv

protoconv is a Go package providing helper functions to convert between Go types and struct.Value and its related types.

I created this package to help me when I need to work with ptypes/struct values as they often need quite a lot of boilerplate for simple operations.

Documentation

You can find the full documentation on godoc.

Installation --

go get -u github.com/robteix/protoconv

The purpose of this package is to turn something like this --

val := &structpb.Value{
    Kind: &structpb.Value_NumberValue{
        NumberValue: float64(12),
    },
}

into something like --

val := protoconv.IntVal(12)

And getting an int from a Value from this --

v, ok := val.Kind.(*structpb.Value_NumberValue)
if !ok {
    // do something
}
i := int(v.NumberValue)

To this --

i := protoconv.Int(val)

In short, helper functions to help converting from struct.Value to standard Go types.

Status

protoconv is ready for production use but it may lack functionality you need as I add functions whenever I need them at work.

Feel free to contribute pull requests or file an issue if you need something that isn't there yet.

License

Copyright (c) 2017 Roberto Selbach Teixeira

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendValue

func AppendValue(lv *structpb.Value, val *structpb.Value) error

AppendValue appends a value to the a list

func AsString

func AsString(value *structpb.Value) string

AsString returns value as a string. If value does not hold a string, returns the empty string

func Bool

func Bool(value *structpb.Value) bool

Bool returns value as an bool. If value is not a BoolValue, the function returns false

func BoolVal

func BoolVal(val bool) *structpb.Value

BoolVal creates a new Value with val

func Empty

func Empty() *empty.Empty

Empty returns a new empty.Empty

func Float32

func Float32(value *structpb.Value) float32

Float32 returns value as an float32. If value is not a NumberValue, returns 0

func Float32Val

func Float32Val(val float32) *structpb.Value

Float32Val creates a new Value with val

func Float64

func Float64(value *structpb.Value) float64

Float64 returns value as an float64. If the value is not a NumberValue, returns 0

func Float64Val

func Float64Val(val float64) *structpb.Value

Float64Val creates a new Value with val

func Int

func Int(value *structpb.Value) int

Int returns value as an int. If value is not a NumberValue, returns 0

func Int32

func Int32(value *structpb.Value) int32

Int32 returns value as an int32. If the value is not a NumberValue, returns 0

func Int32Val

func Int32Val(val int32) *structpb.Value

Int32Val creates a new Value with val

func Int64

func Int64(value *structpb.Value) int64

Int64 returns value as an int64. If the value is not a NumberValue, returns 0

func Int64Val

func Int64Val(val int64) *structpb.Value

Int64Val creates a new Value with val

func IntVal

func IntVal(val int) *structpb.Value

IntVal creates a new Value with val

func IsBool

func IsBool(value *structpb.Value) bool

IsBool tests whether value is of type BoolValue

func IsList

func IsList(value *structpb.Value) bool

IsList tests if the value is of type ListValue

func IsNull

func IsNull(value *structpb.Value) bool

IsNull tests whether value is a NullValue

func IsNumber

func IsNumber(value *structpb.Value) bool

IsNumber tests whether value is a NumberValue

func IsString

func IsString(value *structpb.Value) bool

IsString tests if the value is a StringValue

func IsStruct

func IsStruct(value *structpb.Value) bool

IsStruct tests whether value is a StructValue

func ListValue

func ListValue() *structpb.Value

ListValue returns a new Value of type ListValue

func NullVal

func NullVal() *structpb.Value

NullVal returns a new Value of type NullValue

func StringVal

func StringVal(val string) *structpb.Value

StringVal creates a new Value with val

func StructVal

func StructVal() *structpb.Value

StructVal returns a new empty Value of type StructValue

func TraverseListValue

func TraverseListValue(lv *structpb.Value, fn func(*structpb.Value) error) error

TraverseListValue traverses a ListValue calling fn for each value within. If fn returns an error, the traverse is stopped and the errors is returned

func Uint

func Uint(value *structpb.Value) uint

Uint returns value as an uint. If value is not a NumberValue, returns 0

func Uint32

func Uint32(value *structpb.Value) uint32

Uint32 returns value as an uint32. If value is not a NumberValue, returns 0

func Uint32Val

func Uint32Val(val uint32) *structpb.Value

Uint32Val creates a new Value with val

func Uint64

func Uint64(value *structpb.Value) uint64

Uint64 returns value as an uint64. If value is not a NumberValue, returns 0

func Uint64Val

func Uint64Val(val uint64) *structpb.Value

Uint64Val creates a new Value with val

func UintVal

func UintVal(val uint) *structpb.Value

UintVal creates a new Value with val

Types

type Boolean

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

Boolean holds a boolean Value

func NewBoolean

func NewBoolean() *Boolean

NewBoolean returns a new Value of type BoolValue

func (Boolean) Bool

func (b Boolean) Bool() bool

Bool returns the value of Number as an bool

func (*Boolean) SetBool

func (b *Boolean) SetBool(val bool) *Boolean

SetBool sets the value of the Boolean to the int32 value

func (*Boolean) SetValue

func (b *Boolean) SetValue(value *structpb.Value) error

SetValue sets a new value

func (Boolean) Value

func (b Boolean) Value() *structpb.Value

Value returns the Boolean as a Value

type List

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

List is a Value holding a list of Values

func NewList

func NewList() *List

NewList creates a new value of type list

func (*List) Append

func (lv *List) Append(val *structpb.Value)

Append will append val to the List values

func (List) Len

func (lv List) Len() int

Len returns the number of Values stored in the list

func (List) ListValue

func (lv List) ListValue() *structpb.Value_ListValue

ListValue returns the List as a Value_ListValue

func (*List) SetValue

func (lv *List) SetValue(value *structpb.Value) error

SetValue sets the value of the List

func (List) Traverse

func (lv List) Traverse(fn func(val *structpb.Value) error) error

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)
}

func (List) Value

func (lv List) Value() *structpb.Value

Value returns the List as a Value

func (*List) Values

func (lv *List) Values() []*structpb.Value

Values returns the list of Values in the list

type Null

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

Null holds a struct.Value of type NullValue

func NewNull

func NewNull() *Null

NewNull creates a new Null

func (*Null) SetValue

func (e *Null) SetValue(value *structpb.Value) error

SetValue sets the value of the nullvalue. This function does nothing.

func (*Null) Value

func (e *Null) Value() *structpb.Value

Value returns Null as a struct.Value

type Number

type Number struct {
	structpb.Value_NumberValue
}

Number holds a numeric Value

func NewNumber

func NewNumber() *Number

NewNumber returns a new Value of type NumberValue

func (Number) Float32

func (n Number) Float32() float32

Float32 returns the value of Number as an float32

func (Number) Float64

func (n Number) Float64() float64

Float64 returns the value of Number as an float64

func (Number) Int

func (n Number) Int() int

Int returns the value of Number as an int

func (Number) Int32

func (n Number) Int32() int32

Int32 returns the value of Number as an int32

func (Number) Int64

func (n Number) Int64() int64

Int64 returns the value of Number as an int64

func (*Number) SetFloat32

func (n *Number) SetFloat32(val float32) *Number

SetFloat32 sets the value of the Number to the float32 value

func (*Number) SetFloat64

func (n *Number) SetFloat64(val float64) *Number

SetFloat64 sets the value of the Number to the float64 value

func (*Number) SetInt

func (n *Number) SetInt(val int) *Number

SetInt sets the value of the Number to the int value

func (*Number) SetInt32

func (n *Number) SetInt32(val int32) *Number

SetInt32 sets the value of the Number to the int32 value

func (*Number) SetInt64

func (n *Number) SetInt64(val int64) *Number

SetInt64 sets the value of the Number to the int64 value

func (*Number) SetUint

func (n *Number) SetUint(val uint) *Number

SetUint sets the value of the Number to the uint value

func (*Number) SetUint32

func (n *Number) SetUint32(val uint32) *Number

SetUint32 sets the value of the Number to the uint32 value

func (*Number) SetUint64

func (n *Number) SetUint64(val uint64) *Number

SetUint64 sets the value of the Number to the uint64 value

func (*Number) SetValue

func (n *Number) SetValue(value *structpb.Value) error

SetValue sets a new value

func (Number) Uint

func (n Number) Uint() uint

Uint returns the value of Number as an uint

func (Number) Uint32

func (n Number) Uint32() uint32

Uint32 returns the value of Number as an uint32

func (Number) Uint64

func (n Number) Uint64() uint64

Uint64 returns the value of Number as an uint64

func (Number) Value

func (n Number) Value() *structpb.Value

Value returns the Number as a Value

type ProtoValuer

type ProtoValuer interface {
	Value() *structpb.Value
	SetValue(*structpb.Value) error
}

ProtoValuer is an interface that provides values

type String

type String string

String holds a string Value

func NewString

func NewString() *String

NewString returns a new Value of type StringValue

func (*String) SetString

func (s *String) SetString(val string) *String

SetString sets the value of the Number to the string value

func (*String) SetValue

func (s *String) SetValue(value *structpb.Value) error

SetValue sets a new value

func (String) String

func (s String) String() string

String returns the value of Number as an int32

func (String) StringValue

func (s String) StringValue() *structpb.Value_StringValue

StringValue returns the String as a Value_StringValue

func (String) Value

func (s String) Value() *structpb.Value

Value returns the String as a Value

type Struct

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

Struct holds a struct.Value of type StructValue

func NewStruct

func NewStruct() *Struct

NewStruct creates a new Struct

func (*Struct) Get

func (s *Struct) Get(key string) *structpb.Value

Get returns the value of key. If key is not found, the returned value is a NullValue

func (*Struct) Remove

func (s *Struct) Remove(key string)

Remove removes the item indexed by key from the Struct. If key does not exists, this does nothing

func (*Struct) Set

func (s *Struct) Set(key string, value *structpb.Value)

Set sets the value of key

func (*Struct) SetValue

func (s *Struct) SetValue(value *structpb.Value) error

SetValue sets the value of the Struct

func (*Struct) Value

func (s *Struct) Value() *structpb.Value

Value return Struct as a Value

Jump to

Keyboard shortcuts

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