This commit is contained in:
2025-08-21 23:17:36 +02:00
parent 779d1e0a0e
commit bbc7c50fae

View File

@@ -89,7 +89,7 @@ func ProcessJSON(content string, command utils.ModifyCommand, filename string) (
return commands, fmt.Errorf("failed to convert Lua table back to Go: %v", err)
}
commands, err = applySurgicalJSONChanges(content, jsonData, goData)
commands, err = applyJSONChanges(content, jsonData, goData)
if err != nil {
processJsonLogger.Error("Failed to apply JSON changes: %v", err)
return commands, fmt.Errorf("failed to apply JSON changes: %v", err)
@@ -100,11 +100,10 @@ func ProcessJSON(content string, command utils.ModifyCommand, filename string) (
return commands, nil
}
// applySurgicalJSONChanges compares original and modified data and applies changes surgically
func applySurgicalJSONChanges(content string, originalData, modifiedData interface{}) ([]utils.ReplaceCommand, error) {
// applyJSONChanges compares original and modified data and applies changes surgically
func applyJSONChanges(content string, originalData, modifiedData interface{}) ([]utils.ReplaceCommand, error) {
var commands []utils.ReplaceCommand
// Apply surgical changes
appliedCommands, err := applyChanges(content, originalData, modifiedData)
if err == nil && len(appliedCommands) > 0 {
return appliedCommands, nil
@@ -120,6 +119,8 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
// Find all changes between original and modified data
changes := findDeepChanges("", originalData, modifiedData)
jsonLogger.Debug("applyChanges: Found %d changes: %v", len(changes), changes)
if len(changes) == 0 {
return commands, nil
}
@@ -139,9 +140,7 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
}
}
jsonLogger.Info("applyChanges: Found %d changes: %v", len(changes), changes)
jsonLogger.Info("applyChanges: %d removals, %d additions, %d value changes", len(removals), len(additions), len(valueChanges))
jsonLogger.Debug("applyChanges: %d removals, %d additions, %d value changes", len(removals), len(additions), len(valueChanges))
// Apply removals first (from end to beginning to avoid index shifting)
for _, removalPath := range removals {
@@ -171,13 +170,13 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
actualPath := strings.TrimSuffix(additionPath, "@add")
newValue := changes[additionPath]
jsonLogger.Info("Processing addition: path=%s, value=%v", actualPath, newValue)
jsonLogger.Debug("Processing addition: path=%s, value=%v", actualPath, newValue)
// Find the parent object to add the field to
parentPath := getParentPath(actualPath)
fieldName := getFieldName(actualPath)
jsonLogger.Info("Parent path: %s, field name: %s", parentPath, fieldName)
jsonLogger.Debug("Parent path: %s, field name: %s", parentPath, fieldName)
// Get the parent object
var parentResult gjson.Result
@@ -189,14 +188,14 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
}
if !parentResult.Exists() {
jsonLogger.Info("Parent path %s does not exist, skipping", parentPath)
jsonLogger.Debug("Parent path %s does not exist, skipping", parentPath)
continue
}
// Find where to insert the new field (at the end of the object)
startPos := int(parentResult.Index + len(parentResult.Raw) - 1) // Before closing brace
jsonLogger.Info("Inserting at pos %d", startPos)
jsonLogger.Debug("Inserting at pos %d", startPos)
// Convert the new value to JSON string
newValueStr := convertValueToJSONString(newValue)
@@ -204,7 +203,7 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
// Insert the new field
insertText := fmt.Sprintf(`,"%s":%s`, fieldName, newValueStr)
jsonLogger.Info("Inserting text: %q", insertText)
jsonLogger.Debug("Inserting text: %q", insertText)
commands = append(commands, utils.ReplaceCommand{
From: startPos,
@@ -212,7 +211,7 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
With: insertText,
})
jsonLogger.Info("Added addition command: From=%d, To=%d, With=%q", startPos, startPos, insertText)
jsonLogger.Debug("Added addition command: From=%d, To=%d, With=%q", startPos, startPos, insertText)
}
// Apply value changes (in reverse order to avoid position shifting)
@@ -226,12 +225,12 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
for _, path := range valueChanges {
newValue := changes[path]
jsonLogger.Info("Processing value change: path=%s, value=%v", path, newValue)
jsonLogger.Debug("Processing value change: path=%s, value=%v", path, newValue)
// Get the current value and its position in the original JSON
result := gjson.Get(content, path)
if !result.Exists() {
jsonLogger.Info("Path %s does not exist, skipping", path)
jsonLogger.Debug("Path %s does not exist, skipping", path)
continue // Skip if path doesn't exist
}
@@ -239,12 +238,12 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
startPos := result.Index
endPos := startPos + len(result.Raw)
jsonLogger.Info("Found value at pos %d-%d: %q", startPos, endPos, result.Raw)
jsonLogger.Debug("Found value at pos %d-%d: %q", startPos, endPos, result.Raw)
// Convert the new value to JSON string
newValueStr := convertValueToJSONString(newValue)
jsonLogger.Info("Converting to: %q", newValueStr)
jsonLogger.Debug("Converting to: %q", newValueStr)
// Create a replacement command for this specific value
commands = append(commands, utils.ReplaceCommand{
@@ -253,7 +252,7 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
With: newValueStr,
})
jsonLogger.Info("Added command: From=%d, To=%d, With=%q", int(startPos), int(endPos), newValueStr)
jsonLogger.Debug("Added command: From=%d, To=%d, With=%q", int(startPos), int(endPos), newValueStr)
}
return commands, nil