/
util.go
101 lines (78 loc) · 2.52 KB
/
util.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package codecutil
import (
"fmt"
"os"
"reflect"
"google.golang.org/protobuf/proto"
)
var rvZero reflect.Value
var protoMessageType = reflect.TypeOf((*proto.Message)(nil)).Elem()
type Codec interface {
Marshal(v any) ([]byte, error)
Unmarshal(data []byte, v any) error
}
// Unmarshal decodes raw data using the given codec and returns a newly
// constructed value.
func Unmarshal(c Codec, data []byte, valueType reflect.Type) (any, error) {
// The destination is a pointer (passed to Unmarshal()) to a pointer of the
// underlying value type. This way JSON's "null" is unmarshalled as nil.
dest := reflect.New(valueType)
dest.Elem().Set(reflect.New(valueType.Elem()))
if err := c.Unmarshal(data, dest.Interface()); err != nil {
return nil, err
}
return dest.Elem().Interface(), nil
}
// Internally values are always pointers to a non-pointer type.
func NormalizeValue(value any) (any, reflect.Type) {
v := reflect.ValueOf(value)
if !v.IsValid() {
return nil, nil
}
if v.Kind() == reflect.Pointer {
// Find last pointer in chain (if any).
for v.Kind() == reflect.Pointer && v.Elem().Kind() == reflect.Pointer {
v = v.Elem()
}
} else {
// Get pointer to a copy
v = reflect.New(v.Type())
v.Elem().Set(reflect.ValueOf(value))
}
return v.Interface(), v.Type()
}
func checkProtoType(rv reflect.Value) error {
if vt := rv.Type(); vt.Kind() == reflect.Struct && reflect.PointerTo(vt).Implements(protoMessageType) {
return fmt.Errorf("%w: protobuf messages must be pointers, got %s", os.ErrInvalid, vt.String())
}
return nil
}
func CheckValueType(v any) error {
if rv := reflect.ValueOf(v); rv.IsValid() {
return checkProtoType(rv)
}
return nil
}
// Validate the input value for marshalling.
func PrepareMarshalValue(v any) (reflect.Value, proto.Message, error) {
rv := reflect.ValueOf(v)
if !rv.IsValid() {
return rvZero, nil, fmt.Errorf("%w: invalid input value %#v", os.ErrInvalid, v)
} else if err := checkProtoType(rv); err != nil {
return rvZero, nil, err
}
m, _ := rv.Interface().(proto.Message)
return rv, m, nil
}
// Validate the output destination for unmarshalling. It must be a non-nil
// pointer.
func PrepareUnmarshalDest(v any) (reflect.Value, proto.Message, error) {
rv := reflect.ValueOf(v)
if rv.IsNil() || rv.Kind() != reflect.Pointer {
return rvZero, nil, fmt.Errorf("%w: v must be a non-nil pointer, got %#v", os.ErrInvalid, rv)
} else if err := checkProtoType(rv.Elem()); err != nil {
return rvZero, nil, err
}
m, _ := rv.Elem().Interface().(proto.Message)
return rv, m, nil
}