Fix some go linter warnings
This commit is contained in:
4
go.mod
4
go.mod
@@ -12,7 +12,6 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.5.0 // indirect
|
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/hexops/valast v1.5.0 // indirect
|
github.com/hexops/valast v1.5.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
@@ -22,7 +21,6 @@ require (
|
|||||||
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
||||||
github.com/spf13/cobra v1.10.1 // indirect
|
|
||||||
github.com/spf13/pflag v1.0.9 // indirect
|
github.com/spf13/pflag v1.0.9 // indirect
|
||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
github.com/tidwall/pretty v1.2.0 // indirect
|
github.com/tidwall/pretty v1.2.0 // indirect
|
||||||
@@ -35,7 +33,9 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/BurntSushi/toml v1.5.0
|
||||||
github.com/google/go-cmp v0.6.0
|
github.com/google/go-cmp v0.6.0
|
||||||
|
github.com/spf13/cobra v1.10.1
|
||||||
github.com/tidwall/gjson v1.18.0
|
github.com/tidwall/gjson v1.18.0
|
||||||
gorm.io/driver/sqlite v1.6.0
|
gorm.io/driver/sqlite v1.6.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ END`
|
|||||||
|
|
||||||
// Run the isolate commands
|
// Run the isolate commands
|
||||||
result, err := RunIsolateCommands(association, "test.txt", testContent, false)
|
result, err := RunIsolateCommands(association, "test.txt", testContent, false)
|
||||||
if err != nil && err != NothingToDo {
|
if err != nil && err != ErrNothingToDo {
|
||||||
t.Fatalf("Failed to run isolate commands: %v", err)
|
t.Fatalf("Failed to run isolate commands: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ END_SECTION2`
|
|||||||
|
|
||||||
// Run the isolate commands
|
// Run the isolate commands
|
||||||
result, err := RunIsolateCommands(associations["test.txt"], "test.txt", testContent, false)
|
result, err := RunIsolateCommands(associations["test.txt"], "test.txt", testContent, false)
|
||||||
if err != nil && err != NothingToDo {
|
if err != nil && err != ErrNothingToDo {
|
||||||
t.Fatalf("Failed to run isolate commands: %v", err)
|
t.Fatalf("Failed to run isolate commands: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,7 +235,7 @@ func TestIsolateCommandsWithJSONMode(t *testing.T) {
|
|||||||
|
|
||||||
// Run the isolate commands
|
// Run the isolate commands
|
||||||
result, err := RunIsolateCommands(associations["test.json"], "test.json", testContent, false)
|
result, err := RunIsolateCommands(associations["test.json"], "test.json", testContent, false)
|
||||||
if err != nil && err != NothingToDo {
|
if err != nil && err != ErrNothingToDo {
|
||||||
t.Fatalf("Failed to run isolate commands: %v", err)
|
t.Fatalf("Failed to run isolate commands: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -310,7 +310,7 @@ END_REGULAR`
|
|||||||
|
|
||||||
// First run isolate commands
|
// First run isolate commands
|
||||||
isolateResult, err := RunIsolateCommands(association, "test.txt", testContent, false)
|
isolateResult, err := RunIsolateCommands(association, "test.txt", testContent, false)
|
||||||
if err != nil && err != NothingToDo {
|
if err != nil && err != ErrNothingToDo {
|
||||||
t.Fatalf("Failed to run isolate commands: %v", err)
|
t.Fatalf("Failed to run isolate commands: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,7 +321,7 @@ END_REGULAR`
|
|||||||
// Then run regular commands
|
// Then run regular commands
|
||||||
commandLoggers := make(map[string]*logger.Logger)
|
commandLoggers := make(map[string]*logger.Logger)
|
||||||
finalResult, err := RunOtherCommands("test.txt", isolateResult, association, commandLoggers, false)
|
finalResult, err := RunOtherCommands("test.txt", isolateResult, association, commandLoggers, false)
|
||||||
if err != nil && err != NothingToDo {
|
if err != nil && err != ErrNothingToDo {
|
||||||
t.Fatalf("Failed to run regular commands: %v", err)
|
t.Fatalf("Failed to run regular commands: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,7 +398,7 @@ irons_spellbooks:chain_lightning
|
|||||||
|
|
||||||
// Run the isolate commands
|
// Run the isolate commands
|
||||||
result, err := RunIsolateCommands(association, "irons_spellbooks-server.toml", testContent, false)
|
result, err := RunIsolateCommands(association, "irons_spellbooks-server.toml", testContent, false)
|
||||||
if err != nil && err != NothingToDo {
|
if err != nil && err != ErrNothingToDo {
|
||||||
t.Fatalf("Failed to run isolate commands: %v", err)
|
t.Fatalf("Failed to run isolate commands: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
main.go
14
main.go
@@ -340,23 +340,23 @@ func runModifier(args []string, cmd *cobra.Command) {
|
|||||||
isChanged := false
|
isChanged := false
|
||||||
mainLogger.Debug("Running isolate commands for file %q", file)
|
mainLogger.Debug("Running isolate commands for file %q", file)
|
||||||
fileDataStr, err = RunIsolateCommands(association, file, fileDataStr, jsonFlag)
|
fileDataStr, err = RunIsolateCommands(association, file, fileDataStr, jsonFlag)
|
||||||
if err != nil && err != NothingToDo {
|
if err != nil && err != ErrNothingToDo {
|
||||||
mainLogger.Error("Failed to run isolate commands for file %q: %v", file, err)
|
mainLogger.Error("Failed to run isolate commands for file %q: %v", file, err)
|
||||||
atomic.AddInt64(&stats.FailedFiles, 1)
|
atomic.AddInt64(&stats.FailedFiles, 1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != NothingToDo {
|
if err != ErrNothingToDo {
|
||||||
isChanged = true
|
isChanged = true
|
||||||
}
|
}
|
||||||
|
|
||||||
mainLogger.Debug("Running other commands for file %q", file)
|
mainLogger.Debug("Running other commands for file %q", file)
|
||||||
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, commandLoggers, jsonFlag)
|
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, commandLoggers, jsonFlag)
|
||||||
if err != nil && err != NothingToDo {
|
if err != nil && err != ErrNothingToDo {
|
||||||
mainLogger.Error("Failed to run other commands for file %q: %v", file, err)
|
mainLogger.Error("Failed to run other commands for file %q: %v", file, err)
|
||||||
atomic.AddInt64(&stats.FailedFiles, 1)
|
atomic.AddInt64(&stats.FailedFiles, 1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != NothingToDo {
|
if err != ErrNothingToDo {
|
||||||
isChanged = true
|
isChanged = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -474,7 +474,7 @@ func CreateExampleConfig() {
|
|||||||
createExampleConfigLogger.Info("Wrote example_cook.toml")
|
createExampleConfigLogger.Info("Wrote example_cook.toml")
|
||||||
}
|
}
|
||||||
|
|
||||||
var NothingToDo = errors.New("nothing to do")
|
var ErrNothingToDo = errors.New("nothing to do")
|
||||||
|
|
||||||
func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, commandLoggers map[string]*logger.Logger, jsonFlag bool) (string, error) {
|
func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, commandLoggers map[string]*logger.Logger, jsonFlag bool) (string, error) {
|
||||||
runOtherCommandsLogger := mainLogger.WithPrefix("RunOtherCommands").WithField("file", file)
|
runOtherCommandsLogger := mainLogger.WithPrefix("RunOtherCommands").WithField("file", file)
|
||||||
@@ -565,7 +565,7 @@ func RunOtherCommands(file string, fileDataStr string, association utils.FileCom
|
|||||||
|
|
||||||
if len(modifications) == 0 {
|
if len(modifications) == 0 {
|
||||||
runOtherCommandsLogger.Warning("No modifications found for file")
|
runOtherCommandsLogger.Warning("No modifications found for file")
|
||||||
return fileDataStr, NothingToDo
|
return fileDataStr, ErrNothingToDo
|
||||||
}
|
}
|
||||||
runOtherCommandsLogger.Debug("Executing %d modifications for file", len(modifications))
|
runOtherCommandsLogger.Debug("Executing %d modifications for file", len(modifications))
|
||||||
|
|
||||||
@@ -663,7 +663,7 @@ func RunIsolateCommands(association utils.FileCommandAssociation, file string, f
|
|||||||
}
|
}
|
||||||
if !anythingDone {
|
if !anythingDone {
|
||||||
runIsolateCommandsLogger.Debug("No isolate modifications were made for file")
|
runIsolateCommandsLogger.Debug("No isolate modifications were made for file")
|
||||||
return fileDataStr, NothingToDo
|
return fileDataStr, ErrNothingToDo
|
||||||
}
|
}
|
||||||
return currentFileData, nil
|
return currentFileData, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// Package processor provides JSON processing and Lua script execution capabilities
|
||||||
|
// for data transformation and manipulation.
|
||||||
package processor
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -19,9 +21,9 @@ var jsonLogger = logger.Default.WithPrefix("processor/json")
|
|||||||
|
|
||||||
// ProcessJSON applies Lua processing to JSON content
|
// ProcessJSON applies Lua processing to JSON content
|
||||||
func ProcessJSON(content string, command utils.ModifyCommand, filename string) ([]utils.ReplaceCommand, error) {
|
func ProcessJSON(content string, command utils.ModifyCommand, filename string) ([]utils.ReplaceCommand, error) {
|
||||||
processJsonLogger := jsonLogger.WithPrefix("ProcessJSON").WithField("commandName", command.Name).WithField("file", filename)
|
processJSONLogger := jsonLogger.WithPrefix("ProcessJSON").WithField("commandName", command.Name).WithField("file", filename)
|
||||||
processJsonLogger.Debug("Starting JSON processing for file")
|
processJSONLogger.Debug("Starting JSON processing for file")
|
||||||
processJsonLogger.Trace("Initial file content length: %d", len(content))
|
processJSONLogger.Trace("Initial file content length: %d", len(content))
|
||||||
|
|
||||||
var commands []utils.ReplaceCommand
|
var commands []utils.ReplaceCommand
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
@@ -30,15 +32,15 @@ func ProcessJSON(content string, command utils.ModifyCommand, filename string) (
|
|||||||
var jsonData interface{}
|
var jsonData interface{}
|
||||||
err := json.Unmarshal([]byte(content), &jsonData)
|
err := json.Unmarshal([]byte(content), &jsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
processJsonLogger.Error("Failed to parse JSON content: %v", err)
|
processJSONLogger.Error("Failed to parse JSON content: %v", err)
|
||||||
return commands, fmt.Errorf("failed to parse JSON: %v", err)
|
return commands, fmt.Errorf("failed to parse JSON: %v", err)
|
||||||
}
|
}
|
||||||
processJsonLogger.Debug("Successfully parsed JSON content")
|
processJSONLogger.Debug("Successfully parsed JSON content")
|
||||||
|
|
||||||
// Create Lua state
|
// Create Lua state
|
||||||
L, err := NewLuaState()
|
L, err := NewLuaState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
processJsonLogger.Error("Error creating Lua state: %v", err)
|
processJSONLogger.Error("Error creating Lua state: %v", err)
|
||||||
return commands, fmt.Errorf("error creating Lua state: %v", err)
|
return commands, fmt.Errorf("error creating Lua state: %v", err)
|
||||||
}
|
}
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
@@ -49,70 +51,58 @@ func ProcessJSON(content string, command utils.ModifyCommand, filename string) (
|
|||||||
// Convert JSON data to Lua table
|
// Convert JSON data to Lua table
|
||||||
luaTable, err := ToLuaTable(L, jsonData)
|
luaTable, err := ToLuaTable(L, jsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
processJsonLogger.Error("Failed to convert JSON to Lua table: %v", err)
|
processJSONLogger.Error("Failed to convert JSON to Lua table: %v", err)
|
||||||
return commands, fmt.Errorf("failed to convert JSON to Lua table: %v", err)
|
return commands, fmt.Errorf("failed to convert JSON to Lua table: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the JSON data as a global variable
|
// Set the JSON data as a global variable
|
||||||
L.SetGlobal("data", luaTable)
|
L.SetGlobal("data", luaTable)
|
||||||
processJsonLogger.Debug("Set JSON data as Lua global 'data'")
|
processJSONLogger.Debug("Set JSON data as Lua global 'data'")
|
||||||
|
|
||||||
// Build and execute Lua script for JSON mode
|
// Build and execute Lua script for JSON mode
|
||||||
luaExpr := BuildJSONLuaScript(command.Lua)
|
luaExpr := BuildJSONLuaScript(command.Lua)
|
||||||
processJsonLogger.Debug("Built Lua script from expression: %q", command.Lua)
|
processJSONLogger.Debug("Built Lua script from expression: %q", command.Lua)
|
||||||
processJsonLogger.Trace("Full Lua script: %q", utils.LimitString(luaExpr, 200))
|
processJSONLogger.Trace("Full Lua script: %q", utils.LimitString(luaExpr, 200))
|
||||||
|
|
||||||
if err := L.DoString(luaExpr); err != nil {
|
if err := L.DoString(luaExpr); err != nil {
|
||||||
processJsonLogger.Error("Lua script execution failed: %v\nScript: %s", err, utils.LimitString(luaExpr, 200))
|
processJSONLogger.Error("Lua script execution failed: %v\nScript: %s", err, utils.LimitString(luaExpr, 200))
|
||||||
return commands, fmt.Errorf("lua script execution failed: %v", err)
|
return commands, fmt.Errorf("lua script execution failed: %v", err)
|
||||||
}
|
}
|
||||||
processJsonLogger.Debug("Lua script executed successfully")
|
processJSONLogger.Debug("Lua script executed successfully")
|
||||||
|
|
||||||
// Check if modification flag is set
|
// Check if modification flag is set
|
||||||
modifiedVal := L.GetGlobal("modified")
|
modifiedVal := L.GetGlobal("modified")
|
||||||
if modifiedVal.Type() != lua.LTBool || !lua.LVAsBool(modifiedVal) {
|
if modifiedVal.Type() != lua.LTBool || !lua.LVAsBool(modifiedVal) {
|
||||||
processJsonLogger.Debug("Skipping - no modifications indicated by Lua script")
|
processJSONLogger.Debug("Skipping - no modifications indicated by Lua script")
|
||||||
return commands, nil
|
return commands, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the modified data from Lua
|
// Get the modified data from Lua
|
||||||
modifiedData := L.GetGlobal("data")
|
modifiedData := L.GetGlobal("data")
|
||||||
if modifiedData.Type() != lua.LTTable {
|
if modifiedData.Type() != lua.LTTable {
|
||||||
processJsonLogger.Error("Expected 'data' to be a table after Lua processing, got %s", modifiedData.Type().String())
|
processJSONLogger.Error("Expected 'data' to be a table after Lua processing, got %s", modifiedData.Type().String())
|
||||||
return commands, fmt.Errorf("expected 'data' to be a table after Lua processing")
|
return commands, fmt.Errorf("expected 'data' to be a table after Lua processing")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert back to Go interface
|
// Convert back to Go interface
|
||||||
goData, err := FromLua(L, modifiedData)
|
goData, err := FromLua(L, modifiedData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
processJsonLogger.Error("Failed to convert Lua table back to Go: %v", err)
|
processJSONLogger.Error("Failed to convert Lua table back to Go: %v", err)
|
||||||
return commands, fmt.Errorf("failed to convert Lua table back to Go: %v", err)
|
return commands, fmt.Errorf("failed to convert Lua table back to Go: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
processJsonLogger.Debug("About to call applyChanges with original data and modified data")
|
processJSONLogger.Debug("About to call applyChanges with original data and modified data")
|
||||||
commands, err = applyChanges(content, jsonData, goData)
|
commands, err = applyChanges(content, jsonData, goData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
processJsonLogger.Error("Failed to apply surgical 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)
|
return commands, fmt.Errorf("failed to apply surgical JSON changes: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
processJsonLogger.Debug("Total JSON processing time: %v", time.Since(startTime))
|
processJSONLogger.Debug("Total JSON processing time: %v", time.Since(startTime))
|
||||||
processJsonLogger.Debug("Generated %d total modifications", len(commands))
|
processJSONLogger.Debug("Generated %d total modifications", len(commands))
|
||||||
return commands, nil
|
return commands, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyJSONChanges compares original and modified data and applies changes surgically
|
|
||||||
func applyJSONChanges(content string, originalData, modifiedData interface{}) ([]utils.ReplaceCommand, error) {
|
|
||||||
var commands []utils.ReplaceCommand
|
|
||||||
|
|
||||||
appliedCommands, err := applyChanges(content, originalData, modifiedData)
|
|
||||||
if err == nil && len(appliedCommands) > 0 {
|
|
||||||
return appliedCommands, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return commands, fmt.Errorf("failed to make any changes to the json")
|
|
||||||
}
|
|
||||||
|
|
||||||
// applyChanges attempts to make surgical changes while preserving exact formatting
|
// applyChanges attempts to make surgical changes while preserving exact formatting
|
||||||
func applyChanges(content string, originalData, modifiedData interface{}) ([]utils.ReplaceCommand, error) {
|
func applyChanges(content string, originalData, modifiedData interface{}) ([]utils.ReplaceCommand, error) {
|
||||||
var commands []utils.ReplaceCommand
|
var commands []utils.ReplaceCommand
|
||||||
@@ -200,12 +190,10 @@ func applyChanges(content string, originalData, modifiedData interface{}) ([]uti
|
|||||||
// Convert the new value to JSON string
|
// Convert the new value to JSON string
|
||||||
newValueStr := convertValueToJSONString(newValue)
|
newValueStr := convertValueToJSONString(newValue)
|
||||||
|
|
||||||
|
|
||||||
// Insert the new field with pretty-printed formatting
|
// Insert the new field with pretty-printed formatting
|
||||||
// Format: ,"fieldName": { ... }
|
// Format: ,"fieldName": { ... }
|
||||||
insertText := fmt.Sprintf(`,"%s": %s`, fieldName, newValueStr)
|
insertText := fmt.Sprintf(`,"%s": %s`, fieldName, newValueStr)
|
||||||
|
|
||||||
|
|
||||||
commands = append(commands, utils.ReplaceCommand{
|
commands = append(commands, utils.ReplaceCommand{
|
||||||
From: startPos,
|
From: startPos,
|
||||||
To: startPos,
|
To: startPos,
|
||||||
@@ -437,8 +425,6 @@ func findDeepChanges(basePath string, original, modified interface{}) map[string
|
|||||||
}
|
}
|
||||||
changes[currentPath] = nil // Mark for removal
|
changes[currentPath] = nil // Mark for removal
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Elements added - more complex, skip for now
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Same length - check individual elements for value changes
|
// Same length - check individual elements for value changes
|
||||||
|
|||||||
@@ -229,8 +229,8 @@ func BuildLuaScript(luaExpr string) string {
|
|||||||
|
|
||||||
// BuildJSONLuaScript prepares a Lua expression for JSON mode
|
// BuildJSONLuaScript prepares a Lua expression for JSON mode
|
||||||
func BuildJSONLuaScript(luaExpr string) string {
|
func BuildJSONLuaScript(luaExpr string) string {
|
||||||
buildJsonLuaScriptLogger := processorLogger.WithPrefix("BuildJSONLuaScript").WithField("inputLuaExpr", luaExpr)
|
buildJSONLuaScriptLogger := processorLogger.WithPrefix("BuildJSONLuaScript").WithField("inputLuaExpr", luaExpr)
|
||||||
buildJsonLuaScriptLogger.Debug("Building full Lua script for JSON mode from expression")
|
buildJSONLuaScriptLogger.Debug("Building full Lua script for JSON mode from expression")
|
||||||
|
|
||||||
// Perform $var substitutions from globalVariables
|
// Perform $var substitutions from globalVariables
|
||||||
luaExpr = replaceVariables(luaExpr)
|
luaExpr = replaceVariables(luaExpr)
|
||||||
@@ -242,7 +242,7 @@ func BuildJSONLuaScript(luaExpr string) string {
|
|||||||
local res = run()
|
local res = run()
|
||||||
modified = res == nil or res
|
modified = res == nil or res
|
||||||
`, luaExpr)
|
`, luaExpr)
|
||||||
buildJsonLuaScriptLogger.Trace("Generated full JSON Lua script: %q", utils.LimitString(fullScript, 200))
|
buildJSONLuaScriptLogger.Trace("Generated full JSON Lua script: %q", utils.LimitString(fullScript, 200))
|
||||||
|
|
||||||
return fullScript
|
return fullScript
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ type CaptureGroup struct {
|
|||||||
Range [2]int
|
Range [2]int
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessContent applies regex replacement with Lua processing
|
// ProcessRegex applies regex replacement with Lua processing.
|
||||||
// The filename here exists ONLY so we can pass it to the lua environment
|
// The filename here exists ONLY so we can pass it to the lua environment.
|
||||||
// It's not used for anything else
|
// It's not used for anything else.
|
||||||
func ProcessRegex(content string, command utils.ModifyCommand, filename string) ([]utils.ReplaceCommand, error) {
|
func ProcessRegex(content string, command utils.ModifyCommand, filename string) ([]utils.ReplaceCommand, error) {
|
||||||
processRegexLogger := regexLogger.WithPrefix("ProcessRegex").WithField("commandName", command.Name).WithField("file", filename)
|
processRegexLogger := regexLogger.WithPrefix("ProcessRegex").WithField("commandName", command.Name).WithField("file", filename)
|
||||||
processRegexLogger.Debug("Starting regex processing for file")
|
processRegexLogger.Debug("Starting regex processing for file")
|
||||||
@@ -216,9 +216,6 @@ func ProcessRegex(content string, command utils.ModifyCommand, filename string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if replacement == "" {
|
if replacement == "" {
|
||||||
// Apply the modifications to the original match
|
|
||||||
replacement = matchContent
|
|
||||||
|
|
||||||
// Count groups that were actually modified
|
// Count groups that were actually modified
|
||||||
modifiedGroupsCount := 0
|
modifiedGroupsCount := 0
|
||||||
for _, capture := range updatedCaptureGroups {
|
for _, capture := range updatedCaptureGroups {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func normalizeWhitespace(s string) string {
|
|||||||
return re.ReplaceAllString(strings.TrimSpace(s), " ")
|
return re.ReplaceAllString(strings.TrimSpace(s), " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
func APIAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
||||||
command := utils.ModifyCommand{
|
command := utils.ModifyCommand{
|
||||||
Regex: regex,
|
Regex: regex,
|
||||||
Lua: lua,
|
Lua: lua,
|
||||||
@@ -79,7 +79,7 @@ func TestSimpleValueMultiplication(t *testing.T) {
|
|||||||
</item>
|
</item>
|
||||||
</config>`
|
</config>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(\d+)</value>`, "v1 = v1*1.5")
|
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(\d+)</value>`, "v1 = v1*1.5")
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
||||||
@@ -100,7 +100,7 @@ func TestShorthandNotation(t *testing.T) {
|
|||||||
</item>
|
</item>
|
||||||
</config>`
|
</config>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(\d+)</value>`, "v1*1.5")
|
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(\d+)</value>`, "v1*1.5")
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
||||||
@@ -121,7 +121,7 @@ func TestShorthandNotationFloats(t *testing.T) {
|
|||||||
</item>
|
</item>
|
||||||
</config>`
|
</config>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(\d+\.\d+)</value>`, "v1*1.5")
|
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(\d+\.\d+)</value>`, "v1*1.5")
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
||||||
@@ -146,7 +146,7 @@ func TestArrayNotation(t *testing.T) {
|
|||||||
</prices>
|
</prices>
|
||||||
</config>`
|
</config>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `(?s)<price>(\d+)</price>`, "v1*2")
|
result, mods, matches, err := APIAdaptor(content, `(?s)<price>(\d+)</price>`, "v1*2")
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches)
|
assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches)
|
||||||
@@ -167,7 +167,7 @@ func TestMultipleNumericMatches(t *testing.T) {
|
|||||||
<entry>400</entry>
|
<entry>400</entry>
|
||||||
</data>`
|
</data>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `<entry>(\d+)</entry>`, "v1*2")
|
result, mods, matches, err := APIAdaptor(content, `<entry>(\d+)</entry>`, "v1*2")
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches)
|
assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches)
|
||||||
@@ -186,7 +186,7 @@ func TestMultipleStringMatches(t *testing.T) {
|
|||||||
<name>Mary_modified</name>
|
<name>Mary_modified</name>
|
||||||
</data>`
|
</data>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `<name>([A-Za-z]+)</name>`, `s1 = s1 .. "_modified"`)
|
result, mods, matches, err := APIAdaptor(content, `<name>([A-Za-z]+)</name>`, `s1 = s1 .. "_modified"`)
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
|
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
|
||||||
@@ -205,7 +205,7 @@ func TestStringUpperCase(t *testing.T) {
|
|||||||
<user>MARY</user>
|
<user>MARY</user>
|
||||||
</users>`
|
</users>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `<user>([A-Za-z]+)</user>`, `s1 = string.upper(s1)`)
|
result, mods, matches, err := APIAdaptor(content, `<user>([A-Za-z]+)</user>`, `s1 = string.upper(s1)`)
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
|
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
|
||||||
@@ -224,7 +224,7 @@ func TestStringConcatenation(t *testing.T) {
|
|||||||
<product>Banana_fruit</product>
|
<product>Banana_fruit</product>
|
||||||
</products>`
|
</products>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `<product>([A-Za-z]+)</product>`, `s1 = s1 .. "_fruit"`)
|
result, mods, matches, err := APIAdaptor(content, `<product>([A-Za-z]+)</product>`, `s1 = s1 .. "_fruit"`)
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
|
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
|
||||||
@@ -254,7 +254,7 @@ func TestDecimalValues(t *testing.T) {
|
|||||||
regex := regexp.MustCompile(`(?s)<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>`)
|
regex := regexp.MustCompile(`(?s)<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>`)
|
||||||
luaExpr := BuildLuaScript("v1 = v1 * v2")
|
luaExpr := BuildLuaScript("v1 = v1 * v2")
|
||||||
|
|
||||||
result, _, _, err := ApiAdaptor(content, regex.String(), luaExpr)
|
result, _, _, err := APIAdaptor(content, regex.String(), luaExpr)
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
|
|
||||||
normalizedModified := normalizeWhitespace(result)
|
normalizedModified := normalizeWhitespace(result)
|
||||||
@@ -282,7 +282,7 @@ func TestLuaMathFunctions(t *testing.T) {
|
|||||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||||
luaExpr := BuildLuaScript("v1 = math.sqrt(v1)")
|
luaExpr := BuildLuaScript("v1 = math.sqrt(v1)")
|
||||||
|
|
||||||
modifiedContent, _, _, err := ApiAdaptor(content, regex.String(), luaExpr)
|
modifiedContent, _, _, err := APIAdaptor(content, regex.String(), luaExpr)
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
|
|
||||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||||
@@ -310,7 +310,7 @@ func TestDirectAssignment(t *testing.T) {
|
|||||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||||
luaExpr := BuildLuaScript("=0")
|
luaExpr := BuildLuaScript("=0")
|
||||||
|
|
||||||
modifiedContent, _, _, err := ApiAdaptor(content, regex.String(), luaExpr)
|
modifiedContent, _, _, err := APIAdaptor(content, regex.String(), luaExpr)
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
|
|
||||||
normalizedModified := normalizeWhitespace(modifiedContent)
|
normalizedModified := normalizeWhitespace(modifiedContent)
|
||||||
@@ -369,7 +369,7 @@ func TestStringAndNumericOperations(t *testing.T) {
|
|||||||
luaExpr := BuildLuaScript(tt.luaExpression)
|
luaExpr := BuildLuaScript(tt.luaExpression)
|
||||||
|
|
||||||
// Process with our function
|
// Process with our function
|
||||||
result, modCount, _, err := ApiAdaptor(tt.input, pattern, luaExpr)
|
result, modCount, _, err := APIAdaptor(tt.input, pattern, luaExpr)
|
||||||
assert.NoError(t, err, "Process function failed: %v", err)
|
assert.NoError(t, err, "Process function failed: %v", err)
|
||||||
|
|
||||||
// Check results
|
// Check results
|
||||||
@@ -430,7 +430,7 @@ func TestEdgeCases(t *testing.T) {
|
|||||||
luaExpr := BuildLuaScript(tt.luaExpression)
|
luaExpr := BuildLuaScript(tt.luaExpression)
|
||||||
|
|
||||||
// Process with our function
|
// Process with our function
|
||||||
result, modCount, _, err := ApiAdaptor(tt.input, pattern, luaExpr)
|
result, modCount, _, err := APIAdaptor(tt.input, pattern, luaExpr)
|
||||||
assert.NoError(t, err, "Process function failed: %v", err)
|
assert.NoError(t, err, "Process function failed: %v", err)
|
||||||
|
|
||||||
// Check results
|
// Check results
|
||||||
@@ -453,7 +453,7 @@ func TestNamedCaptureGroups(t *testing.T) {
|
|||||||
</item>
|
</item>
|
||||||
</config>`
|
</config>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(?<amount>\d+)</value>`, "amount = amount * 2")
|
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(?<amount>\d+)</value>`, "amount = amount * 2")
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
||||||
@@ -474,7 +474,7 @@ func TestNamedCaptureGroupsNum(t *testing.T) {
|
|||||||
</item>
|
</item>
|
||||||
</config>`
|
</config>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(?<amount>!num)</value>`, "amount = amount * 2")
|
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(?<amount>!num)</value>`, "amount = amount * 2")
|
||||||
|
|
||||||
assert.NoError(t, err, "Error processing content: %v", err)
|
assert.NoError(t, err, "Error processing content: %v", err)
|
||||||
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
|
||||||
@@ -495,7 +495,7 @@ func TestMultipleNamedCaptureGroups(t *testing.T) {
|
|||||||
<quantity>15</quantity>
|
<quantity>15</quantity>
|
||||||
</product>`
|
</product>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`(?s)<name>(?<prodName>[^<]+)</name>.*?<price>(?<prodPrice>\d+\.\d+)</price>.*?<quantity>(?<prodQty>\d+)</quantity>`,
|
`(?s)<name>(?<prodName>[^<]+)</name>.*?<price>(?<prodPrice>\d+\.\d+)</price>.*?<quantity>(?<prodQty>\d+)</quantity>`,
|
||||||
`prodName = string.upper(prodName)
|
`prodName = string.upper(prodName)
|
||||||
prodPrice = round(prodPrice + 8, 2)
|
prodPrice = round(prodPrice + 8, 2)
|
||||||
@@ -518,7 +518,7 @@ func TestMixedIndexedAndNamedCaptures(t *testing.T) {
|
|||||||
<data>VALUE</data>
|
<data>VALUE</data>
|
||||||
</entry>`
|
</entry>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`(?s)<id>(\d+)</id>.*?<data>(?<dataField>[^<]+)</data>`,
|
`(?s)<id>(\d+)</id>.*?<data>(?<dataField>[^<]+)</data>`,
|
||||||
`v1 = v1 * 2
|
`v1 = v1 * 2
|
||||||
dataField = string.upper(dataField)`)
|
dataField = string.upper(dataField)`)
|
||||||
@@ -550,7 +550,7 @@ func TestComplexNestedNamedCaptures(t *testing.T) {
|
|||||||
</contact>
|
</contact>
|
||||||
</person>`
|
</person>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`(?s)<details>.*?<name>(?<fullName>[^<]+)</name>.*?<age>(?<age>\d+)</age>`,
|
`(?s)<details>.*?<name>(?<fullName>[^<]+)</name>.*?<age>(?<age>\d+)</age>`,
|
||||||
`fullName = string.upper(fullName) .. " (" .. age .. ")"`)
|
`fullName = string.upper(fullName) .. " (" .. age .. ")"`)
|
||||||
|
|
||||||
@@ -571,7 +571,7 @@ func TestNamedCaptureWithVariableReadback(t *testing.T) {
|
|||||||
<mana>300</mana>
|
<mana>300</mana>
|
||||||
</stats>`
|
</stats>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`(?s)<health>(?<hp>\d+)</health>.*?<mana>(?<mp>\d+)</mana>`,
|
`(?s)<health>(?<hp>\d+)</health>.*?<mana>(?<mp>\d+)</mana>`,
|
||||||
`hp = hp * 1.5
|
`hp = hp * 1.5
|
||||||
mp = mp * 1.5`)
|
mp = mp * 1.5`)
|
||||||
@@ -587,7 +587,7 @@ func TestNamedCaptureWithSpecialCharsInName(t *testing.T) {
|
|||||||
|
|
||||||
expected := `<data value="84" min="10" max="100" />`
|
expected := `<data value="84" min="10" max="100" />`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<data value="(?<val_1>\d+)"`,
|
`<data value="(?<val_1>\d+)"`,
|
||||||
`val_1 = val_1 * 2`)
|
`val_1 = val_1 * 2`)
|
||||||
|
|
||||||
@@ -602,7 +602,7 @@ func TestEmptyNamedCapture(t *testing.T) {
|
|||||||
|
|
||||||
expected := `<tag attr="default" />`
|
expected := `<tag attr="default" />`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`attr="(?<value>.*?)"`,
|
`attr="(?<value>.*?)"`,
|
||||||
`value = value == "" and "default" or value`)
|
`value = value == "" and "default" or value`)
|
||||||
|
|
||||||
@@ -617,7 +617,7 @@ func TestMultipleNamedCapturesInSameLine(t *testing.T) {
|
|||||||
|
|
||||||
expected := `<rect x="20" y="40" width="200" height="100" />`
|
expected := `<rect x="20" y="40" width="200" height="100" />`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`x="(?<x>\d+)" y="(?<y>\d+)" width="(?<w>\d+)" height="(?<h>\d+)"`,
|
`x="(?<x>\d+)" y="(?<y>\d+)" width="(?<w>\d+)" height="(?<h>\d+)"`,
|
||||||
`x = x * 2
|
`x = x * 2
|
||||||
y = y * 2
|
y = y * 2
|
||||||
@@ -641,7 +641,7 @@ func TestConditionalNamedCapture(t *testing.T) {
|
|||||||
<item status="inactive" count="10" />
|
<item status="inactive" count="10" />
|
||||||
`
|
`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<item status="(?<status>[^"]+)" count="(?<count>\d+)"`,
|
`<item status="(?<status>[^"]+)" count="(?<count>\d+)"`,
|
||||||
`count = status == "active" and count * 2 or count`)
|
`count = status == "active" and count * 2 or count`)
|
||||||
|
|
||||||
@@ -662,7 +662,7 @@ func TestLuaFunctionsOnNamedCaptures(t *testing.T) {
|
|||||||
<user name="JANE SMITH" role="admin" />
|
<user name="JANE SMITH" role="admin" />
|
||||||
`
|
`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<user name="(?<name>[^"]+)" role="(?<role>[^"]+)"`,
|
`<user name="(?<name>[^"]+)" role="(?<role>[^"]+)"`,
|
||||||
`-- Capitalize first letters for regular users
|
`-- Capitalize first letters for regular users
|
||||||
if role == "user" then
|
if role == "user" then
|
||||||
@@ -692,7 +692,7 @@ func TestNamedCaptureWithMath(t *testing.T) {
|
|||||||
<item price="19.99" quantity="3" total="59.97" />
|
<item price="19.99" quantity="3" total="59.97" />
|
||||||
`
|
`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<item price="(?<price>\d+\.\d+)" quantity="(?<qty>\d+)"!any$`,
|
`<item price="(?<price>\d+\.\d+)" quantity="(?<qty>\d+)"!any$`,
|
||||||
`-- Calculate and add total
|
`-- Calculate and add total
|
||||||
replacement = string.format('<item price="%s" quantity="%s" total="%.2f" />',
|
replacement = string.format('<item price="%s" quantity="%s" total="%.2f" />',
|
||||||
@@ -712,7 +712,7 @@ func TestNamedCaptureWithGlobals(t *testing.T) {
|
|||||||
|
|
||||||
expected := `<temp unit="F">77</temp>`
|
expected := `<temp unit="F">77</temp>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<temp unit="(?<unit>[CF]?)">(?<value>\d+)</temp>`,
|
`<temp unit="(?<unit>[CF]?)">(?<value>\d+)</temp>`,
|
||||||
`if unit == "C" then
|
`if unit == "C" then
|
||||||
value = value * 9/5 + 32
|
value = value * 9/5 + 32
|
||||||
@@ -739,7 +739,7 @@ func TestMixedDynamicAndNamedCaptures(t *testing.T) {
|
|||||||
<color rgb="0,255,0" name="GREEN" hex="#00FF00" />
|
<color rgb="0,255,0" name="GREEN" hex="#00FF00" />
|
||||||
`
|
`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<color rgb="(?<r>\d+),(?<g>\d+),(?<b>\d+)" name="(?<colorName>[^"]+)" />`,
|
`<color rgb="(?<r>\d+),(?<g>\d+),(?<b>\d+)" name="(?<colorName>[^"]+)" />`,
|
||||||
`-- Uppercase the name
|
`-- Uppercase the name
|
||||||
colorName = string.upper(colorName)
|
colorName = string.upper(colorName)
|
||||||
@@ -765,7 +765,7 @@ func TestNamedCapturesWithMultipleReferences(t *testing.T) {
|
|||||||
|
|
||||||
expected := `<text format="uppercase" length="11">HELLO WORLD</text>`
|
expected := `<text format="uppercase" length="11">HELLO WORLD</text>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<text>(?<content>[^<]+)</text>`,
|
`<text>(?<content>[^<]+)</text>`,
|
||||||
`local uppercaseContent = string.upper(content)
|
`local uppercaseContent = string.upper(content)
|
||||||
local contentLength = string.len(content)
|
local contentLength = string.len(content)
|
||||||
@@ -783,7 +783,7 @@ func TestNamedCaptureWithJsonData(t *testing.T) {
|
|||||||
|
|
||||||
expected := `<data>{"name":"JOHN","age":30}</data>`
|
expected := `<data>{"name":"JOHN","age":30}</data>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<data>(?<json>\{.*?\})</data>`,
|
`<data>(?<json>\{.*?\})</data>`,
|
||||||
`-- Parse JSON (simplified, assumes valid JSON)
|
`-- Parse JSON (simplified, assumes valid JSON)
|
||||||
local name = json:match('"name":"([^"]+)"')
|
local name = json:match('"name":"([^"]+)"')
|
||||||
@@ -813,7 +813,7 @@ func TestNamedCaptureInXML(t *testing.T) {
|
|||||||
</product>
|
</product>
|
||||||
`
|
`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`(?s)<price currency="(?<currency>[^"]+)">(?<price>\d+\.\d+)</price>.*?<stock>(?<stock>\d+)</stock>`,
|
`(?s)<price currency="(?<currency>[^"]+)">(?<price>\d+\.\d+)</price>.*?<stock>(?<stock>\d+)</stock>`,
|
||||||
`-- Add 20% to price if USD
|
`-- Add 20% to price if USD
|
||||||
if currency == "USD" then
|
if currency == "USD" then
|
||||||
@@ -870,7 +870,7 @@ func TestComprehensiveNamedCaptures(t *testing.T) {
|
|||||||
</products>
|
</products>
|
||||||
`
|
`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`(?s)<product sku="(?<sku>[^"]+)" status="(?<status>[^"]+)"[^>]*>\s*<name>(?<product_name>[^<]+)</name>\s*<price currency="(?<currency>[^"]+)">(?<price>\d+\.\d+)</price>\s*<quantity>(?<qty>\d+)</quantity>`,
|
`(?s)<product sku="(?<sku>[^"]+)" status="(?<status>[^"]+)"[^>]*>\s*<name>(?<product_name>[^<]+)</name>\s*<price currency="(?<currency>[^"]+)">(?<price>\d+\.\d+)</price>\s*<quantity>(?<qty>\d+)</quantity>`,
|
||||||
`-- Only process in-stock items
|
`-- Only process in-stock items
|
||||||
if status == "in-stock" then
|
if status == "in-stock" then
|
||||||
@@ -924,7 +924,7 @@ func TestVariousNamedCaptureFormats(t *testing.T) {
|
|||||||
</data>
|
</data>
|
||||||
`
|
`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`<entry id="(?<id_num>\d+)" value="(?<val>\d+)"(?: status="(?<status>[^"]*)")? />`,
|
`<entry id="(?<id_num>\d+)" value="(?<val>\d+)"(?: status="(?<status>[^"]*)")? />`,
|
||||||
`-- Prefix the ID with "ID-"
|
`-- Prefix the ID with "ID-"
|
||||||
id_num = "ID-" .. id_num
|
id_num = "ID-" .. id_num
|
||||||
@@ -963,7 +963,7 @@ func TestSimpleNamedCapture(t *testing.T) {
|
|||||||
|
|
||||||
expected := `<product name="WIDGET" price="19.99"/>`
|
expected := `<product name="WIDGET" price="19.99"/>`
|
||||||
|
|
||||||
result, mods, matches, err := ApiAdaptor(content,
|
result, mods, matches, err := APIAdaptor(content,
|
||||||
`name="(?<product_name>[^"]+)"`,
|
`name="(?<product_name>[^"]+)"`,
|
||||||
`product_name = string.upper(product_name)`)
|
`product_name = string.upper(product_name)`)
|
||||||
|
|
||||||
|
|||||||
@@ -404,6 +404,7 @@ files = ["test.txt"
|
|||||||
|
|
||||||
invalidFile := filepath.Join(tmpDir, "invalid.toml")
|
invalidFile := filepath.Join(tmpDir, "invalid.toml")
|
||||||
err = os.WriteFile(invalidFile, []byte(invalidTOML), 0644)
|
err = os.WriteFile(invalidFile, []byte(invalidTOML), 0644)
|
||||||
|
assert.NoError(t, err, "Should write invalid TOML file")
|
||||||
|
|
||||||
commands, err := utils.LoadCommandsFromTomlFiles("invalid.toml")
|
commands, err := utils.LoadCommandsFromTomlFiles("invalid.toml")
|
||||||
assert.Error(t, err, "Should return error for invalid TOML syntax")
|
assert.Error(t, err, "Should return error for invalid TOML syntax")
|
||||||
@@ -418,6 +419,7 @@ files = ["test.txt"
|
|||||||
// Test 3: Empty TOML file creates an error (this is expected behavior)
|
// Test 3: Empty TOML file creates an error (this is expected behavior)
|
||||||
emptyFile := filepath.Join(tmpDir, "empty.toml")
|
emptyFile := filepath.Join(tmpDir, "empty.toml")
|
||||||
err = os.WriteFile(emptyFile, []byte(""), 0644)
|
err = os.WriteFile(emptyFile, []byte(""), 0644)
|
||||||
|
assert.NoError(t, err, "Should write empty TOML file")
|
||||||
|
|
||||||
commands, err = utils.LoadCommandsFromTomlFiles("empty.toml")
|
commands, err = utils.LoadCommandsFromTomlFiles("empty.toml")
|
||||||
assert.Error(t, err, "Should return error for empty TOML file")
|
assert.Error(t, err, "Should return error for empty TOML file")
|
||||||
|
|||||||
Reference in New Issue
Block a user