Enable root modifications
Though I can not see why you would want to..... But there's no reason you would not be able to
This commit is contained in:
@@ -147,6 +147,52 @@ func (p *JSONProcessor) ProcessContent(content string, pattern string, luaExpr s
|
||||
|
||||
// updateJSONValue updates a value in the JSON structure based on its JSONPath
|
||||
func (p *JSONProcessor) updateJSONValue(jsonData interface{}, path string, newValue interface{}) error {
|
||||
// Special handling for root node
|
||||
if path == "$" {
|
||||
// For the root node, we'll copy the value to the jsonData reference
|
||||
// This is a special case since we can't directly replace the interface{} variable
|
||||
|
||||
// We need to handle different types of root elements
|
||||
switch rootValue := newValue.(type) {
|
||||
case map[string]interface{}:
|
||||
// For objects, we need to copy over all keys
|
||||
rootMap, ok := jsonData.(map[string]interface{})
|
||||
if !ok {
|
||||
// If the original wasn't a map, completely replace it with the new map
|
||||
// This is handled by the jsonpath.Set function
|
||||
return jsonpath.Set(jsonData, path, newValue)
|
||||
}
|
||||
|
||||
// Clear the original map
|
||||
for k := range rootMap {
|
||||
delete(rootMap, k)
|
||||
}
|
||||
|
||||
// Copy all keys from the new map
|
||||
for k, v := range rootValue {
|
||||
rootMap[k] = v
|
||||
}
|
||||
return nil
|
||||
|
||||
case []interface{}:
|
||||
// For arrays, we need to handle similarly
|
||||
rootArray, ok := jsonData.([]interface{})
|
||||
if !ok {
|
||||
// If the original wasn't an array, use jsonpath.Set
|
||||
return jsonpath.Set(jsonData, path, newValue)
|
||||
}
|
||||
|
||||
// Clear and recreate the array
|
||||
*&rootArray = rootValue
|
||||
return nil
|
||||
|
||||
default:
|
||||
// For other types, use jsonpath.Set
|
||||
return jsonpath.Set(jsonData, path, newValue)
|
||||
}
|
||||
}
|
||||
|
||||
// For non-root paths, use the regular Set method
|
||||
err := jsonpath.Set(jsonData, path, newValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update JSON value at path '%s': %w", path, err)
|
||||
|
Reference in New Issue
Block a user