@@ -70,6 +70,9 @@ func getResourceKey(m map[string]interface{}, ks ...string) (val interface{}, er
70
70
var obj map [string ]interface {}
71
71
for i := range items {
72
72
if index == int64 (i ) {
73
+ if strValue , ok := items [i ].(string ); ok {
74
+ return strValue , nil
75
+ }
73
76
if obj , ok = items [i ].(map [string ]interface {}); ! ok {
74
77
return nil , fmt .Errorf ("malformed structure at provided index: %d" , i )
75
78
}
@@ -92,34 +95,72 @@ func getResourceKey(m map[string]interface{}, ks ...string) (val interface{}, er
92
95
}
93
96
}
94
97
95
- func mapQueryResponseInputKey (m map [string ]interface {}, value , prev string , parentKeys []string ) (key string , ok bool ) {
96
- for k , v := range m {
97
- var jsonV string
98
- if str , isString := v .(string ); ! isString {
99
- bytes , err := json .Marshal (v )
100
- if err != nil {
101
- return
98
+ func mapQueryResponseInputKey (m interface {}, value , prev string , parentKeys []string ) (key string , ok bool ) {
99
+ if mapObj , isMap := m .(map [string ]interface {}); isMap {
100
+ for k , v := range mapObj {
101
+ var jsonV string
102
+ // Check if v is a string, and if not, marshal it as a json string for comparison
103
+ if str , isString := v .(string ); ! isString {
104
+ bytes , err := json .Marshal (v )
105
+ if err != nil {
106
+ return
107
+ }
108
+ jsonV = string (bytes )
109
+ } else {
110
+ jsonV = str
102
111
}
103
- jsonV = string (bytes )
104
- } else {
105
- jsonV = str
106
- }
107
112
108
- if jsonV == value {
109
- if len (parentKeys ) != 0 {
110
- newPrev := parentKeys [len (parentKeys )- 1 ]
111
- if newPrev != prev {
112
- parentKeys = parentKeys [:len (parentKeys )- 1 ]
113
+ // If jsonV and input value are the same, we have found our match.
114
+ if strings .Compare (jsonV , value ) == 0 || jsonV == value {
115
+ if len (parentKeys ) != 0 && ! strings .Contains (parentKeys [len (parentKeys )- 1 ], "[" ) {
116
+ newPrev := parentKeys [len (parentKeys )- 1 ]
117
+ if newPrev != prev {
118
+ parentKeys = parentKeys [:len (parentKeys )- 1 ]
119
+ }
113
120
}
114
- }
115
121
116
- parentKeys = append (parentKeys , k )
117
- key = strings .Join (parentKeys , "." )
118
- ok = true
119
- return
120
- } else if nv , isMap := v .(map [string ]interface {}); isMap {
121
- parentKeys = append (parentKeys , k )
122
- key , ok = mapQueryResponseInputKey (nv , value , k , parentKeys )
122
+ parentKeys = append (parentKeys , k )
123
+ key = strings .Join (parentKeys , "." )
124
+ ok = true
125
+ return
126
+ } else if slice , isSlice := v .([]interface {}); isSlice { // If v is a slice, we need to loop over items in the slice
127
+ // key, ok, parentKeys = handleSlice(slice, value, parentKeys, prev)
128
+
129
+ for i , iv := range slice {
130
+ // if the items in the slice are strings, we can simply compare its values
131
+ if innerStr , valueIsString := iv .(string ); valueIsString {
132
+ if innerStr == value {
133
+ if len (parentKeys ) != 0 && ! strings .Contains (parentKeys [len (parentKeys )- 1 ], "[" ) {
134
+ newPrev := parentKeys [len (parentKeys )- 1 ]
135
+ if newPrev != prev {
136
+ parentKeys = parentKeys [:len (parentKeys )- 1 ]
137
+ }
138
+ }
139
+ parentKeys = append (parentKeys , fmt .Sprintf ("%s[%d]" , k , i ))
140
+ key = strings .Join (parentKeys , "." )
141
+ ok = true
142
+ return
143
+ }
144
+ } else if innerObj , isObject := iv .(map [string ]interface {}); isObject { // If v is an object, traverse the object futher
145
+ parentKeys = append (parentKeys , fmt .Sprintf ("%s[%d]" , k , i ))
146
+ key , ok = mapQueryResponseInputKey (innerObj , value , k , parentKeys )
147
+ if ! ok {
148
+ parentKeys = parentKeys [:len (parentKeys )- 1 ]
149
+ continue
150
+ }
151
+ return
152
+ } else {
153
+ parentKeys = parentKeys [:len (parentKeys )- 1 ]
154
+ continue
155
+ }
156
+ }
157
+ } else if nv , isMap := v .(map [string ]interface {}); isMap {
158
+ parentKeys = append (parentKeys , k )
159
+ key , ok = mapQueryResponseInputKey (nv , value , k , parentKeys )
160
+ if ! ok {
161
+ continue
162
+ }
163
+ }
123
164
}
124
165
}
125
166
return
0 commit comments