@@ -16,9 +16,10 @@ import (
1616
1717func Test_ParseJSON (t * testing.T ) {
1818 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 )
2223 }{
2324 {
2425 name : "handle string" ,
@@ -27,7 +28,7 @@ func Test_ParseJSON(t *testing.T) {
2728 return `{"test":"string value"}` , nil
2829 },
2930 },
30- want : func (expectedMap pcommon.Map ) {
31+ wantMap : func (expectedMap pcommon.Map ) {
3132 expectedMap .PutStr ("test" , "string value" )
3233 },
3334 },
@@ -38,7 +39,7 @@ func Test_ParseJSON(t *testing.T) {
3839 return `{"test":true}` , nil
3940 },
4041 },
41- want : func (expectedMap pcommon.Map ) {
42+ wantMap : func (expectedMap pcommon.Map ) {
4243 expectedMap .PutBool ("test" , true )
4344 },
4445 },
@@ -49,7 +50,7 @@ func Test_ParseJSON(t *testing.T) {
4950 return `{"test":1}` , nil
5051 },
5152 },
52- want : func (expectedMap pcommon.Map ) {
53+ wantMap : func (expectedMap pcommon.Map ) {
5354 expectedMap .PutDouble ("test" , 1 )
5455 },
5556 },
@@ -60,7 +61,7 @@ func Test_ParseJSON(t *testing.T) {
6061 return `{"test":1.1}` , nil
6162 },
6263 },
63- want : func (expectedMap pcommon.Map ) {
64+ wantMap : func (expectedMap pcommon.Map ) {
6465 expectedMap .PutDouble ("test" , 1.1 )
6566 },
6667 },
@@ -71,7 +72,7 @@ func Test_ParseJSON(t *testing.T) {
7172 return `{"test":null}` , nil
7273 },
7374 },
74- want : func (expectedMap pcommon.Map ) {
75+ wantMap : func (expectedMap pcommon.Map ) {
7576 expectedMap .PutEmpty ("test" )
7677 },
7778 },
@@ -82,20 +83,45 @@ func Test_ParseJSON(t *testing.T) {
8283 return `{"test":["string","value"]}` , nil
8384 },
8485 },
85- want : func (expectedMap pcommon.Map ) {
86+ wantMap : func (expectedMap pcommon.Map ) {
8687 emptySlice := expectedMap .PutEmptySlice ("test" )
8788 emptySlice .AppendEmpty ().SetStr ("string" )
8889 emptySlice .AppendEmpty ().SetStr ("value" )
8990 },
9091 },
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+ },
91117 {
92118 name : "handle nested object" ,
93119 target : ottl.StandardStringGetter [any ]{
94120 Getter : func (_ context.Context , _ any ) (any , error ) {
95121 return `{"test":{"nested":"true"}}` , nil
96122 },
97123 },
98- want : func (expectedMap pcommon.Map ) {
124+ wantMap : func (expectedMap pcommon.Map ) {
99125 newMap := expectedMap .PutEmptyMap ("test" )
100126 newMap .PutStr ("nested" , "true" )
101127 },
@@ -107,7 +133,7 @@ func Test_ParseJSON(t *testing.T) {
107133 return `{"existing":"pass"}` , nil
108134 },
109135 },
110- want : func (expectedMap pcommon.Map ) {
136+ wantMap : func (expectedMap pcommon.Map ) {
111137 expectedMap .PutStr ("existing" , "pass" )
112138 },
113139 },
@@ -118,7 +144,7 @@ func Test_ParseJSON(t *testing.T) {
118144 return `{"test1":{"nested":"true"},"test2":"string","test3":1,"test4":1.1,"test5":[[1], [2, 3],[]],"test6":null}` , nil
119145 },
120146 },
121- want : func (expectedMap pcommon.Map ) {
147+ wantMap : func (expectedMap pcommon.Map ) {
122148 newMap := expectedMap .PutEmptyMap ("test1" )
123149 newMap .PutStr ("nested" , "true" )
124150 expectedMap .PutStr ("test2" , "string" )
@@ -141,19 +167,26 @@ func Test_ParseJSON(t *testing.T) {
141167 result , err := exprFunc (context .Background (), nil )
142168 assert .NoError (t , err )
143169
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+ }
149189
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- })
157190 })
158191 }
159192}
0 commit comments