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:
2025-03-25 18:57:32 +01:00
parent aba10267d1
commit 4640281fbf
3 changed files with 140 additions and 19 deletions

View File

@@ -138,10 +138,6 @@ func Set(data interface{}, path string, value interface{}) error {
return fmt.Errorf("failed to parse JSONPath %q: %w", path, err)
}
if len(steps) <= 1 {
return fmt.Errorf("cannot set root node; the provided path %q is invalid", path)
}
success := false
err = setWithPath(data, steps, &success, value, "$", ModifyFirstMode)
if err != nil {
@@ -157,10 +153,6 @@ func SetAll(data interface{}, path string, value interface{}) error {
return fmt.Errorf("failed to parse JSONPath %q: %w", path, err)
}
// if len(steps) <= 1 {
// return fmt.Errorf("cannot set root node; the provided path %q is invalid", path)
// }
success := false
err = setWithPath(data, steps, &success, value, "$", ModifyAllMode)
if err != nil {
@@ -178,17 +170,20 @@ func setWithPath(node interface{}, steps []JSONStep, success *bool, value interf
// Skip root step
actualSteps := steps
if len(steps) > 0 && steps[0].Type == RootStep {
// if len(steps) == 1 {
// return fmt.Errorf("cannot set root node; the provided path %q is invalid", currentPath)
// }
actualSteps = steps[1:]
}
// Process the first step
// if len(actualSteps) == 0 {
// return fmt.Errorf("cannot set root node; no steps provided for path %q", currentPath)
// }
// If we have no steps left, we're setting the root value
if len(actualSteps) == 0 {
// For the root node, we need to handle it differently depending on what's passed in
// since we can't directly replace the interface{} variable
// We'll signal success and let the JSONProcessor handle updating the root
*success = true
return nil
}
// Process the first step
step := actualSteps[0]
remainingSteps := actualSteps[1:]
isLastStep := len(remainingSteps) == 0