fix:moveValue bug
This commit is contained in:
@@ -198,7 +198,7 @@ func applyOperations(jsonStr string, operations []ParamOperation) (string, error
|
|||||||
}
|
}
|
||||||
result, err = sjson.Set(result, op.Path, op.Value)
|
result, err = sjson.Set(result, op.Path, op.Value)
|
||||||
case "move":
|
case "move":
|
||||||
result, err = moveValue(result, op.From, op.To, op.KeepOrigin)
|
result, err = moveValue(result, op.From, op.To)
|
||||||
case "prepend":
|
case "prepend":
|
||||||
result, err = modifyValue(result, op.Path, op.Value, op.KeepOrigin, true)
|
result, err = modifyValue(result, op.Path, op.Value, op.KeepOrigin, true)
|
||||||
case "append":
|
case "append":
|
||||||
@@ -213,14 +213,11 @@ func applyOperations(jsonStr string, operations []ParamOperation) (string, error
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func moveValue(jsonStr, fromPath, toPath string, keepOrigin bool) (string, error) {
|
func moveValue(jsonStr, fromPath, toPath string) (string, error) {
|
||||||
sourceValue := gjson.Get(jsonStr, fromPath)
|
sourceValue := gjson.Get(jsonStr, fromPath)
|
||||||
if !sourceValue.Exists() {
|
if !sourceValue.Exists() {
|
||||||
return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
|
return jsonStr, fmt.Errorf("source path does not exist: %s", fromPath)
|
||||||
}
|
}
|
||||||
if keepOrigin && gjson.Get(jsonStr, toPath).Exists() {
|
|
||||||
return sjson.Delete(jsonStr, fromPath)
|
|
||||||
}
|
|
||||||
result, err := sjson.Set(jsonStr, toPath, sourceValue.Value())
|
result, err := sjson.Set(jsonStr, toPath, sourceValue.Value())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
Reference in New Issue
Block a user