-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
49 lines (43 loc) · 948 Bytes
/
map.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
43
44
45
46
47
48
49
// Copyright 2021, Justen Walker
// SPDX-License-Identifier: Apache-2.0
package value
import (
"go.justen.tech/goodwill/internal/pb"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
type Map map[string]interface{}
func (v Map) Type() string {
return "map"
}
func (v *Map) unmarshalAny(any *anypb.Any) error {
var message pb.MapValue
if err := any.UnmarshalTo(&message); err != nil {
return err
}
result := make(map[string]interface{})
for key, any := range message.Value {
value, err := unmarshalAny(any.Value)
if err != nil {
return err
}
result[key] = value
}
*v = result
return nil
}
func (v Map) marshalMessage() (proto.Message, error) {
mapAny := make(map[string]*pb.Value)
for key, val := range v {
any, err := marshalInterface(val)
if err != nil {
return nil, err
}
mapAny[key] = &pb.Value{
Value: any,
}
}
return &pb.MapValue{
Value: mapAny,
}, nil
}