-
Notifications
You must be signed in to change notification settings - Fork 2
/
object.go
205 lines (159 loc) · 4.76 KB
/
object.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package sdktypes
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"go.autokitteh.dev/autokitteh/internal/kittehs"
akproto "go.autokitteh.dev/autokitteh/proto"
"go.autokitteh.dev/autokitteh/sdk/sdkerrors"
)
type Object interface {
json.Marshaler
fmt.Stringer
isValider
stricter
isObject()
}
type objectTraits[M interface{ proto.Message }] interface {
// Validate is used to validate all fields in the given
// message. If a field is missing, it can be ignored.
Validate(m M) error
// StrictValidate is used to validate that all mandatory
// fields are specified. It does not need to call Validate,
// the underlying object will do it.
StrictValidate(m M) error
}
type nopObjectTraits[M proto.Message] struct{}
func (nopObjectTraits[M]) Validate(m M) error { return nil }
func (nopObjectTraits[M]) StrictValidate(m M) error { return nil }
var _ objectTraits[proto.Message] = nopObjectTraits[proto.Message]{}
type comparableMessage interface {
proto.Message
comparable // for comparison to nil (proto.Message will always be a ptr).
}
type object[M comparableMessage, T objectTraits[M]] struct {
kittehs.DoNotCompare
m M
}
func clone[M proto.Message](m M) M { return proto.Clone(m).(M) }
func (o object[T, M]) isObject() {}
func (o object[M, T]) IsValid() bool { var zero M; return o.m != zero }
func (o object[M, T]) ToProto() M { return clone(o.m) }
func (o object[M, T]) Message() proto.Message { return o.ToProto() }
// the returned message will not always be the message stored in the object.
func (o *object[M, T]) read() M {
if o.IsValid() {
return o.m
}
return reflect.New(reflect.TypeOf(o.m).Elem()).Interface().(M)
}
// sets the message is nil. this mutates the object.
func (o *object[M, T]) reset() { var zero M; o.m = zero }
func (o object[M, T]) String() string {
if !o.IsValid() {
return ""
}
return string(kittehs.Must1(prototext.Marshal(o.m)))
}
// forceUpdate replaces the message without validation.
// This can be called only and only if the message is known to be valid.
func (o object[M, T]) forceUpdate(f func(M)) object[M, T] {
m := proto.Clone(o.read()).(M)
f(m)
return object[M, T]{m: m}
}
var protoMarshal = protojson.MarshalOptions{UseProtoNames: true}.Marshal
func (o object[M, T]) MarshalJSON() ([]byte, error) {
// The object can be marshalled as a pointer, so if it's null, we just
// specify null in JSON.
if !o.IsValid() {
return []byte("null"), nil
}
return protoMarshal(o.m)
}
func (o *object[M, T]) UnmarshalJSON(b []byte) (err error) {
// The object can be marshalled as a pointer, so if it's null, we reset the object.
// (ie. we got an invalid/nil object)
if string(b) == "null" {
o.reset()
return
}
o.m = o.read()
if err = protojson.Unmarshal(b, o.m); err != nil {
return
}
if err = validate[M, T](o.m); err != nil {
o.reset()
return
}
return
}
func (o object[M, T]) Strict() error {
if !o.IsValid() {
return sdkerrors.NewInvalidArgumentError("zero object")
}
var t T
return t.StrictValidate(o.m)
}
func (o object[M, T]) Hash() string { return hash(o.m) }
func (o object[M, T]) Equal(other interface{ ToProto() M }) bool {
return proto.Equal(o.m, other.ToProto())
}
func strictValidate[M proto.Message, T objectTraits[M]](m M) error {
var zero M
if proto.Equal(zero, m) {
return errors.New("empty")
}
var t T
if err := t.StrictValidate(m); err != nil {
return sdkerrors.ErrInvalidArgument{Underlying: err}
}
return validate[M, T](m)
}
func validate[M proto.Message, T objectTraits[M]](m M) error {
var zero M
if proto.Equal(zero, m) {
return nil
}
if err := akproto.Validate(m); err != nil {
return sdkerrors.ErrInvalidArgument{Underlying: err}
}
var t T
if err := t.Validate(m); err != nil {
return sdkerrors.ErrInvalidArgument{Underlying: err}
}
return nil
}
func fromProto[M comparableMessage, T objectTraits[M]](m M) (o object[M, T], err error) {
if err = validate[M, T](m); err != nil {
return
}
o = object[M, T]{m: clone(m)}
return
}
func forceFromProto[W ~struct{ object[M, T] }, M comparableMessage, T objectTraits[M]](m M) W {
var zero M
if proto.Equal(m, zero) {
return W{}
}
return W{object[M, T]{m: clone(m)}}
}
func FromProto[W ~struct{ object[M, T] }, M comparableMessage, T objectTraits[M]](m M) (w W, err error) {
var o object[M, T]
if o, err = fromProto[M, T](m); err != nil {
return
}
w = W{o}
return
}
// Use this to create a valid, but empty object.
func zeroObject[W ~struct{ object[M, T] }, M comparableMessage, T objectTraits[M]]() W {
o := object[M, T]{}
o.m = o.read()
return W{o}
}
func ToProto[O interface{ ToProto() M }, M proto.Message](o O) M { return o.ToProto() }