structpbenc

package module
v0.0.0-...-7fa73a5 Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

README

structpbenc

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

Installation

go get "github.com/karantin2020/structpbenc"

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
package main

import (
	"fmt"

	structpb "github.com/karantin2020/structpbenc"

	pb "google.golang.org/protobuf/types/known/structpb"
)

func main() {
	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
package main

import (
	"fmt"

	structpb "github.com/karantin2020/structpbenc"

	pb "google.golang.org/protobuf/types/known/structpb"
)

func main() {
	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
package main

import (
	"fmt"

	structpb "github.com/karantin2020/structpbenc"

	pb "google.golang.org/protobuf/types/known/structpb"
)

func main() {
	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