fix:moveValue bug

This commit is contained in:
Nekohy
2025-08-16 11:58:40 +08:00
parent dd497d5bd8
commit b591b4ebdf

View File

@@ -198,7 +198,7 @@ func applyOperations(jsonStr string, operations []ParamOperation) (string, error
}
result, err = sjson.Set(result, op.Path, op.Value)
case "move":
result, err = moveValue(result, op.From, op.To, op.KeepOrigin)
result, err = moveValue(result, op.From, op.To)
case "prepend":
result, err = modifyValue(result, op.Path, op.Value, op.KeepOrigin, true)
case "append":
@@ -213,14 +213,11 @@ func applyOperations(jsonStr string, operations []ParamOperation) (string, error
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)
if !sourceValue.Exists() {
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())
if err != nil {
return "", err