File: field_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.44.133-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 245,296 kB
  • sloc: makefile: 120
file content (128 lines) | stat: -rw-r--r-- 2,514 bytes parent folder | download | duplicates (3)
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
package dynamodbattribute

import (
	"reflect"
	"testing"
)

type testUnionValues struct {
	Name  string
	Value interface{}
}

type unionSimple struct {
	A int
	B string
	C []string
}

type unionComplex struct {
	unionSimple
	A int
}

type unionTagged struct {
	A int `json:"A"`
}

type unionTaggedComplex struct {
	unionSimple
	unionTagged
	B string
}

func TestUnionStructFields(t *testing.T) {
	var cases = []struct {
		in     interface{}
		expect []testUnionValues
	}{
		{
			in: unionSimple{1, "2", []string{"abc"}},
			expect: []testUnionValues{
				{"A", 1},
				{"B", "2"},
				{"C", []string{"abc"}},
			},
		},
		{
			in: unionComplex{
				unionSimple: unionSimple{1, "2", []string{"abc"}},
				A:           2,
			},
			expect: []testUnionValues{
				{"B", "2"},
				{"C", []string{"abc"}},
				{"A", 2},
			},
		},
		{
			in: unionTaggedComplex{
				unionSimple: unionSimple{1, "2", []string{"abc"}},
				unionTagged: unionTagged{3},
				B:           "3",
			},
			expect: []testUnionValues{
				{"C", []string{"abc"}},
				{"A", 3},
				{"B", "3"},
			},
		},
	}

	for i, c := range cases {
		v := reflect.ValueOf(c.in)

		fields := unionStructFields(v.Type(), MarshalOptions{SupportJSONTags: true})
		for j, f := range fields.All() {
			expected := c.expect[j]
			if e, a := expected.Name, f.Name; e != a {
				t.Errorf("%d:%d expect %v, got %v", i, j, e, f)
			}
			actual := v.FieldByIndex(f.Index).Interface()
			if e, a := expected.Value, actual; !reflect.DeepEqual(e, a) {
				t.Errorf("%d:%d expect %v, got %v", i, j, e, f)
			}
		}
	}
}

func TestCachedFields(t *testing.T) {
	type myStruct struct {
		Dog  int
		CAT  string
		bird bool
	}

	fields := unionStructFields(reflect.TypeOf(myStruct{}), MarshalOptions{})

	const expectedNumFields = 2
	if numFields := len(fields.All()); numFields != expectedNumFields {
		t.Errorf("expected number of fields to be %d but got %d", expectedNumFields, numFields)
	}

	cases := []struct {
		Name      string
		FieldName string
		Found     bool
	}{
		{"Dog", "Dog", true},
		{"dog", "Dog", true},
		{"DOG", "Dog", true},
		{"Yorkie", "", false},
		{"Cat", "CAT", true},
		{"cat", "CAT", true},
		{"CAT", "CAT", true},
		{"tiger", "", false},
		{"bird", "", false},
	}

	for _, c := range cases {
		f, found := fields.FieldByName(c.Name)
		if found != c.Found {
			t.Errorf("expected found to be %v but got %v", c.Found, found)
		}
		if found && f.Name != c.FieldName {
			t.Errorf("expected field name to be %s but got %s", c.FieldName, f.Name)
		}
	}
}