MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestStructProto

Function TestStructProto

pkg/goproto/struct_test.go:43–129  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

41}
42
43func TestStructProto(t *testing.T) {
44 a := assertions.New(t)
45
46 ptr := "ptr"
47 m := map[string]any{
48 "foo": "bar",
49 "ptr": &ptr,
50 "answer": 42,
51 "answer.precise": 42.0,
52 "works": true,
53 "empty": nil,
54 "list": []string{"a", "b", "c"},
55 "map": map[string]string{"foo": "bar"},
56 "eui": types.EUI64{1, 2, 3, 4, 5, 6, 7, 8},
57 "jsonMarshaler": &jsonMarshaler{Text: "testtext"},
58 }
59 s, err := goproto.Struct(m)
60 a.So(err, should.BeNil)
61 sm, err := goproto.Map(s)
62 a.So(err, should.BeNil)
63 for k, v := range m {
64 a.So(s.Fields, should.ContainKey, k)
65 a.So(sm, should.ContainKey, k)
66 if v == nil {
67 a.So(s.Fields[k].Kind, should.HaveSameTypeAs, &structpb.Value_NullValue{})
68 a.So(sm[k], should.BeNil)
69 continue
70 }
71
72 rv := reflect.Indirect(reflect.ValueOf(v))
73
74 switch kind := rv.Kind(); kind {
75 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
76 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
77 reflect.Float32, reflect.Float64:
78
79 var vt float64
80 a.So(s.Fields[k].Kind, should.HaveSameTypeAs, &structpb.Value_NumberValue{})
81 a.So(sm[k], should.HaveSameTypeAs, vt)
82 a.So(sm[k], should.Equal, cast.ToFloat64(rv.Interface()))
83
84 case reflect.Bool:
85 var vt bool
86 a.So(s.Fields[k].Kind, should.HaveSameTypeAs, &structpb.Value_BoolValue{})
87 a.So(sm[k], should.HaveSameTypeAs, vt)
88 a.So(sm[k], should.Equal, rv.Bool())
89
90 case reflect.String:
91 var vt string
92 a.So(s.Fields[k].Kind, should.HaveSameTypeAs, &structpb.Value_StringValue{})
93 a.So(sm[k], should.HaveSameTypeAs, vt)
94 a.So(sm[k], should.Equal, rv.String())
95
96 case reflect.Slice, reflect.Array:
97 var vt []any
98 a.So(s.Fields[k].Kind, should.HaveSameTypeAs, &structpb.Value_ListValue{})
99 a.So(sm[k], should.HaveSameTypeAs, vt)
100 if a.So(sm[k], should.HaveLength, rv.Len()) {

Callers

nothing calls this directly

Calls 7

StructFunction · 0.92
MapFunction · 0.92
ValueFunction · 0.92
InterfaceFunction · 0.92
NewMethod · 0.65
StringMethod · 0.65
LenMethod · 0.45

Tested by

no test coverage detected