@@ -3,6 +3,7 @@ package gapi
33import (
44 "encoding/json"
55 "fmt"
6+ "strings"
67)
78
89type LokiDerivedField struct {
@@ -176,23 +177,48 @@ func (d SecureJSONData) Map() (map[string]interface{}, error) {
176177 return fields , nil
177178}
178179
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+
179188func JSONDataWithHeaders (jsonData , secureJSONData map [string ]interface {}, headers map [string ]string ) (map [string ]interface {}, map [string ]interface {}) {
180189 // 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 )
189192
190193 idx := 1
191194 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
194197 idx += 1
195198 }
196199
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
198224}
0 commit comments