@@ -16,9 +16,10 @@ import (
16
16
17
17
func Test_ParseJSON (t * testing.T ) {
18
18
tests := []struct {
19
- name string
20
- target ottl.StringGetter [any ]
21
- want func (pcommon.Map )
19
+ name string
20
+ target ottl.StringGetter [any ]
21
+ wantMap func (pcommon.Map )
22
+ wantSlice func (pcommon.Slice )
22
23
}{
23
24
{
24
25
name : "handle string" ,
@@ -27,7 +28,7 @@ func Test_ParseJSON(t *testing.T) {
27
28
return `{"test":"string value"}` , nil
28
29
},
29
30
},
30
- want : func (expectedMap pcommon.Map ) {
31
+ wantMap : func (expectedMap pcommon.Map ) {
31
32
expectedMap .PutStr ("test" , "string value" )
32
33
},
33
34
},
@@ -38,7 +39,7 @@ func Test_ParseJSON(t *testing.T) {
38
39
return `{"test":true}` , nil
39
40
},
40
41
},
41
- want : func (expectedMap pcommon.Map ) {
42
+ wantMap : func (expectedMap pcommon.Map ) {
42
43
expectedMap .PutBool ("test" , true )
43
44
},
44
45
},
@@ -49,7 +50,7 @@ func Test_ParseJSON(t *testing.T) {
49
50
return `{"test":1}` , nil
50
51
},
51
52
},
52
- want : func (expectedMap pcommon.Map ) {
53
+ wantMap : func (expectedMap pcommon.Map ) {
53
54
expectedMap .PutDouble ("test" , 1 )
54
55
},
55
56
},
@@ -60,7 +61,7 @@ func Test_ParseJSON(t *testing.T) {
60
61
return `{"test":1.1}` , nil
61
62
},
62
63
},
63
- want : func (expectedMap pcommon.Map ) {
64
+ wantMap : func (expectedMap pcommon.Map ) {
64
65
expectedMap .PutDouble ("test" , 1.1 )
65
66
},
66
67
},
@@ -71,7 +72,7 @@ func Test_ParseJSON(t *testing.T) {
71
72
return `{"test":null}` , nil
72
73
},
73
74
},
74
- want : func (expectedMap pcommon.Map ) {
75
+ wantMap : func (expectedMap pcommon.Map ) {
75
76
expectedMap .PutEmpty ("test" )
76
77
},
77
78
},
@@ -82,20 +83,45 @@ func Test_ParseJSON(t *testing.T) {
82
83
return `{"test":["string","value"]}` , nil
83
84
},
84
85
},
85
- want : func (expectedMap pcommon.Map ) {
86
+ wantMap : func (expectedMap pcommon.Map ) {
86
87
emptySlice := expectedMap .PutEmptySlice ("test" )
87
88
emptySlice .AppendEmpty ().SetStr ("string" )
88
89
emptySlice .AppendEmpty ().SetStr ("value" )
89
90
},
90
91
},
92
+ {
93
+ name : "handle top level array" ,
94
+ target : ottl.StandardStringGetter [any ]{
95
+ Getter : func (_ context.Context , _ any ) (any , error ) {
96
+ return `["string","value"]` , nil
97
+ },
98
+ },
99
+ wantSlice : func (expectedSlice pcommon.Slice ) {
100
+ expectedSlice .AppendEmpty ().SetStr ("string" )
101
+ expectedSlice .AppendEmpty ().SetStr ("value" )
102
+ },
103
+ },
104
+ {
105
+ name : "handle top level array of objects" ,
106
+ target : ottl.StandardStringGetter [any ]{
107
+ Getter : func (_ context.Context , _ any ) (any , error ) {
108
+ return `[{"test":"value"},{"test":"value"}]` , nil
109
+ },
110
+ },
111
+ wantSlice : func (expectedSlice pcommon.Slice ) {
112
+
113
+ expectedSlice .AppendEmpty ().SetEmptyMap ().PutStr ("test" , "value" )
114
+ expectedSlice .AppendEmpty ().SetEmptyMap ().PutStr ("test" , "value" )
115
+ },
116
+ },
91
117
{
92
118
name : "handle nested object" ,
93
119
target : ottl.StandardStringGetter [any ]{
94
120
Getter : func (_ context.Context , _ any ) (any , error ) {
95
121
return `{"test":{"nested":"true"}}` , nil
96
122
},
97
123
},
98
- want : func (expectedMap pcommon.Map ) {
124
+ wantMap : func (expectedMap pcommon.Map ) {
99
125
newMap := expectedMap .PutEmptyMap ("test" )
100
126
newMap .PutStr ("nested" , "true" )
101
127
},
@@ -107,7 +133,7 @@ func Test_ParseJSON(t *testing.T) {
107
133
return `{"existing":"pass"}` , nil
108
134
},
109
135
},
110
- want : func (expectedMap pcommon.Map ) {
136
+ wantMap : func (expectedMap pcommon.Map ) {
111
137
expectedMap .PutStr ("existing" , "pass" )
112
138
},
113
139
},
@@ -118,7 +144,7 @@ func Test_ParseJSON(t *testing.T) {
118
144
return `{"test1":{"nested":"true"},"test2":"string","test3":1,"test4":1.1,"test5":[[1], [2, 3],[]],"test6":null}` , nil
119
145
},
120
146
},
121
- want : func (expectedMap pcommon.Map ) {
147
+ wantMap : func (expectedMap pcommon.Map ) {
122
148
newMap := expectedMap .PutEmptyMap ("test1" )
123
149
newMap .PutStr ("nested" , "true" )
124
150
expectedMap .PutStr ("test2" , "string" )
@@ -141,19 +167,26 @@ func Test_ParseJSON(t *testing.T) {
141
167
result , err := exprFunc (context .Background (), nil )
142
168
assert .NoError (t , err )
143
169
144
- resultMap , ok := result .(pcommon.Map )
145
- require .True (t , ok )
146
-
147
- expected := pcommon .NewMap ()
148
- tt .want (expected )
170
+ if tt .wantMap != nil {
171
+ resultMap , ok := result .(pcommon.Map )
172
+ require .True (t , ok )
173
+ expected := pcommon .NewMap ()
174
+ tt .wantMap (expected )
175
+ assert .Equal (t , expected .Len (), resultMap .Len ())
176
+ expected .Range (func (k string , _ pcommon.Value ) bool {
177
+ ev , _ := expected .Get (k )
178
+ av , _ := resultMap .Get (k )
179
+ assert .Equal (t , ev , av )
180
+ return true
181
+ })
182
+ } else if tt .wantSlice != nil {
183
+ resultSlice , ok := result .(pcommon.Slice )
184
+ require .True (t , ok )
185
+ expected := pcommon .NewSlice ()
186
+ tt .wantSlice (expected )
187
+ assert .Equal (t , expected , resultSlice )
188
+ }
149
189
150
- assert .Equal (t , expected .Len (), resultMap .Len ())
151
- expected .Range (func (k string , _ pcommon.Value ) bool {
152
- ev , _ := expected .Get (k )
153
- av , _ := resultMap .Get (k )
154
- assert .Equal (t , ev , av )
155
- return true
156
- })
157
190
})
158
191
}
159
192
}
0 commit comments