/
protobuf.go
42 lines (37 loc) · 923 Bytes
/
protobuf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package protoutil
import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)
func NewListFromStrings(s []string) *structpb.ListValue {
values := make([]*structpb.Value, len(s))
for i := range s {
values[i] = structpb.NewStringValue(s[i])
}
return &structpb.ListValue{Values: values}
}
func AsStringList(src *structpb.ListValue) []string {
if src == nil {
return nil
}
out := make([]string, len(src.Values))
for i := range src.Values {
out[i] = src.Values[i].GetStringValue()
}
return out
}
func StructMarshal(v map[string]interface{}) ([]byte, error) {
s, err := structpb.NewStruct(v)
if err != nil {
return nil, err
}
return proto.Marshal(s)
}
func StructUnMarshal(b []byte) (map[string]interface{}, error) {
s, _ := structpb.NewStruct(map[string]interface{}{})
err := proto.Unmarshal(b, s)
if err != nil {
return nil, err
}
return s.AsMap(), nil
}