-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.go
400 lines (368 loc) · 11.2 KB
/
structs.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package dbus
import (
"cmp"
"fmt"
"reflect"
"slices"
"strconv"
"strings"
)
// InlineLayout marks a struct as being inlined. A struct with a field
// of type InlineLayout will be laid out in DBus messages without the
// initial 8-byte alignment that DBus structs normally enforce.
//
// By convention, InlineLayout should be used as the type of a field
// named "_", placed at the beginning of the struct type definition.
type InlineLayout struct{}
// structField is the information about a struct field that needs to
// be marshaled/unmarshaled.
type structField struct {
Name string
Index [][]int
Type reflect.Type
// VarDictFields are the key-specific fields associated with this
// structField. This structField must be a vardict map
// (map[K]any).
//
// VarDictFields is always of type map[K]*varDictField, but has to
// be a reflect.Value here because the vardict's key type is only
// known at runtime.
VarDictFields reflect.Value
}
// IsVarDict reports whether the struct field is a vardict, with
// attached associated fields.
func (f *structField) IsVarDict() bool {
return f.VarDictFields.IsValid()
}
// VarDictKeyCmp returns a comparison function for the vardict's key
// type. Panics if the field is not a vardict.
func (f *structField) VarDictKeyCmp() func(a, b reflect.Value) int {
return mapKeyCmp(f.Type.Key())
}
// VarDictField returns the varDictField information for the field
// associated with the given vardict key, or nil if there is no
// associated field.
func (f *structField) VarDictField(key reflect.Value) *varDictField {
ret := f.VarDictFields.MapIndex(key)
if ret.IsZero() {
return nil
}
return ret.Interface().(*varDictField)
}
// GetWithZero loads the struct field from structVal. If loading
// requires traversing a nil pointer into an embedded struct,
// GetWithZero returns a non-settable zero value of the field.
func (f *structField) GetWithZero(structVal reflect.Value) reflect.Value {
v := structVal
for i, hop := range f.Index {
if i > 0 {
if v.IsNil() {
return reflect.Zero(f.Type)
}
v = v.Elem()
}
v = v.FieldByIndex(hop)
}
return v
}
// GetWithAlloc loads the struct field from structVal. If loading
// requires traversing a nil pointer into an embedded struct,
// GetWithAlloc allocates zero values appropriately. The returned
// [reflect.Value] is settable.
func (f *structField) GetWithAlloc(structVal reflect.Value) reflect.Value {
v := structVal
for i, hop := range f.Index {
if i > 0 {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
v = v.FieldByIndex(hop)
}
return v
}
func (f *structField) StringGetter() func(reflect.Value) string {
if f.Type.Kind() == reflect.Pointer {
return func(structVal reflect.Value) string {
v := derefZero(f.GetWithZero(structVal))
if !v.IsValid() {
return ""
}
return v.String()
}
} else {
return func(structVal reflect.Value) string {
return f.GetWithZero(structVal).String()
}
}
}
func (f *structField) String() string {
var ret strings.Builder
kindStr := ""
if ks := f.Type.Kind().String(); ks != f.Type.String() {
kindStr = fmt.Sprintf(" (%s)", ks)
}
fmt.Fprintf(&ret, "%s: %s%s at %v", f.Name, f.Type, kindStr, f.Index)
if f.VarDictFields.IsValid() {
ret.WriteString(", vardict fields:")
ks := f.VarDictFields.MapKeys()
slices.SortFunc(ks, mapKeyCmp(f.VarDictFields.Type().Key()))
for _, k := range ks {
v := f.VarDictField(k)
encodeZero := ""
if v.EncodeZero {
encodeZero = "(encode zero) "
}
fmt.Fprintf(&ret, "\n %v: %s%s", v.StrKey, v, encodeZero)
}
}
return ret.String()
}
// varDictField describes an "associated field" of a vardict. An
// associated field stores the vardict value for a particular key with
// strong typing, as opposed to the vardict's default any values.
type varDictField struct {
*structField
Key reflect.Value
StrKey string
// EncodeZero is whether to encode zero values into the
// vardict. By default, zero values are presumed to be unset
// optional values and skipped.
EncodeZero bool
}
// structInfo is the information about a struct relevant to
// marshaling/unmarshaling.
type structInfo struct {
// Name is the struct's name, for use in diagnostics.
Name string
// Type is the struct's type.
Type reflect.Type
// NoPad, if true, specifies that the struct should be aligned
// according to the alignment of its first encoded field, instead
// of the customary 8-byte alignment.
NoPad bool
// StructFields is the information about each struct field
// eligible for DBus encoding/decoding.
StructFields []*structField
}
func (s *structInfo) Signature() string {
var ret strings.Builder
if !s.NoPad {
ret.WriteByte('(')
}
for _, f := range s.StructFields {
sig, err := signatureFor(f.Type, nil)
if err != nil {
panic(err)
}
ret.WriteString(sig.String())
}
if !s.NoPad {
ret.WriteByte(')')
}
return ret.String()
}
func (s *structInfo) String() string {
var ret strings.Builder
name, typ := s.Name, s.Type.String()
if s.Type.Kind() == reflect.Struct {
typ = "struct"
}
fmt.Fprintf(&ret, "%s: %s, fields:\n", name, typ)
for _, f := range s.StructFields {
ret.WriteString(f.String())
ret.WriteByte('\n')
}
return ret.String()
}
// getStructInfo returns the structInfo for t.
//
// getStructInfo returns an error if t is not a struct, or if the
// struct is malformed in a way that prevents its use for dbus
// messaging.
func getStructInfo(t reflect.Type) (*structInfo, error) {
if t.Kind() != reflect.Struct {
return nil, fmt.Errorf("%s is not a struct", t)
}
ret := &structInfo{
Name: t.String(),
Type: t,
}
var (
varDictMap *structField
varDictFields []*varDictField
)
for field := range structFields(t, nil) {
if field.Type == reflect.TypeFor[InlineLayout]() {
ret.NoPad = true
continue
}
if !field.IsExported() {
continue
}
encodeZero, isVardict, vardictKey := parseStructTag(field)
fieldInfo := &structField{
Name: field.Name,
Type: field.Type,
Index: allocSteps(t, field.Index),
}
if isVardict {
if !isValidVarDictMapType(fieldInfo.Type) {
return nil, fmt.Errorf("vardict map %s.%s must be a map[K]any", ret.Name, fieldInfo.Name)
}
fieldInfo.VarDictFields = reflect.MakeMap(reflect.MapOf(
fieldInfo.Type.Key(),
reflect.TypeFor[*varDictField]()))
varDictMap = fieldInfo
ret.StructFields = append(ret.StructFields, fieldInfo)
} else if vardictKey != "" {
varDictFields = append(varDictFields, &varDictField{
structField: fieldInfo,
StrKey: vardictKey,
EncodeZero: encodeZero,
})
} else {
ret.StructFields = append(ret.StructFields, fieldInfo)
}
}
if len(varDictFields) == 0 {
// Simple struct, all done.
return ret, nil
}
// Struct containing vardict fields. Validate its shape and parse
// out keys for later use.
if varDictMap == nil {
return nil, fmt.Errorf("vardict fields declared in struct %s, but no map[K]any tagged with 'vardict'", ret.Name)
}
seen := map[string]*varDictField{}
keyParser := mapKeyParser(varDictMap.Type.Key())
for _, f := range varDictFields {
v, err := keyParser(f.StrKey)
if err != nil {
return nil, fmt.Errorf("invalid key %q for vardict field %s.%s (expected type %s): %w", f.StrKey, ret.Name, f.Name, varDictMap.Type.Key(), err)
}
// Careful, v.String() only returns the underlying value if
// it's a string or implements Stringer! Other values get a
// placeholder with just the type name. fmt has special
// handling of reflect.Value to always print the underlying
// value.
canonicalKey := fmt.Sprint(v)
f.Key = v
if prev := seen[canonicalKey]; prev != nil {
if canonicalKey != f.StrKey {
return nil, fmt.Errorf("duplicate vardict key %q (canonicalized from %q) in struct %s, used by %s and %s", canonicalKey, f.StrKey, ret.Name, f.Name, prev.Name)
}
return nil, fmt.Errorf("duplicate vardict key %q for type %s", f.StrKey, ret.Name)
}
// Parsing the key can change its value (e.g. ParseBool
// coerces "true", "TRUE", "1" to bool(true)). Store the
// canonical key.
f.StrKey = canonicalKey
varDictMap.VarDictFields.SetMapIndex(f.Key, reflect.ValueOf(f))
}
return ret, nil
}
// parseStructTag returns the information contained in field's "dbus"
// struct tag.
func parseStructTag(field reflect.StructField) (encodeZero, isVardict bool, vardictKey string) {
for _, f := range strings.Split(field.Tag.Get("dbus"), ",") {
if f == "encodeZero" {
encodeZero = true
} else if f == "vardict" {
isVardict = true
} else if val, ok := strings.CutPrefix(f, "key="); ok {
if val == "@" {
vardictKey = field.Name
} else {
vardictKey = val
}
}
}
return encodeZero, isVardict, vardictKey
}
// isValidVarDictMapType reports whether t is a valid vardict type,
// i.e. a map[K]any where K is a valid dbus map key type.
func isValidVarDictMapType(t reflect.Type) bool {
return t.Kind() == reflect.Map && mapKeyKinds.Has(t.Key().Kind()) && t.Elem() == reflect.TypeFor[any]()
}
// mapKeyParser returns a function that converts strings into values
// of the given map key type.
func mapKeyParser(t reflect.Type) func(string) (reflect.Value, error) {
if !mapKeyKinds.Has(t.Kind()) {
panic("mapKeyParser called on type that can't be a map key")
}
switch t.Kind() {
case reflect.Bool:
return func(s string) (reflect.Value, error) {
b, err := strconv.ParseBool(s)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(b), nil
}
case reflect.Int16, reflect.Int32, reflect.Int64:
return func(s string) (reflect.Value, error) {
i64, err := strconv.ParseInt(s, 10, int(t.Size())*8)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(i64).Convert(t), nil
}
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return func(s string) (reflect.Value, error) {
u64, err := strconv.ParseUint(s, 10, int(t.Size())*8)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(u64).Convert(t), nil
}
case reflect.Float32, reflect.Float64:
return func(s string) (reflect.Value, error) {
f64, err := strconv.ParseFloat(s, int(t.Size())*8)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(f64).Convert(t), nil
}
case reflect.String:
return func(s string) (reflect.Value, error) {
return reflect.ValueOf(s), nil
}
default:
panic(fmt.Sprintf("invalid dbus map key type %s", t))
}
}
// mapKeyCmp returns a comparison function for the given map key type.
func mapKeyCmp(t reflect.Type) func(a, b reflect.Value) int {
switch t.Kind() {
case reflect.Bool:
return func(a, b reflect.Value) int {
if a.Bool() == b.Bool() {
return 0
}
if !a.Bool() {
return -1
}
return 1
}
case reflect.Int16, reflect.Int32, reflect.Int64:
return func(a, b reflect.Value) int {
return cmp.Compare(a.Int(), b.Int())
}
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return func(a, b reflect.Value) int {
return cmp.Compare(a.Uint(), b.Uint())
}
case reflect.Float32, reflect.Float64:
return func(a, b reflect.Value) int {
return cmp.Compare(a.Float(), b.Float())
}
case reflect.String:
return func(a, b reflect.Value) int {
return cmp.Compare(a.String(), b.String())
}
default:
panic("invalid map key type")
}
}