-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_test.go
More file actions
193 lines (159 loc) · 5.27 KB
/
Copy pathreference_test.go
File metadata and controls
193 lines (159 loc) · 5.27 KB
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
package jsonlog
import (
"strings"
"testing"
"github.com/NextronSystems/jsonlog/jsonpointer"
"github.com/stretchr/testify/assert"
)
type testObject struct {
ObjectHeader
Recursive *Reference
Substruct struct {
SubField1 string `json:"subfield1" textlog:"subfield1"`
} `json:"substruct" textlog:"substruct,expand"`
// nolint:unused // not used, just used to check that unexported fields are not included in the event
unexported string
AnonymousSubstruct
Nested NestedSubstruct `json:"nested" textlog:"nested,expand"`
Unexpanded UnexpandedSubstruct `json:"unexpanded" textlog:"unexpanded"`
Subfield5 string `json:"subfield5" textlog:"subfield5"`
Resolver TestResolver `json:"resolver" textlog:"resolver"`
SubObject *SubObject `json:"subobject" textlog:"subobject,expand"`
Slice []SliceSubstruct `json:"slice" textlog:"slice,expand"`
Map map[string]*MapSubstruct `json:"map" textlog:"map,expand"`
}
type AnonymousSubstruct struct {
SubField2 string `json:"subfield2" textlog:"subfield2"`
}
type NestedSubstruct struct {
Substruct struct {
SubField3 string `json:"subfield3" textlog:"subfield3"`
} `json:"substruct" textlog:",expand"`
}
type UnexpandedSubstruct struct {
SubField4 string `json:"subfield4" textlog:"subfield4"`
}
func (u UnexpandedSubstruct) String() string {
return u.SubField4
}
type TestResolver struct {
Subfield6 string `json:"subfield6"`
Subfield7 string `json:"subfield7"`
Ignore string
}
func (t TestResolver) String() string {
return strings.Join([]string{t.Subfield6, t.Subfield7}, ", ")
}
var _ TextReferenceResolver = (*TestResolver)(nil)
func (t *TestResolver) RelativeTextPointer(pointee any) (string, bool, bool) {
virtual := true // We do not implement TextlogMarshaler
if pointee == &t.Subfield6 {
return "subfield6", virtual, true
}
if pointee == &t.Subfield7 {
return "subfield7", virtual, true
}
return "", false, false
}
var _ JsonReferenceResolver = (*TestResolver)(nil)
func (t *TestResolver) RelativeJsonPointer(pointee any) jsonpointer.Pointer {
if pointee == &t.Subfield6 {
return jsonpointer.New("subfield6")
}
if pointee == &t.Subfield7 {
return jsonpointer.New("subfield7")
}
return nil
}
type SubObject struct {
ObjectHeader
Subfield8 string `json:"subfield8" textlog:"subfield8"`
}
type SliceSubstruct struct {
Subfield9 string `json:"subfield9" textlog:"subfield9"`
}
type MapSubstruct struct {
Subfield10 string `json:"subfield10" textlog:"subfield10"`
}
func (m MapSubstruct) String() string {
return m.Subfield10
}
func makeTestObject() testObject {
var test testObject
test.Substruct.SubField1 = "subfield1"
test.SubField2 = "subfield2"
test.Nested.Substruct.SubField3 = "subfield3"
test.Unexpanded.SubField4 = "subfield4"
test.Subfield5 = "subfield5"
test.Resolver.Subfield6 = "subfield6"
test.Resolver.Subfield7 = "subfield7"
test.SubObject = &SubObject{Subfield8: "subfield8"}
test.Recursive = NewReference(&test, &test.Substruct)
test.Slice = []SliceSubstruct{{Subfield9: "slice"}}
test.Map = map[string]*MapSubstruct{mapKey: {Subfield10: "map"}}
return test
}
const mapKey = "key"
func TestReference_ToJsonPointer(t *testing.T) {
test := makeTestObject()
var tests = []struct {
PointedField any
want string
}{
{&test.Substruct, "/substruct"},
{&test.Substruct.SubField1, "/substruct/subfield1"},
{&test.SubField2, "/subfield2"},
{&test.Nested, "/nested"},
{&test.Nested.Substruct, "/nested/substruct"},
{&test.Nested.Substruct.SubField3, "/nested/substruct/subfield3"},
{&test.Unexpanded, "/unexpanded"},
{&test.Unexpanded.SubField4, "/unexpanded/subfield4"},
{&test.Subfield5, "/subfield5"},
{&test.Resolver, "/resolver"},
{&test.Resolver.Subfield6, "/resolver/subfield6"},
{&test.Resolver.Subfield7, "/resolver/subfield7"},
{&test.SubObject, "/subobject"},
{&test.SubObject.Subfield8, "/subobject/subfield8"},
{&test.Slice[0].Subfield9, "/slice/0/subfield9"},
{&test.Map[mapKey].Subfield10, "/map/key/subfield10"},
// test.Recursive not required here: if there is a flaw in the pointer
// search logic, it will panic when encountering this cycle.
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
ref := NewReference(&test, tt.PointedField)
assert.Equal(t, tt.want, ref.ToJsonPointer().String())
})
}
}
func TestReference_ToTextPointer(t *testing.T) {
test := makeTestObject()
var tests = []struct {
PointedField any
want string
}{
{&test.Substruct, "SUBSTRUCT"},
{&test.Substruct.SubField1, "SUBSTRUCT_SUBFIELD1"},
{&test.SubField2, "SUBFIELD2"},
{&test.Nested, "NESTED"},
{&test.Nested.Substruct, "NESTED"},
{&test.Nested.Substruct.SubField3, "NESTED_SUBFIELD3"},
{&test.Unexpanded, "UNEXPANDED"},
{&test.Unexpanded.SubField4, ""},
{&test.Subfield5, "SUBFIELD5"},
{&test.Resolver, "RESOLVER"},
{&test.Resolver.Subfield6, "subfield6"},
{&test.Resolver.Subfield7, "subfield7"},
{&test.SubObject, "SUBOBJECT"},
{&test.SubObject.Subfield8, "SUBOBJECT_SUBFIELD8"},
{&test.Slice[0].Subfield9, "SLICE_SUBFIELD9_1"},
// Map values aren't expanded and their subfields therefore aren't reachable in textlog
{&test.Map[mapKey].Subfield10, ""},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
ref := NewReference(&test, tt.PointedField)
assert.Equal(t, tt.want, ref.ToTextLabel())
})
}
}