-
Notifications
You must be signed in to change notification settings - Fork 2
/
value_collections.go
206 lines (150 loc) · 4.86 KB
/
value_collections.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
206
package sdktypes
import (
"fmt"
"slices"
"sort"
"go.autokitteh.dev/autokitteh/internal/kittehs"
valuev1 "go.autokitteh.dev/autokitteh/proto/gen/go/autokitteh/values/v1"
)
type BytesValuePB = valuev1.Bytes
type BytesValue struct {
object[*BytesValuePB, nopObjectTraits[*BytesValuePB]]
}
func (BytesValue) isConcreteValue() {}
func (s BytesValue) Value() []byte { return clone(s.m).V }
func NewBytesValue(v []byte) Value {
return forceFromProto[Value](clone(&ValuePB{Bytes: &BytesValuePB{V: v}}))
}
func (v Value) IsBytes() bool { return v.read().Bytes != nil }
func (v Value) GetBytes() BytesValue { return forceFromProto[BytesValue](v.read().Bytes) }
func init() {
registerValueGetter(func(v Value) concreteValue {
if v.IsBytes() {
return v.GetBytes()
}
return nil
})
}
// ---
type ListValuePB = valuev1.List
type listValueTraits struct{}
func (listValueTraits) Validate(m *ListValuePB) error { return valuesSlice(m.Vs) }
func (listValueTraits) StrictValidate(m *ListValuePB) error { return nil }
var _ objectTraits[*ListValuePB] = listValueTraits{}
type ListValue struct {
object[*ListValuePB, listValueTraits]
}
func (ListValue) isConcreteValue() {}
func NewListValue(vs []Value) (Value, error) {
return FromProto[Value](&ValuePB{List: &ListValuePB{Vs: kittehs.Transform(vs, ToProto)}})
}
func (l ListValue) Values() []Value { return kittehs.Transform(l.read().Vs, forceFromProto[Value]) }
func (v Value) IsList() bool { return v.read().List != nil }
func (v Value) GetList() ListValue { return forceFromProto[ListValue](v.read().List) }
func init() {
registerValueGetter(func(v Value) concreteValue {
if v.IsList() {
return v.GetList()
}
return nil
})
}
// ---
type SetValuePB = valuev1.Set
type setValueTraits struct{}
func (setValueTraits) Validate(m *SetValuePB) error { return valuesSlice(m.Vs) }
func (setValueTraits) StrictValidate(m *SetValuePB) error { return nil }
var _ objectTraits[*SetValuePB] = setValueTraits{}
type SetValue struct {
object[*SetValuePB, setValueTraits]
}
func (SetValue) isConcreteValue() {}
func NewSetValue(vs []Value) (Value, error) {
vs = slices.Clone(vs)
// Make item order deterministic.
sort.Slice(vs, func(i, j int) bool { return vs[i].Hash() < vs[j].Hash() })
return FromProto[Value](&ValuePB{Set: &SetValuePB{Vs: kittehs.Transform(vs, ToProto)}})
}
func (l SetValue) Values() []Value { return kittehs.Transform(l.read().Vs, forceFromProto[Value]) }
func (v Value) IsSet() bool { return v.read().Set != nil }
func (v Value) GetSet() SetValue { return forceFromProto[SetValue](v.read().Set) }
func init() {
registerValueGetter(func(v Value) concreteValue {
if v.IsSet() {
return v.GetSet()
}
return nil
})
}
// ---
type (
DictValuePB = valuev1.Dict
dictItemPB = valuev1.Dict_Item
)
type dictValueTraits struct{}
func (dictValueTraits) Validate(m *DictValuePB) error {
keys := make(map[string]*ValuePB, len(m.Items))
for _, i := range m.Items {
hash := hash(i.K)
if keys[hash] != nil {
return fmt.Errorf("duplicate key %q", i.K)
}
if err := validateValue(i.K); err != nil {
return fmt.Errorf("key %q: %w", i.K, err)
}
keys[hash] = i.K
if err := validateValue(i.V); err != nil {
return fmt.Errorf("value for key %q: %w", i.K, err)
}
}
return nil
}
func (dictValueTraits) StrictValidate(m *DictValuePB) error { return nil }
var _ objectTraits[*DictValuePB] = dictValueTraits{}
type DictValue struct {
object[*DictValuePB, dictValueTraits]
}
type DictItem struct{ K, V Value }
func (DictValue) isConcreteValue() {}
func (d DictValue) Items() []DictItem {
return kittehs.Transform(d.read().Items, func(i *dictItemPB) DictItem {
return DictItem{K: forceFromProto[Value](i.K), V: forceFromProto[Value](i.V)}
})
}
func (d DictValue) ToStringValuesMap() (map[string]Value, error) {
return kittehs.ListToMapError(d.Items(), func(i DictItem) (string, Value, error) {
s, err := i.K.ToString()
if err != nil {
return "", Value{}, err
}
return s, i.V, nil
})
}
func NewDictValue(items []DictItem) (Value, error) {
items = slices.Clone(items)
// Make item order deterministic.
sort.SliceStable(items, func(i, j int) bool { return items[i].K.Hash() < items[j].K.Hash() })
return FromProto[Value](&ValuePB{Dict: &DictValuePB{
Items: kittehs.Transform(items, func(i DictItem) *dictItemPB {
return &dictItemPB{K: ToProto(i.K), V: ToProto(i.V)}
}),
}})
}
func NewDictValueFromStringMap(m map[string]Value) Value {
return kittehs.Must1(NewDictValue(
kittehs.TransformMapToList(
m,
func(k string, v Value) DictItem { return DictItem{K: NewStringValue(k), V: v} },
),
))
}
func (v Value) IsDict() bool { return v.read().Dict != nil }
func (v Value) GetDict() DictValue { return forceFromProto[DictValue](v.read().Dict) }
func init() {
registerValueGetter(func(v Value) concreteValue {
if v.IsDict() {
return v.GetDict()
}
return nil
})
}