@@ -3,6 +3,7 @@ package gapi
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "strings"
6
7
)
7
8
8
9
type LokiDerivedField struct {
@@ -176,23 +177,48 @@ func (d SecureJSONData) Map() (map[string]interface{}, error) {
176
177
return fields , nil
177
178
}
178
179
180
+ func cloneMap (m map [string ]interface {}) map [string ]interface {} {
181
+ clone := make (map [string ]interface {})
182
+ for k , v := range m {
183
+ clone [k ] = v
184
+ }
185
+ return clone
186
+ }
187
+
179
188
func JSONDataWithHeaders (jsonData , secureJSONData map [string ]interface {}, headers map [string ]string ) (map [string ]interface {}, map [string ]interface {}) {
180
189
// Clone the maps so we don't modify the original
181
- clonedJSONData := make (map [string ]interface {}, len (jsonData ))
182
- for k , v := range jsonData {
183
- clonedJSONData [k ] = v
184
- }
185
- clonedSecureJSONData := make (map [string ]interface {}, len (secureJSONData ))
186
- for k , v := range secureJSONData {
187
- clonedSecureJSONData [k ] = v
188
- }
190
+ jsonData = cloneMap (jsonData )
191
+ secureJSONData = cloneMap (secureJSONData )
189
192
190
193
idx := 1
191
194
for name , value := range headers {
192
- clonedJSONData [fmt .Sprintf ("httpHeaderName%d" , idx )] = name
193
- clonedSecureJSONData [fmt .Sprintf ("httpHeaderValue%d" , idx )] = value
195
+ jsonData [fmt .Sprintf ("httpHeaderName%d" , idx )] = name
196
+ secureJSONData [fmt .Sprintf ("httpHeaderValue%d" , idx )] = value
194
197
idx += 1
195
198
}
196
199
197
- return clonedJSONData , clonedSecureJSONData
200
+ return jsonData , secureJSONData
201
+ }
202
+
203
+ func ExtractHeadersFromJSONData (jsonData , secureJSONData map [string ]interface {}) (map [string ]interface {}, map [string ]interface {}, map [string ]string ) {
204
+ // Clone the maps so we don't modify the original
205
+ jsonData = cloneMap (jsonData )
206
+ secureJSONData = cloneMap (secureJSONData )
207
+ headers := make (map [string ]string )
208
+
209
+ for dataName , dataValue := range jsonData {
210
+ if strings .HasPrefix (dataName , "httpHeaderName" ) {
211
+ // Remove the header name from JSON data
212
+ delete (jsonData , dataName )
213
+
214
+ // Remove the header value from secure JSON data
215
+ secureDataName := strings .Replace (dataName , "httpHeaderName" , "httpHeaderValue" , 1 )
216
+ delete (secureJSONData , secureDataName )
217
+
218
+ headerName := dataValue .(string )
219
+ headers [headerName ] = "true" // We can't retrieve the headers, so we just set a dummy value
220
+ }
221
+ }
222
+
223
+ return jsonData , secureJSONData , headers
198
224
}
0 commit comments