structpb

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

structpb-enc

Encode map (or struct) to protobuf Struct, and decode from protobuf Struct to map

Installation

go get "github.com/kei2100/structpb-enc"

Examples

Encode to protobuf Struct
func ExampleEncode() {
	src := map[string]interface{}{
		"str": "str",
		"int": 1,
		"map": map[string]interface{}{
			"nested_str": "nested_str",
			"nested_int": 10,
		},
		"struct": struct {
			Field string
		}{
			Field: "Field",
		},
		"slice": []string{"one"},
		"nil":   interface{}(nil),
	}

	var pbst pb.Struct
	structpb.Encode(src, &pbst)

	fmt.Println(
		pbst.Fields["str"].GetStringValue(),
		pbst.Fields["int"].GetNumberValue(),
		pbst.Fields["map"].GetStructValue().Fields["nested_str"].GetStringValue(),
		pbst.Fields["map"].GetStructValue().Fields["nested_int"].GetNumberValue(),
		pbst.Fields["struct"].GetStructValue().Fields["Field"].GetStringValue(),
		pbst.Fields["slice"].GetListValue().Values[0].GetStringValue(),
	)

	_, ok := pbst.Fields["nil"].GetKind().(*pb.Value_NullValue)
	fmt.Println(
		ok,
	)

	// Output:
	// str 1 nested_str 10 Field one
	// true
}

func ExampleEncodeFromStruct() {
	src := struct {
		StrField   string
		IntField   int
		unexported string
	}{
		StrField:   "str",
		IntField:   10,
		unexported: "unexported",
	}

	var pbst pb.Struct
	structpb.EncodeFromStruct(src, &pbst)

	fmt.Println(
		pbst.Fields["StrField"].GetStringValue(),
		pbst.Fields["IntField"].GetNumberValue(),
		pbst.Fields["unexported"],
	)

	// Output:
	// str 10 <nil>
}
Decode to map
func ExampleDecode() {
	src := &pb.Struct{
		Fields: map[string]*pb.Value{
			"null":   {Kind: &pb.Value_NullValue{}},
			"number": {Kind: &pb.Value_NumberValue{NumberValue: 10}},
			"str":    {Kind: &pb.Value_StringValue{StringValue: "str"}},
			"bool":   {Kind: &pb.Value_BoolValue{BoolValue: true}},
			"struct": {Kind: &pb.Value_StructValue{StructValue: &pb.Struct{
				Fields: map[string]*pb.Value{
					"nested": {Kind: &pb.Value_StringValue{StringValue: "nested"}},
				},
			}}},
			"slice": {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{
				Values: []*pb.Value{
					{Kind: &pb.Value_StringValue{StringValue: "one"}},
					{Kind: &pb.Value_StringValue{StringValue: "two"}},
				},
			}}},
		},
	}

	dest := structpb.Decode(src)

	fmt.Println(
		dest["null"],
		dest["number"],
		dest["str"],
		dest["bool"],
	)
	if nested, ok := dest["struct"].(map[string]interface{}); ok {
		fmt.Println(nested["nested"])
	}
	if slice, ok := dest["slice"].([]interface{}); ok {
		fmt.Println(slice[0], slice[1])
	}

	// Output:
	// <nil> 10 str true
	// nested
	// one two
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(pbst *pb.Struct) map[string]interface{}

Decode decodes pb.Struct to map[string]interface{}

Example
src := &pb.Struct{
	Fields: map[string]*pb.Value{
		"null":   {Kind: &pb.Value_NullValue{}},
		"number": {Kind: &pb.Value_NumberValue{NumberValue: 10}},
		"str":    {Kind: &pb.Value_StringValue{StringValue: "str"}},
		"bool":   {Kind: &pb.Value_BoolValue{BoolValue: true}},
		"struct": {Kind: &pb.Value_StructValue{StructValue: &pb.Struct{
			Fields: map[string]*pb.Value{
				"nested": {Kind: &pb.Value_StringValue{StringValue: "nested"}},
			},
		}}},
		"slice": {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{
			Values: []*pb.Value{
				{Kind: &pb.Value_StringValue{StringValue: "one"}},
				{Kind: &pb.Value_StringValue{StringValue: "two"}},
			},
		}}},
	},
}

dest := structpb.Decode(src)

fmt.Println(
	dest["null"],
	dest["number"],
	dest["str"],
	dest["bool"],
)
if nested, ok := dest["struct"].(map[string]interface{}); ok {
	fmt.Println(nested["nested"])
}
if slice, ok := dest["slice"].([]interface{}); ok {
	fmt.Println(slice[0], slice[1])
}
Output:

<nil> 10 str true
nested
one two

func DecodeValue

func DecodeValue(pbv *pb.Value) interface{}

DecodeValue decodes pb.Value to interface{}

func Encode

func Encode(src map[string]interface{}, dest *pb.Struct) error

Encode encodes map[string]interface{} to pb.Struct

Example
src := map[string]interface{}{
	"str": "str",
	"int": 1,
	"map": map[string]interface{}{
		"nested_str": "nested_str",
		"nested_int": 10,
	},
	"struct": struct {
		Field string
	}{
		Field: "Field",
	},
	"slice": []string{"one"},
	"nil":   interface{}(nil),
}

var pbst pb.Struct
structpb.Encode(src, &pbst)

fmt.Println(
	pbst.Fields["str"].GetStringValue(),
	pbst.Fields["int"].GetNumberValue(),
	pbst.Fields["map"].GetStructValue().Fields["nested_str"].GetStringValue(),
	pbst.Fields["map"].GetStructValue().Fields["nested_int"].GetNumberValue(),
	pbst.Fields["struct"].GetStructValue().Fields["Field"].GetStringValue(),
	pbst.Fields["slice"].GetListValue().Values[0].GetStringValue(),
)

_, ok := pbst.Fields["nil"].GetKind().(*pb.Value_NullValue)
fmt.Println(
	ok,
)
Output:

str 1 nested_str 10 Field one
true

func EncodeFromStruct

func EncodeFromStruct(srcStruct interface{}, dest *pb.Struct) error

EncodeFromStruct encodes an interface of struct to pb.Struct

Example
src := struct {
	StrField   string
	IntField   int
	unexported string
}{
	StrField:   "str",
	IntField:   10,
	unexported: "unexported",
}

var pbst pb.Struct
structpb.EncodeFromStruct(src, &pbst)

fmt.Println(
	pbst.Fields["StrField"].GetStringValue(),
	pbst.Fields["IntField"].GetNumberValue(),
	pbst.Fields["unexported"],
)
Output:

str 10 <nil>

func EncodeValue

func EncodeValue(src interface{}, dest *pb.Value) error

EncodeValue encodes src value to pb.Struct

Types

This section is empty.

Jump to

Keyboard shortcuts

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