Refactor everything to processors and implement json and xml processors such as they are

This commit is contained in:
2025-03-24 15:45:04 +01:00
parent a796e8cac5
commit 4f70eaa329
9 changed files with 3109 additions and 1477 deletions

505
main.go
View File

@@ -6,14 +6,13 @@ import (
"io"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"github.com/bmatcuk/doublestar/v4"
lua "github.com/yuin/gopher-lua"
"modify/processor"
)
var Error *log.Logger
@@ -21,24 +20,24 @@ var Warning *log.Logger
var Info *log.Logger
var Success *log.Logger
// ModificationRecord tracks a single value modification
type ModificationRecord struct {
File string
OldValue string
NewValue string
Operation string
Context string
}
// GlobalStats tracks all modifications across files
type GlobalStats struct {
TotalMatches int
TotalModifications int
Modifications []ModificationRecord
Modifications []processor.ModificationRecord
ProcessedFiles int
FailedFiles int
}
// FileMode defines how we interpret and process files
type FileMode string
const (
ModeRegex FileMode = "regex" // Default mode using regex
ModeXML FileMode = "xml" // XML mode using XPath
ModeJSON FileMode = "json" // JSON mode using JSONPath
)
var stats GlobalStats
func init() {
@@ -65,19 +64,35 @@ func init() {
// Initialize global stats
stats = GlobalStats{
Modifications: make([]ModificationRecord, 0),
Modifications: make([]processor.ModificationRecord, 0),
}
}
func main() {
// Define flags
fileModeFlag := flag.String("mode", "regex", "Processing mode: regex, xml, json")
xpathFlag := flag.String("xpath", "", "XPath expression (for XML mode)")
jsonpathFlag := flag.String("jsonpath", "", "JSONPath expression (for JSON mode)")
verboseFlag := flag.Bool("verbose", false, "Enable verbose output")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <regex_with_capture_groups> <lua_expression> <...files_or_globs>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Usage: %s [options] <pattern> <lua_expression> <...files_or_globs>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n")
fmt.Fprintf(os.Stderr, " -mode string\n")
fmt.Fprintf(os.Stderr, " Processing mode: regex, xml, json (default \"regex\")\n")
fmt.Fprintf(os.Stderr, " -xpath string\n")
fmt.Fprintf(os.Stderr, " XPath expression (for XML mode)\n")
fmt.Fprintf(os.Stderr, " -jsonpath string\n")
fmt.Fprintf(os.Stderr, " JSONPath expression (for JSON mode)\n")
fmt.Fprintf(os.Stderr, " -verbose\n")
fmt.Fprintf(os.Stderr, " Enable verbose output\n")
fmt.Fprintf(os.Stderr, "\nExamples:\n")
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"*1.5\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"*1.5\" \"*.xml\"\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>,(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"=0\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " Regex mode (default):\n")
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"*1.5\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " XML mode:\n")
fmt.Fprintf(os.Stderr, " %s -mode=xml -xpath=\"//value\" \"*1.5\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " JSON mode:\n")
fmt.Fprintf(os.Stderr, " %s -mode=json -jsonpath=\"$.items[*].value\" \"*1.5\" data.json\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nNote: v1, v2, etc. are used to refer to capture groups as numbers.\n")
fmt.Fprintf(os.Stderr, " s1, s2, etc. are used to refer to capture groups as strings.\n")
fmt.Fprintf(os.Stderr, " Helper functions: num(str) converts string to number, str(num) converts number to string\n")
@@ -88,34 +103,127 @@ func main() {
}
flag.Parse()
// Set up verbose mode
if !*verboseFlag {
// If not verbose, suppress Info level logs
Info.SetOutput(io.Discard)
}
args := flag.Args()
if len(args) < 3 {
Error.Println("Insufficient arguments - need regex pattern, lua expression, and at least one file or glob pattern")
requiredArgCount := 3 // Default for regex mode
// XML/JSON modes need one fewer positional argument
if *fileModeFlag == "xml" || *fileModeFlag == "json" {
requiredArgCount = 2
}
if len(args) < requiredArgCount {
Error.Printf("%s mode requires %d arguments minimum", *fileModeFlag, requiredArgCount)
flag.Usage()
return
}
regexPattern := args[0]
luaExpr := args[1]
filePatterns := args[2:]
// Expand file patterns with glob support
files, err := expandFilePatterns(filePatterns)
if err != nil {
Error.Printf("Error expanding file patterns: %v", err)
// Validate mode-specific parameters
if *fileModeFlag == "xml" && *xpathFlag == "" {
Error.Printf("XML mode requires an XPath expression with -xpath flag")
return
}
if *fileModeFlag == "json" && *jsonpathFlag == "" {
Error.Printf("JSON mode requires a JSONPath expression with -jsonpath flag")
return
}
if len(files) == 0 {
Error.Println("No files found matching the specified patterns")
return
// Get the appropriate pattern and expression based on mode
var regexPattern string
var luaExpr string
var filePatterns []string
// In regex mode, we need both pattern arguments
// In XML/JSON modes, we only need the lua expression from args
if *fileModeFlag == "regex" {
regexPattern = args[0]
luaExpr = args[1]
filePatterns = args[2:]
// Process files with regex mode
processFilesWithRegex(regexPattern, luaExpr, filePatterns)
} else {
// XML/JSON modes
luaExpr = args[0]
filePatterns = args[1:]
// Prepare the Lua expression
originalLuaExpr := luaExpr
luaExpr = processor.BuildLuaScript(luaExpr)
if originalLuaExpr != luaExpr {
Info.Printf("Transformed Lua expression from '%s' to '%s'", originalLuaExpr, luaExpr)
}
// Expand file patterns with glob support
files, err := expandFilePatterns(filePatterns)
if err != nil {
Error.Printf("Error expanding file patterns: %v", err)
return
}
if len(files) == 0 {
Error.Printf("No files found matching the specified patterns")
return
}
// Create the processor based on mode
var proc processor.Processor
if *fileModeFlag == "xml" {
Info.Printf("Starting XML modifier with XPath '%s', expression '%s' on %d files",
*xpathFlag, luaExpr, len(files))
proc = processor.NewXMLProcessor(Info)
} else {
Info.Printf("Starting JSON modifier with JSONPath '%s', expression '%s' on %d files",
*jsonpathFlag, luaExpr, len(files))
proc = processor.NewJSONProcessor(Info)
}
var wg sync.WaitGroup
// Process each file
for _, file := range files {
wg.Add(1)
go func(file string) {
defer wg.Done()
Info.Printf("🔄 Processing file: %s", file)
// Pass the appropriate path expression as the pattern
var pattern string
if *fileModeFlag == "xml" {
pattern = *xpathFlag
} else {
pattern = *jsonpathFlag
}
modCount, matchCount, err := proc.Process(file, pattern, luaExpr, originalLuaExpr)
if err != nil {
Error.Printf("❌ Failed to process file %s: %v", file, err)
stats.FailedFiles++
} else {
Info.Printf("✅ Successfully processed file: %s", file)
stats.ProcessedFiles++
stats.TotalMatches += matchCount
stats.TotalModifications += modCount
}
}(file)
}
wg.Wait()
}
Info.Printf("Starting modifier with pattern '%s', expression '%s' on %d files", regexPattern, luaExpr, len(files))
// Print summary of all modifications
printSummary(luaExpr)
}
// processFilesWithRegex handles regex mode pattern processing for multiple files
func processFilesWithRegex(regexPattern string, luaExpr string, filePatterns []string) {
// Prepare the Lua expression
originalLuaExpr := luaExpr
luaExpr = buildLuaScript(luaExpr)
luaExpr = processor.BuildLuaScript(luaExpr)
if originalLuaExpr != luaExpr {
Info.Printf("Transformed Lua expression from '%s' to '%s'", originalLuaExpr, luaExpr)
}
@@ -146,6 +254,24 @@ func main() {
return
}
// Expand file patterns with glob support
files, err := expandFilePatterns(filePatterns)
if err != nil {
Error.Printf("Error expanding file patterns: %v", err)
return
}
if len(files) == 0 {
Error.Printf("No files found matching the specified patterns")
return
}
Info.Printf("Starting regex modifier with pattern '%s', expression '%s' on %d files",
regexPattern, luaExpr, len(files))
// Create the regex processor
proc := processor.NewRegexProcessor(pattern, Info)
var wg sync.WaitGroup
// Process each file
for _, file := range files {
@@ -153,20 +279,19 @@ func main() {
go func(file string) {
defer wg.Done()
Info.Printf("🔄 Processing file: %s", file)
err := processFile(file, pattern, luaExpr, originalLuaExpr)
modCount, matchCount, err := proc.Process(file, regexPattern, luaExpr, originalLuaExpr)
if err != nil {
Error.Printf("❌ Failed to process file %s: %v", file, err)
stats.FailedFiles++
} else {
Info.Printf("✅ Successfully processed file: %s", file)
stats.ProcessedFiles++
stats.TotalMatches += matchCount
stats.TotalModifications += modCount
}
}(file)
}
wg.Wait()
// Print summary of all modifications
printSummary(originalLuaExpr)
}
// printSummary outputs a formatted summary of all modifications made
@@ -180,7 +305,7 @@ func printSummary(operation string) {
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles, operation)
// Group modifications by file for better readability
fileGroups := make(map[string][]ModificationRecord)
fileGroups := make(map[string][]processor.ModificationRecord)
for _, mod := range stats.Modifications {
fileGroups[mod.File] = append(fileGroups[mod.File], mod)
}
@@ -212,292 +337,6 @@ func printSummary(operation string) {
}
}
// buildLuaScript creates a complete Lua script from the expression
func buildLuaScript(luaExpr string) string {
// Track if we modified the expression
modified := false
original := luaExpr
// Auto-prepend v1 for expressions starting with operators
if strings.HasPrefix(luaExpr, "*") ||
strings.HasPrefix(luaExpr, "/") ||
strings.HasPrefix(luaExpr, "+") ||
strings.HasPrefix(luaExpr, "-") ||
strings.HasPrefix(luaExpr, "^") ||
strings.HasPrefix(luaExpr, "%") {
luaExpr = "v1 = v1" + luaExpr
modified = true
} else if strings.HasPrefix(luaExpr, "=") {
// Handle direct assignment with = operator
luaExpr = "v1 " + luaExpr
modified = true
}
// Add assignment if needed
if !strings.Contains(luaExpr, "=") {
luaExpr = "v1 = " + luaExpr
modified = true
}
// Replace shorthand v[] and s[] with their direct variable names
newExpr := strings.ReplaceAll(luaExpr, "v[1]", "v1")
newExpr = strings.ReplaceAll(newExpr, "v[2]", "v2")
newExpr = strings.ReplaceAll(newExpr, "s[1]", "s1")
newExpr = strings.ReplaceAll(newExpr, "s[2]", "s2")
if newExpr != luaExpr {
luaExpr = newExpr
modified = true
}
if modified {
Info.Printf("Transformed Lua expression: '%s' → '%s'", original, luaExpr)
}
return luaExpr
}
func processFile(filename string, pattern *regexp.Regexp, luaExpr string, originalExpr string) error {
fullPath := filepath.Join(".", filename)
// Read file content
content, err := os.ReadFile(fullPath)
if err != nil {
Error.Printf("Cannot read file %s: %v", fullPath, err)
return fmt.Errorf("error reading file: %v", err)
}
fileContent := string(content)
Info.Printf("File %s loaded: %d bytes", fullPath, len(content))
// Process the content
result, modificationCount, matchCount, err := process(fileContent, pattern, luaExpr, filename, originalExpr)
if err != nil {
Error.Printf("Processing failed for %s: %v", fullPath, err)
return err
}
// Update global stats
stats.TotalMatches += matchCount
stats.TotalModifications += modificationCount
if modificationCount == 0 {
Warning.Printf("No modifications made to %s - pattern didn't match any content", fullPath)
return nil
}
// Write the modified content back
err = os.WriteFile(fullPath, []byte(result), 0644)
if err != nil {
Error.Printf("Failed to save changes to %s: %v", fullPath, err)
return fmt.Errorf("error writing file: %v", err)
}
Info.Printf("Made %d modifications to %s and saved (%d bytes)",
modificationCount, fullPath, len(result))
return nil
}
func process(data string, pattern *regexp.Regexp, luaExpr string, filename string, originalExpr string) (string, int, int, error) {
L := lua.NewState()
defer L.Close()
// Initialize Lua environment
modificationCount := 0
matchCount := 0
// Load math library
L.Push(L.GetGlobal("require"))
L.Push(lua.LString("math"))
if err := L.PCall(1, 1, nil); err != nil {
Error.Printf("Failed to load Lua math library: %v", err)
return data, 0, 0, fmt.Errorf("error loading Lua math library: %v", err)
}
// Initialize helper functions
helperScript := `
-- Custom Lua helpers for math operations
function min(a, b) return math.min(a, b) end
function max(a, b) return math.max(a, b) end
function round(x) return math.floor(x + 0.5) end
function floor(x) return math.floor(x) end
function ceil(x) return math.ceil(x) end
-- String to number conversion helper
function num(str)
return tonumber(str) or 0
end
-- Number to string conversion
function str(num)
return tostring(num)
end
-- Check if string is numeric
function is_number(str)
return tonumber(str) ~= nil
end
`
if err := L.DoString(helperScript); err != nil {
Error.Printf("Failed to load Lua helper functions: %v", err)
return data, 0, 0, fmt.Errorf("error loading helper functions: %v", err)
}
// Process all regex matches
result := pattern.ReplaceAllStringFunc(data, func(match string) string {
matchCount++
captures := pattern.FindStringSubmatch(match)
if len(captures) <= 1 {
// No capture groups, return unchanged
Warning.Printf("Match found but no capture groups: %s", limitString(match, 50))
return match
}
Info.Printf("Match found: %s", limitString(match, 50))
// Set up global variables v1, v2, etc. for the Lua context
captureValues := make([]string, len(captures)-1)
for i, capture := range captures[1:] {
captureValues[i] = capture
// Set the raw string value with s prefix
L.SetGlobal(fmt.Sprintf("s%d", i+1), lua.LString(capture))
// Also set numeric version with v prefix if possible
floatVal, err := strconv.ParseFloat(capture, 64)
if err == nil {
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LNumber(floatVal))
} else {
// For non-numeric values, set v also to the string value
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LString(capture))
}
}
// Execute the user's Lua code
if err := L.DoString(luaExpr); err != nil {
Error.Printf("Lua execution failed for match '%s': %v", limitString(match, 50), err)
return match // Return unchanged on error
}
// Get the modified values after Lua execution
modifications := make(map[int]string)
for i := 0; i < len(captures)-1 && i < 12; i++ {
// Check both v and s variables to see if any were modified
vVarName := fmt.Sprintf("v%d", i+1)
sVarName := fmt.Sprintf("s%d", i+1)
// First check the v-prefixed numeric variable
vLuaVal := L.GetGlobal(vVarName)
sLuaVal := L.GetGlobal(sVarName)
oldVal := captures[i+1]
var newVal string
var useModification bool
// First priority: check if the string variable was modified
if sLuaVal != lua.LNil {
if sStr, ok := sLuaVal.(lua.LString); ok {
newStrVal := string(sStr)
if newStrVal != oldVal {
newVal = newStrVal
useModification = true
}
}
}
// Second priority: if string wasn't modified, check numeric variable
if !useModification && vLuaVal != lua.LNil {
switch v := vLuaVal.(type) {
case lua.LNumber:
newNumVal := strconv.FormatFloat(float64(v), 'f', -1, 64)
if newNumVal != oldVal {
newVal = newNumVal
useModification = true
}
case lua.LString:
newStrVal := string(v)
if newStrVal != oldVal {
newVal = newStrVal
useModification = true
}
default:
newDefaultVal := fmt.Sprintf("%v", v)
if newDefaultVal != oldVal {
newVal = newDefaultVal
useModification = true
}
}
}
// Record the modification if anything changed
if useModification {
modifications[i] = newVal
}
}
// Apply modifications to the matched text
if len(modifications) == 0 {
return match // No changes
}
result := match
for i, newVal := range modifications {
oldVal := captures[i+1]
// Special handling for empty capture groups
if oldVal == "" {
// Find the position where the empty capture group should be
// by analyzing the regex pattern and current match
parts := pattern.SubexpNames()
if i+1 < len(parts) && parts[i+1] != "" {
// Named capture groups
subPattern := fmt.Sprintf("(?P<%s>)", parts[i+1])
emptyGroupPattern := regexp.MustCompile(subPattern)
if loc := emptyGroupPattern.FindStringIndex(result); loc != nil {
// Insert the new value at the capture group location
result = result[:loc[0]] + newVal + result[loc[1]:]
}
} else {
// For unnamed capture groups, we need to find where they would be in the regex
// This is a simplification that might not work for complex regex patterns
// but should handle the test case with <value></value>
tagPattern := regexp.MustCompile("<value></value>")
if loc := tagPattern.FindStringIndex(result); loc != nil {
// Replace the empty tag content with our new value
result = result[:loc[0]+7] + newVal + result[loc[1]-8:]
}
}
} else {
// Normal replacement for non-empty capture groups
result = strings.Replace(result, oldVal, newVal, 1)
}
// Extract a bit of context from the match for better reporting
contextStart := max(0, strings.Index(match, oldVal)-10)
contextLength := min(30, len(match)-contextStart)
if contextStart+contextLength > len(match) {
contextLength = len(match) - contextStart
}
contextStr := "..." + match[contextStart:contextStart+contextLength] + "..."
// Log the modification
Info.Printf("Modified value [%d]: '%s' → '%s'", i+1, limitString(oldVal, 30), limitString(newVal, 30))
// Record the modification for summary
stats.Modifications = append(stats.Modifications, ModificationRecord{
File: filename,
OldValue: oldVal,
NewValue: newVal,
Operation: originalExpr,
Context: fmt.Sprintf("(in %s)", limitString(contextStr, 30)),
})
}
modificationCount++
return result
})
return result, modificationCount, matchCount, nil
}
// limitString truncates a string to maxLen and adds "..." if truncated
func limitString(s string, maxLen int) string {
s = strings.ReplaceAll(s, "\n", "\\n")
@@ -507,22 +346,6 @@ func limitString(s string, maxLen int) string {
return s[:maxLen-3] + "..."
}
// max returns the maximum of two integers
func max(a, b int) int {
if a > b {
return a
}
return b
}
// min returns the minimum of two integers
func min(a, b int) int {
if a < b {
return a
}
return b
}
func expandFilePatterns(patterns []string) ([]string, error) {
var files []string
filesMap := make(map[string]bool)

File diff suppressed because it is too large Load Diff

609
processor/json.go Normal file
View File

@@ -0,0 +1,609 @@
package processor
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
lua "github.com/yuin/gopher-lua"
)
// JSONProcessor implements the Processor interface using JSONPath
type JSONProcessor struct {
Logger Logger
}
// NewJSONProcessor creates a new JSONProcessor
func NewJSONProcessor(logger Logger) *JSONProcessor {
return &JSONProcessor{
Logger: logger,
}
}
// Process implements the Processor interface for JSONProcessor
func (p *JSONProcessor) Process(filename string, pattern string, luaExpr string, originalExpr string) (int, int, error) {
// Use pattern as JSONPath expression
jsonPathExpr := pattern
// Read file content
fullPath := filepath.Join(".", filename)
content, err := os.ReadFile(fullPath)
if err != nil {
return 0, 0, fmt.Errorf("error reading file: %v", err)
}
fileContent := string(content)
if p.Logger != nil {
p.Logger.Printf("File %s loaded: %d bytes", fullPath, len(content))
}
// Process the content
modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, jsonPathExpr, luaExpr, originalExpr)
if err != nil {
return 0, 0, err
}
// If we made modifications, save the file
if modCount > 0 {
err = os.WriteFile(fullPath, []byte(modifiedContent), 0644)
if err != nil {
return 0, 0, fmt.Errorf("error writing file: %v", err)
}
if p.Logger != nil {
p.Logger.Printf("Made %d JSON value modifications to %s and saved (%d bytes)",
modCount, fullPath, len(modifiedContent))
}
} else if p.Logger != nil {
p.Logger.Printf("No modifications made to %s", fullPath)
}
return modCount, matchCount, nil
}
// ToLua implements the Processor interface for JSONProcessor
func (p *JSONProcessor) ToLua(L *lua.LState, data interface{}) error {
// For JSON, convert different types to appropriate Lua types
return nil
}
// FromLua implements the Processor interface for JSONProcessor
func (p *JSONProcessor) FromLua(L *lua.LState) (interface{}, error) {
// Extract changes from Lua environment
return nil, nil
}
// ProcessContent implements the Processor interface for JSONProcessor
// It processes JSON content directly without file I/O
func (p *JSONProcessor) ProcessContent(content string, pattern string, luaExpr string, originalExpr string) (string, int, int, error) {
// Parse JSON
var jsonDoc interface{}
err := json.Unmarshal([]byte(content), &jsonDoc)
if err != nil {
return "", 0, 0, fmt.Errorf("error parsing JSON: %v", err)
}
// Log the JSONPath expression we're using
if p.Logger != nil {
p.Logger.Printf("JSON mode selected with JSONPath expression: %s", pattern)
}
// Initialize Lua state
L := lua.NewState()
defer L.Close()
// Setup Lua helper functions
if err := InitLuaHelpers(L); err != nil {
return "", 0, 0, err
}
// Setup JSON helpers
p.SetupJSONHelpers(L)
// Find matching nodes with simple JSONPath implementation
matchingPaths, err := p.findNodePaths(jsonDoc, pattern)
if err != nil {
return "", 0, 0, fmt.Errorf("error finding JSON nodes: %v", err)
}
if len(matchingPaths) == 0 {
if p.Logger != nil {
p.Logger.Printf("No JSON nodes matched JSONPath expression: %s", pattern)
}
return content, 0, 0, nil
}
if p.Logger != nil {
p.Logger.Printf("Found %d JSON nodes matching the path", len(matchingPaths))
}
// Process each node
matchCount := len(matchingPaths)
modificationCount := 0
modifications := []ModificationRecord{}
// Clone the document for modification
var modifiedDoc interface{}
modifiedBytes, err := json.Marshal(jsonDoc)
if err != nil {
return "", 0, 0, fmt.Errorf("error cloning JSON document: %v", err)
}
err = json.Unmarshal(modifiedBytes, &modifiedDoc)
if err != nil {
return "", 0, 0, fmt.Errorf("error cloning JSON document: %v", err)
}
// For each matching path, extract value, apply Lua script, and update
for i, path := range matchingPaths {
// Extract the original value
originalValue, err := p.getValueAtPath(jsonDoc, path)
if err != nil || originalValue == nil {
if p.Logger != nil {
p.Logger.Printf("Error getting value at path %v: %v", path, err)
}
continue
}
if p.Logger != nil {
p.Logger.Printf("Processing node #%d at path %v with value: %v", i+1, path, originalValue)
}
// Process based on the value type
switch val := originalValue.(type) {
case float64:
// Set up Lua environment for numeric value
L.SetGlobal("v1", lua.LNumber(val))
L.SetGlobal("s1", lua.LString(fmt.Sprintf("%v", val)))
// Execute Lua script
if err := L.DoString(luaExpr); err != nil {
if p.Logger != nil {
p.Logger.Printf("Lua execution failed for node #%d: %v", i+1, err)
}
continue
}
// Extract modified value
modVal := L.GetGlobal("v1")
if v, ok := modVal.(lua.LNumber); ok {
newValue := float64(v)
// Update the value in the document only if it changed
if newValue != val {
err := p.setValueAtPath(modifiedDoc, path, newValue)
if err != nil {
if p.Logger != nil {
p.Logger.Printf("Error updating value at path %v: %v", path, err)
}
continue
}
modificationCount++
modifications = append(modifications, ModificationRecord{
File: "",
OldValue: fmt.Sprintf("%v", val),
NewValue: fmt.Sprintf("%v", newValue),
Operation: originalExpr,
Context: fmt.Sprintf("(JSONPath: %s)", pattern),
})
if p.Logger != nil {
p.Logger.Printf("Modified numeric node #%d: %v -> %v", i+1, val, newValue)
}
}
}
case string:
// Set up Lua environment for string value
L.SetGlobal("s1", lua.LString(val))
// Try to convert to number if possible
if floatVal, err := strconv.ParseFloat(val, 64); err == nil {
L.SetGlobal("v1", lua.LNumber(floatVal))
} else {
L.SetGlobal("v1", lua.LNumber(0)) // Default to 0 if not numeric
}
// Execute Lua script
if err := L.DoString(luaExpr); err != nil {
if p.Logger != nil {
p.Logger.Printf("Lua execution failed for node #%d: %v", i+1, err)
}
continue
}
// Check for modifications in string (s1) or numeric (v1) values
var newValue interface{}
modified := false
// Check if s1 was modified
sVal := L.GetGlobal("s1")
if s, ok := sVal.(lua.LString); ok && string(s) != val {
newValue = string(s)
modified = true
} else {
// Check if v1 was modified to a number
vVal := L.GetGlobal("v1")
if v, ok := vVal.(lua.LNumber); ok {
numStr := strconv.FormatFloat(float64(v), 'f', -1, 64)
if numStr != val {
newValue = numStr
modified = true
}
}
}
// Apply the modification if anything changed
if modified {
err := p.setValueAtPath(modifiedDoc, path, newValue)
if err != nil {
if p.Logger != nil {
p.Logger.Printf("Error updating value at path %v: %v", path, err)
}
continue
}
modificationCount++
modifications = append(modifications, ModificationRecord{
File: "",
OldValue: val,
NewValue: fmt.Sprintf("%v", newValue),
Operation: originalExpr,
Context: fmt.Sprintf("(JSONPath: %s)", pattern),
})
if p.Logger != nil {
p.Logger.Printf("Modified string node #%d: '%s' -> '%s'",
i+1, LimitString(val, 30), LimitString(fmt.Sprintf("%v", newValue), 30))
}
}
}
}
// Marshal the modified document back to JSON with indentation
if modificationCount > 0 {
modifiedJSON, err := json.MarshalIndent(modifiedDoc, "", " ")
if err != nil {
return "", 0, 0, fmt.Errorf("error marshaling modified JSON: %v", err)
}
if p.Logger != nil {
p.Logger.Printf("Made %d JSON node modifications", modificationCount)
}
return string(modifiedJSON), modificationCount, matchCount, nil
}
// If no modifications were made, return the original content
return content, 0, matchCount, nil
}
// findNodePaths implements a simplified JSONPath for finding paths to nodes
func (p *JSONProcessor) findNodePaths(doc interface{}, path string) ([][]interface{}, error) {
// Validate the path has proper syntax
if strings.Contains(path, "[[") || strings.Contains(path, "]]") {
return nil, fmt.Errorf("invalid JSONPath syntax: %s", path)
}
// Handle root element special case
if path == "$" {
return [][]interface{}{{doc}}, nil
}
// Split path into segments
segments := strings.Split(strings.TrimPrefix(path, "$."), ".")
// Start with the root
current := [][]interface{}{{doc}}
// Process each segment
for _, segment := range segments {
var next [][]interface{}
// Handle array notation [*]
if segment == "[*]" || strings.HasSuffix(segment, "[*]") {
baseName := strings.TrimSuffix(segment, "[*]")
for _, path := range current {
item := path[len(path)-1] // Get the last item in the path
switch v := item.(type) {
case map[string]interface{}:
if baseName == "" {
// [*] means all elements at this level
for _, val := range v {
if arr, ok := val.([]interface{}); ok {
for i, elem := range arr {
newPath := make([]interface{}, len(path)+2)
copy(newPath, path)
newPath[len(path)] = i // Array index
newPath[len(path)+1] = elem
next = append(next, newPath)
}
}
}
} else if arr, ok := v[baseName].([]interface{}); ok {
for i, elem := range arr {
newPath := make([]interface{}, len(path)+3)
copy(newPath, path)
newPath[len(path)] = baseName
newPath[len(path)+1] = i // Array index
newPath[len(path)+2] = elem
next = append(next, newPath)
}
}
case []interface{}:
for i, elem := range v {
newPath := make([]interface{}, len(path)+1)
copy(newPath, path)
newPath[len(path)-1] = i // Replace last elem with index
newPath[len(path)] = elem
next = append(next, newPath)
}
}
}
current = next
continue
}
// Handle specific array indices
if strings.Contains(segment, "[") && strings.Contains(segment, "]") {
// Validate proper array syntax
if !regexp.MustCompile(`\[\d+\]$`).MatchString(segment) {
return nil, fmt.Errorf("invalid array index in JSONPath: %s", segment)
}
// Extract base name and index
baseName := segment[:strings.Index(segment, "[")]
idxStr := segment[strings.Index(segment, "[")+1 : strings.Index(segment, "]")]
idx, err := strconv.Atoi(idxStr)
if err != nil {
return nil, fmt.Errorf("invalid array index: %s", idxStr)
}
for _, path := range current {
item := path[len(path)-1] // Get the last item in the path
if obj, ok := item.(map[string]interface{}); ok {
if arr, ok := obj[baseName].([]interface{}); ok && idx < len(arr) {
newPath := make([]interface{}, len(path)+3)
copy(newPath, path)
newPath[len(path)] = baseName
newPath[len(path)+1] = idx
newPath[len(path)+2] = arr[idx]
next = append(next, newPath)
}
}
}
current = next
continue
}
// Handle regular object properties
for _, path := range current {
item := path[len(path)-1] // Get the last item in the path
if obj, ok := item.(map[string]interface{}); ok {
if val, exists := obj[segment]; exists {
newPath := make([]interface{}, len(path)+2)
copy(newPath, path)
newPath[len(path)] = segment
newPath[len(path)+1] = val
next = append(next, newPath)
}
}
}
current = next
}
return current, nil
}
// getValueAtPath extracts a value from a JSON document at the specified path
func (p *JSONProcessor) getValueAtPath(doc interface{}, path []interface{}) (interface{}, error) {
if len(path) == 0 {
return nil, fmt.Errorf("empty path")
}
// The last element in the path is the value itself
return path[len(path)-1], nil
}
// setValueAtPath updates a value in a JSON document at the specified path
func (p *JSONProcessor) setValueAtPath(doc interface{}, path []interface{}, newValue interface{}) error {
if len(path) < 2 {
return fmt.Errorf("path too short to update value")
}
// The path structure alternates: object/key/object/key/.../finalObject/finalKey/value
// We need to navigate to the object containing our key
// We'll get the parent object and the key to modify
// Find the parent object (second to last object) and the key (last object's property name)
// For the path structure, the parent is at index len-3 and key at len-2
if len(path) < 3 {
// Simple case: directly update the root object
rootObj, ok := doc.(map[string]interface{})
if !ok {
return fmt.Errorf("root is not an object, cannot update")
}
// Key should be a string
key, ok := path[len(path)-2].(string)
if !ok {
return fmt.Errorf("key is not a string: %v", path[len(path)-2])
}
rootObj[key] = newValue
return nil
}
// More complex case: we need to navigate to the parent object
parentIdx := len(path) - 3
keyIdx := len(path) - 2
// The actual key we need to modify
key, isString := path[keyIdx].(string)
keyInt, isInt := path[keyIdx].(int)
if !isString && !isInt {
return fmt.Errorf("key must be string or int, got %T", path[keyIdx])
}
// Get the parent object that contains the key
parent := path[parentIdx]
// If parent is a map, use string key
if parentMap, ok := parent.(map[string]interface{}); ok && isString {
parentMap[key] = newValue
return nil
}
// If parent is an array, use int key
if parentArray, ok := parent.([]interface{}); ok && isInt {
if keyInt < 0 || keyInt >= len(parentArray) {
return fmt.Errorf("array index %d out of bounds [0,%d)", keyInt, len(parentArray))
}
parentArray[keyInt] = newValue
return nil
}
return fmt.Errorf("cannot update value: parent is %T and key is %T", parent, path[keyIdx])
}
// SetupJSONHelpers adds JSON-specific helper functions to Lua
func (p *JSONProcessor) SetupJSONHelpers(L *lua.LState) {
// Helper to get type of JSON value
L.SetGlobal("json_type", L.NewFunction(func(L *lua.LState) int {
// Get the value passed to the function
val := L.Get(1)
// Determine type
switch val.Type() {
case lua.LTNil:
L.Push(lua.LString("null"))
case lua.LTBool:
L.Push(lua.LString("boolean"))
case lua.LTNumber:
L.Push(lua.LString("number"))
case lua.LTString:
L.Push(lua.LString("string"))
case lua.LTTable:
// Could be object or array - check for numeric keys
isArray := true
table := val.(*lua.LTable)
table.ForEach(func(key, value lua.LValue) {
if key.Type() != lua.LTNumber {
isArray = false
}
})
if isArray {
L.Push(lua.LString("array"))
} else {
L.Push(lua.LString("object"))
}
default:
L.Push(lua.LString("unknown"))
}
return 1
}))
}
// jsonToLua converts a Go JSON value to a Lua value
func (p *JSONProcessor) jsonToLua(L *lua.LState, val interface{}) lua.LValue {
if val == nil {
return lua.LNil
}
switch v := val.(type) {
case bool:
return lua.LBool(v)
case float64:
return lua.LNumber(v)
case string:
return lua.LString(v)
case []interface{}:
arr := L.NewTable()
for i, item := range v {
arr.RawSetInt(i+1, p.jsonToLua(L, item))
}
return arr
case map[string]interface{}:
obj := L.NewTable()
for k, item := range v {
obj.RawSetString(k, p.jsonToLua(L, item))
}
return obj
default:
// For unknown types, convert to string representation
return lua.LString(fmt.Sprintf("%v", val))
}
}
// luaToJSON converts a Lua value to a Go JSON-compatible value
func (p *JSONProcessor) luaToJSON(val lua.LValue) interface{} {
switch val.Type() {
case lua.LTNil:
return nil
case lua.LTBool:
return lua.LVAsBool(val)
case lua.LTNumber:
return float64(val.(lua.LNumber))
case lua.LTString:
return val.String()
case lua.LTTable:
table := val.(*lua.LTable)
// Check if it's an array or an object
isArray := true
maxN := 0
table.ForEach(func(key, _ lua.LValue) {
if key.Type() == lua.LTNumber {
n := int(key.(lua.LNumber))
if n > maxN {
maxN = n
}
} else {
isArray = false
}
})
if isArray && maxN > 0 {
// It's an array
arr := make([]interface{}, maxN)
for i := 1; i <= maxN; i++ {
item := table.RawGetInt(i)
if item != lua.LNil {
arr[i-1] = p.luaToJSON(item)
}
}
return arr
} else {
// It's an object
obj := make(map[string]interface{})
table.ForEach(func(key, value lua.LValue) {
if key.Type() == lua.LTString {
obj[key.String()] = p.luaToJSON(value)
} else {
// Convert key to string if it's not already
obj[fmt.Sprintf("%v", key)] = p.luaToJSON(value)
}
})
return obj
}
default:
// For functions, userdata, etc., convert to string
return val.String()
}
}

511
processor/json_test.go Normal file
View File

@@ -0,0 +1,511 @@
package processor
import (
"encoding/json"
"strings"
"testing"
)
// TestJSONProcessor_Process_NumericValues tests processing numeric JSON values
func TestJSONProcessor_Process_NumericValues(t *testing.T) {
// Test JSON with numeric price values we want to modify
testJSON := `{
"catalog": {
"books": [
{
"id": "bk101",
"author": "Gambardella, Matthew",
"title": "JSON Developer's Guide",
"genre": "Computer",
"price": 44.95,
"publish_date": "2000-10-01"
},
{
"id": "bk102",
"author": "Ralls, Kim",
"title": "Midnight Rain",
"genre": "Fantasy",
"price": 5.95,
"publish_date": "2000-12-16"
}
]
}
}`
// Create a JSON processor
processor := NewJSONProcessor(&TestLogger{T: t})
// Process the JSON content directly to double all prices
jsonPathExpr := "$.catalog.books[*].price"
modifiedJSON, modCount, matchCount, err := processor.ProcessContent(testJSON, jsonPathExpr, "v1 = v1 * 2", "*2")
if err != nil {
t.Fatalf("Failed to process JSON content: %v", err)
}
// Check that we found and modified the correct number of nodes
if matchCount != 2 {
t.Errorf("Expected to match 2 nodes, got %d", matchCount)
}
if modCount != 2 {
t.Errorf("Expected to modify 2 nodes, got %d", modCount)
}
// Parse the JSON to check values more precisely
var result map[string]interface{}
if err := json.Unmarshal([]byte(modifiedJSON), &result); err != nil {
t.Fatalf("Failed to parse modified JSON: %v", err)
}
// Navigate to the books array
catalog, ok := result["catalog"].(map[string]interface{})
if !ok {
t.Fatalf("No catalog object found in result")
}
books, ok := catalog["books"].([]interface{})
if !ok {
t.Fatalf("No books array found in catalog")
}
// Check that both books have their prices doubled
// Note: The JSON numbers might be parsed as float64
book1, ok := books[0].(map[string]interface{})
if !ok {
t.Fatalf("First book is not an object")
}
price1, ok := book1["price"].(float64)
if !ok {
t.Fatalf("Price of first book is not a number")
}
if price1 != 89.9 {
t.Errorf("Expected first book price to be 89.9, got %v", price1)
}
book2, ok := books[1].(map[string]interface{})
if !ok {
t.Fatalf("Second book is not an object")
}
price2, ok := book2["price"].(float64)
if !ok {
t.Fatalf("Price of second book is not a number")
}
if price2 != 11.9 {
t.Errorf("Expected second book price to be 11.9, got %v", price2)
}
}
// TestJSONProcessor_Process_StringValues tests processing string JSON values
func TestJSONProcessor_Process_StringValues(t *testing.T) {
// Test JSON with string values we want to modify
testJSON := `{
"config": {
"settings": [
{ "name": "maxUsers", "value": "100" },
{ "name": "timeout", "value": "30" },
{ "name": "retries", "value": "5" }
]
}
}`
// Create a JSON processor
processor := NewJSONProcessor(&TestLogger{T: t})
// Process the JSON content directly to double all numeric values
jsonPathExpr := "$.config.settings[*].value"
modifiedJSON, modCount, matchCount, err := processor.ProcessContent(testJSON, jsonPathExpr, "v1 = v1 * 2", "*2")
if err != nil {
t.Fatalf("Failed to process JSON content: %v", err)
}
// Check that we found and modified the correct number of nodes
if matchCount != 3 {
t.Errorf("Expected to match 3 nodes, got %d", matchCount)
}
if modCount != 3 {
t.Errorf("Expected to modify 3 nodes, got %d", modCount)
}
// Check that the string values were doubled
if !strings.Contains(modifiedJSON, `"value": "200"`) {
t.Errorf("Modified content does not contain updated value 200")
}
if !strings.Contains(modifiedJSON, `"value": "60"`) {
t.Errorf("Modified content does not contain updated value 60")
}
if !strings.Contains(modifiedJSON, `"value": "10"`) {
t.Errorf("Modified content does not contain updated value 10")
}
// Verify the JSON is valid after modification
var result map[string]interface{}
if err := json.Unmarshal([]byte(modifiedJSON), &result); err != nil {
t.Fatalf("Modified JSON is not valid: %v", err)
}
}
// TestJSONProcessor_FindNodes tests the JSONPath implementation
func TestJSONProcessor_FindNodes(t *testing.T) {
// Test simple JSONPath functionality
testCases := []struct {
name string
jsonData string
path string
expectLen int
expectErr bool
}{
{
name: "Root element",
jsonData: `{"name": "root", "value": 100}`,
path: "$",
expectLen: 1,
expectErr: false,
},
{
name: "Direct property",
jsonData: `{"name": "test", "value": 100}`,
path: "$.value",
expectLen: 1,
expectErr: false,
},
{
name: "Array access",
jsonData: `{"items": [10, 20, 30]}`,
path: "$.items[1]",
expectLen: 1,
expectErr: false,
},
{
name: "All array elements",
jsonData: `{"items": [10, 20, 30]}`,
path: "$.items[*]",
expectLen: 3,
expectErr: false,
},
{
name: "Nested property",
jsonData: `{"user": {"name": "John", "age": 30}}`,
path: "$.user.age",
expectLen: 1,
expectErr: false,
},
{
name: "Array of objects",
jsonData: `{"users": [{"name": "John"}, {"name": "Jane"}]}`,
path: "$.users[*].name",
expectLen: 2,
expectErr: false,
},
{
name: "Invalid path",
jsonData: `{"name": "test"}`,
path: "$.invalid[[", // Double bracket should cause an error
expectLen: 0,
expectErr: true,
},
}
processor := &JSONProcessor{Logger: &TestLogger{}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Parse the JSON data
var jsonDoc interface{}
if err := json.Unmarshal([]byte(tc.jsonData), &jsonDoc); err != nil {
t.Fatalf("Failed to parse test JSON: %v", err)
}
// Find nodes with the given path
nodes, err := processor.findNodePaths(jsonDoc, tc.path)
// Check error expectation
if tc.expectErr && err == nil {
t.Errorf("Expected error but got none")
}
if !tc.expectErr && err != nil {
t.Errorf("Unexpected error: %v", err)
}
// Skip further checks if we expected an error
if tc.expectErr {
return
}
// Check the number of nodes found
if len(nodes) != tc.expectLen {
t.Errorf("Expected %d nodes, got %d", tc.expectLen, len(nodes))
}
})
}
}
// TestJSONProcessor_NestedModifications tests modifying nested JSON objects
func TestJSONProcessor_NestedModifications(t *testing.T) {
testJSON := `{
"company": {
"name": "ABC Corp",
"departments": [
{
"id": "dev",
"name": "Development",
"employees": [
{"id": 1, "name": "John Doe", "salary": 75000},
{"id": 2, "name": "Jane Smith", "salary": 82000}
]
},
{
"id": "sales",
"name": "Sales",
"employees": [
{"id": 3, "name": "Bob Johnson", "salary": 65000},
{"id": 4, "name": "Alice Brown", "salary": 68000}
]
}
]
}
}`
// Create a JSON processor
processor := NewJSONProcessor(&TestLogger{T: t})
// Process the JSON to give everyone a 10% raise
jsonPathExpr := "$.company.departments[*].employees[*].salary"
modifiedJSON, modCount, matchCount, err := processor.ProcessContent(testJSON, jsonPathExpr, "v1 = v1 * 1.1", "10% raise")
if err != nil {
t.Fatalf("Failed to process JSON content: %v", err)
}
// Check counts
if matchCount != 4 {
t.Errorf("Expected to match 4 salary nodes, got %d", matchCount)
}
if modCount != 4 {
t.Errorf("Expected to modify 4 nodes, got %d", modCount)
}
// Parse the result to verify changes
var result map[string]interface{}
if err := json.Unmarshal([]byte(modifiedJSON), &result); err != nil {
t.Fatalf("Failed to parse modified JSON: %v", err)
}
// Get company > departments
company := result["company"].(map[string]interface{})
departments := company["departments"].([]interface{})
// Check first department's first employee
dept1 := departments[0].(map[string]interface{})
employees1 := dept1["employees"].([]interface{})
emp1 := employees1[0].(map[string]interface{})
salary1 := emp1["salary"].(float64)
// Salary should be 75000 * 1.1 = 82500
if salary1 != 82500 {
t.Errorf("Expected first employee salary to be 82500, got %v", salary1)
}
// Check second department's second employee
dept2 := departments[1].(map[string]interface{})
employees2 := dept2["employees"].([]interface{})
emp4 := employees2[1].(map[string]interface{})
salary4 := emp4["salary"].(float64)
// Salary should be 68000 * 1.1 = 74800
if salary4 != 74800 {
t.Errorf("Expected fourth employee salary to be 74800, got %v", salary4)
}
}
// TestJSONProcessor_ArrayManipulation tests modifying JSON arrays
func TestJSONProcessor_ArrayManipulation(t *testing.T) {
testJSON := `{
"dataPoints": [10, 20, 30, 40, 50]
}`
// Create a JSON processor
processor := NewJSONProcessor(&TestLogger{T: t})
// Process the JSON to normalize values (divide by max value)
jsonPathExpr := "$.dataPoints[*]"
modifiedJSON, modCount, matchCount, err := processor.ProcessContent(testJSON, jsonPathExpr, "v1 = v1 / 50", "normalize")
if err != nil {
t.Fatalf("Failed to process JSON content: %v", err)
}
// Check counts
if matchCount != 5 {
t.Errorf("Expected to match 5 data points, got %d", matchCount)
}
if modCount != 5 {
t.Errorf("Expected to modify 5 nodes, got %d", modCount)
}
// Parse the result to verify changes
var result map[string]interface{}
if err := json.Unmarshal([]byte(modifiedJSON), &result); err != nil {
t.Fatalf("Failed to parse modified JSON: %v", err)
}
// Get the data points array
dataPoints := result["dataPoints"].([]interface{})
// Check values (should be divided by 50)
expectedValues := []float64{0.2, 0.4, 0.6, 0.8, 1.0}
for i, val := range dataPoints {
if val.(float64) != expectedValues[i] {
t.Errorf("Expected dataPoints[%d] to be %v, got %v", i, expectedValues[i], val)
}
}
}
// TestJSONProcessor_ConditionalModification tests applying changes only to certain elements
func TestJSONProcessor_ConditionalModification(t *testing.T) {
testJSON := `{
"products": [
{"id": "p1", "name": "Laptop", "price": 999.99, "discount": 0},
{"id": "p2", "name": "Headphones", "price": 59.99, "discount": 0},
{"id": "p3", "name": "Mouse", "price": 29.99, "discount": 0}
]
}`
// Create a JSON processor
processor := NewJSONProcessor(&TestLogger{T: t})
// Process: apply 10% discount to items over $50, 5% to others
luaScript := `
-- Get the path to find the parent (product)
local path = string.gsub(_PATH, ".discount$", "")
-- Custom logic based on price
local price = _PARENT.price
if price > 50 then
v1 = 0.1 -- 10% discount
else
v1 = 0.05 -- 5% discount
end
`
jsonPathExpr := "$.products[*].discount"
modifiedJSON, modCount, matchCount, err := processor.ProcessContent(testJSON, jsonPathExpr, luaScript, "apply discounts")
if err != nil {
t.Fatalf("Failed to process JSON content: %v", err)
}
// Check counts
if matchCount != 3 {
t.Errorf("Expected to match 3 discount nodes, got %d", matchCount)
}
if modCount != 3 {
t.Errorf("Expected to modify 3 nodes, got %d", modCount)
}
// Parse the result to verify changes
var result map[string]interface{}
if err := json.Unmarshal([]byte(modifiedJSON), &result); err != nil {
t.Fatalf("Failed to parse modified JSON: %v", err)
}
// Get products array
products := result["products"].([]interface{})
// Laptop and Headphones should have 10% discount
laptop := products[0].(map[string]interface{})
headphones := products[1].(map[string]interface{})
mouse := products[2].(map[string]interface{})
if laptop["discount"].(float64) != 0.1 {
t.Errorf("Expected laptop discount to be 0.1, got %v", laptop["discount"])
}
if headphones["discount"].(float64) != 0.1 {
t.Errorf("Expected headphones discount to be 0.1, got %v", headphones["discount"])
}
if mouse["discount"].(float64) != 0.05 {
t.Errorf("Expected mouse discount to be 0.05, got %v", mouse["discount"])
}
}
// TestJSONProcessor_ComplexScripts tests using more complex Lua scripts
func TestJSONProcessor_ComplexScripts(t *testing.T) {
testJSON := `{
"metrics": [
{"name": "CPU", "values": [45, 60, 75, 90, 80]},
{"name": "Memory", "values": [30, 40, 45, 50, 60]},
{"name": "Disk", "values": [20, 25, 30, 40, 50]}
]
}`
// Create a JSON processor
processor := NewJSONProcessor(&TestLogger{T: t})
// Apply a moving average transformation
luaScript := `
-- This script transforms an array using a moving average
local values = {}
local window = 3 -- window size
-- Get all the values as a table
for i = 1, 5 do
local element = _VALUE[i]
if element then
values[i] = element
end
end
-- Calculate moving averages
local result = {}
for i = 1, #values do
local sum = 0
local count = 0
-- Sum the window
for j = math.max(1, i-(window-1)/2), math.min(#values, i+(window-1)/2) do
sum = sum + values[j]
count = count + 1
end
-- Set the average
result[i] = sum / count
end
-- Update all values
for i = 1, #result do
_VALUE[i] = result[i]
end
`
jsonPathExpr := "$.metrics[*].values"
modifiedJSON, modCount, matchCount, err := processor.ProcessContent(testJSON, jsonPathExpr, luaScript, "moving average")
if err != nil {
t.Fatalf("Failed to process JSON content: %v", err)
}
// Check counts
if matchCount != 3 {
t.Errorf("Expected to match 3 value arrays, got %d", matchCount)
}
if modCount != 3 {
t.Errorf("Expected to modify 3 nodes, got %d", modCount)
}
// Parse and verify the values were smoothed
var result map[string]interface{}
if err := json.Unmarshal([]byte(modifiedJSON), &result); err != nil {
t.Fatalf("Failed to parse modified JSON: %v", err)
}
// The modification logic would smooth out the values
// We'll check that the JSON is valid at least
metrics := result["metrics"].([]interface{})
if len(metrics) != 3 {
t.Errorf("Expected 3 metrics, got %d", len(metrics))
}
// Each metrics should have 5 values
for i, metric := range metrics {
m := metric.(map[string]interface{})
values := m["values"].([]interface{})
if len(values) != 5 {
t.Errorf("Metric %d should have 5 values, got %d", i, len(values))
}
}
}

93
processor/processor.go Normal file
View File

@@ -0,0 +1,93 @@
package processor
import (
"fmt"
"strings"
lua "github.com/yuin/gopher-lua"
)
// Processor defines the interface for all file processors
type Processor interface {
// Process handles processing a file with the given pattern and Lua expression
Process(filename string, pattern string, luaExpr string, originalExpr string) (int, int, error)
// ProcessContent handles processing a string content directly with the given pattern and Lua expression
// Returns the modified content, modification count, match count, and any error
ProcessContent(content string, pattern string, luaExpr string, originalExpr string) (string, int, int, error)
// ToLua converts processor-specific data to Lua variables
ToLua(L *lua.LState, data interface{}) error
// FromLua retrieves modified data from Lua
FromLua(L *lua.LState) (interface{}, error)
}
// ModificationRecord tracks a single value modification
type ModificationRecord struct {
File string
OldValue string
NewValue string
Operation string
Context string
}
// InitLuaHelpers initializes common Lua helper functions
func InitLuaHelpers(L *lua.LState) error {
helperScript := `
-- Custom Lua helpers for math operations
function min(a, b) return math.min(a, b) end
function max(a, b) return math.max(a, b) end
function round(x) return math.floor(x + 0.5) end
function floor(x) return math.floor(x) end
function ceil(x) return math.ceil(x) end
function upper(s) return string.upper(s) end
function lower(s) return string.lower(s) end
-- String to number conversion helper
function num(str)
return tonumber(str) or 0
end
-- Number to string conversion
function str(num)
return tostring(num)
end
-- Check if string is numeric
function is_number(str)
return tonumber(str) ~= nil
end
`
if err := L.DoString(helperScript); err != nil {
return fmt.Errorf("error loading helper functions: %v", err)
}
return nil
}
// Helper utility functions
// LimitString truncates a string to maxLen and adds "..." if truncated
func LimitString(s string, maxLen int) string {
s = strings.ReplaceAll(s, "\n", "\\n")
if len(s) <= maxLen {
return s
}
return s[:maxLen-3] + "..."
}
// Max returns the maximum of two integers
func Max(a, b int) int {
if a > b {
return a
}
return b
}
// Min returns the minimum of two integers
func Min(a, b int) int {
if a < b {
return a
}
return b
}

328
processor/regex.go Normal file
View File

@@ -0,0 +1,328 @@
package processor
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
lua "github.com/yuin/gopher-lua"
)
// RegexProcessor implements the Processor interface using regex patterns
type RegexProcessor struct {
CompiledPattern *regexp.Regexp
Logger Logger
}
// Logger interface abstracts logging functionality
type Logger interface {
Printf(format string, v ...interface{})
}
// NewRegexProcessor creates a new RegexProcessor with the given pattern
func NewRegexProcessor(pattern *regexp.Regexp, logger Logger) *RegexProcessor {
return &RegexProcessor{
CompiledPattern: pattern,
Logger: logger,
}
}
// Process implements the Processor interface for RegexProcessor
func (p *RegexProcessor) Process(filename string, pattern string, luaExpr string, originalExpr string) (int, int, error) {
// Read file content
fullPath := filepath.Join(".", filename)
content, err := os.ReadFile(fullPath)
if err != nil {
return 0, 0, fmt.Errorf("error reading file: %v", err)
}
fileContent := string(content)
if p.Logger != nil {
p.Logger.Printf("File %s loaded: %d bytes", fullPath, len(content))
}
// Process the content with regex
result, modCount, matchCount, err := p.ProcessContent(fileContent, luaExpr, filename, originalExpr)
if err != nil {
return 0, 0, err
}
if modCount == 0 {
if p.Logger != nil {
p.Logger.Printf("No modifications made to %s - pattern didn't match any content", fullPath)
}
return 0, 0, nil
}
// Write the modified content back
err = os.WriteFile(fullPath, []byte(result), 0644)
if err != nil {
return 0, 0, fmt.Errorf("error writing file: %v", err)
}
if p.Logger != nil {
p.Logger.Printf("Made %d modifications to %s and saved (%d bytes)",
modCount, fullPath, len(result))
}
return modCount, matchCount, nil
}
// ToLua sets capture groups as Lua variables (v1, v2, etc. for numeric values and s1, s2, etc. for strings)
func (p *RegexProcessor) ToLua(L *lua.LState, data interface{}) error {
captures, ok := data.([]string)
if !ok {
return fmt.Errorf("expected []string for captures, got %T", data)
}
// Set variables for each capture group, starting from v1/s1 for the first capture
for i := 1; i < len(captures); i++ {
// Set string version (always available as s1, s2, etc.)
L.SetGlobal(fmt.Sprintf("s%d", i), lua.LString(captures[i]))
// Try to convert to number and set v1, v2, etc.
if val, err := strconv.ParseFloat(captures[i], 64); err == nil {
L.SetGlobal(fmt.Sprintf("v%d", i), lua.LNumber(val))
} else {
// For non-numeric values, set v to 0
L.SetGlobal(fmt.Sprintf("v%d", i), lua.LNumber(0))
}
}
return nil
}
// FromLua implements the Processor interface for RegexProcessor
func (p *RegexProcessor) FromLua(L *lua.LState) (interface{}, error) {
// Get the modified values after Lua execution
modifications := make(map[int]string)
// Check for modifications to v1-v12 and s1-s12
for i := 0; i < 12; i++ {
// Check both v and s variables to see if any were modified
vVarName := fmt.Sprintf("v%d", i+1)
sVarName := fmt.Sprintf("s%d", i+1)
vLuaVal := L.GetGlobal(vVarName)
sLuaVal := L.GetGlobal(sVarName)
// Get the v variable if it exists
if vLuaVal != lua.LNil {
switch v := vLuaVal.(type) {
case lua.LNumber:
// Convert numeric value to string
newNumVal := strconv.FormatFloat(float64(v), 'f', -1, 64)
modifications[i] = newNumVal
// We found a value, continue to next capture group
continue
case lua.LString:
// Use string value directly
newStrVal := string(v)
modifications[i] = newStrVal
continue
default:
// Convert other types to string
newDefaultVal := fmt.Sprintf("%v", v)
modifications[i] = newDefaultVal
continue
}
}
// Try the s variable if v variable wasn't found or couldn't be used
if sLuaVal != lua.LNil {
if sStr, ok := sLuaVal.(lua.LString); ok {
newStrVal := string(sStr)
modifications[i] = newStrVal
continue
}
}
}
if p.Logger != nil {
p.Logger.Printf("Final modifications map: %v", modifications)
}
return modifications, nil
}
// ProcessContent applies regex replacement with Lua processing
func (p *RegexProcessor) ProcessContent(data string, luaExpr string, filename string, originalExpr string) (string, int, int, error) {
L := lua.NewState()
defer L.Close()
// Initialize Lua environment
modificationCount := 0
matchCount := 0
modifications := []ModificationRecord{}
// Load math library
L.Push(L.GetGlobal("require"))
L.Push(lua.LString("math"))
if err := L.PCall(1, 1, nil); err != nil {
if p.Logger != nil {
p.Logger.Printf("Failed to load Lua math library: %v", err)
}
return data, 0, 0, fmt.Errorf("error loading Lua math library: %v", err)
}
// Initialize helper functions
if err := InitLuaHelpers(L); err != nil {
return data, 0, 0, err
}
// Process all regex matches
result := p.CompiledPattern.ReplaceAllStringFunc(data, func(match string) string {
matchCount++
captures := p.CompiledPattern.FindStringSubmatch(match)
if len(captures) <= 1 {
// No capture groups, return unchanged
if p.Logger != nil {
p.Logger.Printf("Match found but no capture groups: %s", LimitString(match, 50))
}
return match
}
if p.Logger != nil {
p.Logger.Printf("Match found: %s", LimitString(match, 50))
}
// Pass the captures to Lua environment
if err := p.ToLua(L, captures); err != nil {
if p.Logger != nil {
p.Logger.Printf("Failed to set Lua variables: %v", err)
}
return match
}
// Debug: print the Lua variables before execution
if p.Logger != nil {
v1 := L.GetGlobal("v1")
s1 := L.GetGlobal("s1")
p.Logger.Printf("Before Lua: v1=%v, s1=%v", v1, s1)
}
// Execute the user's Lua code
if err := L.DoString(luaExpr); err != nil {
if p.Logger != nil {
p.Logger.Printf("Lua execution failed for match '%s': %v", LimitString(match, 50), err)
}
return match // Return unchanged on error
}
// Debug: print the Lua variables after execution
if p.Logger != nil {
v1 := L.GetGlobal("v1")
s1 := L.GetGlobal("s1")
p.Logger.Printf("After Lua: v1=%v, s1=%v", v1, s1)
}
// Get modifications from Lua
modResult, err := p.FromLua(L)
if err != nil {
if p.Logger != nil {
p.Logger.Printf("Failed to get modifications from Lua: %v", err)
}
return match
}
// Debug: print the modifications detected
if p.Logger != nil {
p.Logger.Printf("Modifications detected: %v", modResult)
}
// Apply modifications to the matched text
modsMap, ok := modResult.(map[int]string)
if !ok || len(modsMap) == 0 {
p.Logger.Printf("No modifications detected after Lua script execution")
return match // No changes
}
// Apply the modifications to the original match
result := match
for i, newVal := range modsMap {
oldVal := captures[i+1]
// Special handling for empty capture groups
if oldVal == "" {
// Find the position where the empty capture group should be
// by analyzing the regex pattern and current match
parts := p.CompiledPattern.SubexpNames()
if i+1 < len(parts) && parts[i+1] != "" {
// Named capture groups
subPattern := fmt.Sprintf("(?P<%s>)", parts[i+1])
emptyGroupPattern := regexp.MustCompile(subPattern)
if loc := emptyGroupPattern.FindStringIndex(result); loc != nil {
// Insert the new value at the capture group location
result = result[:loc[0]] + newVal + result[loc[1]:]
}
} else {
// For unnamed capture groups, we need to find where they would be in the regex
// This is a simplification that might not work for complex regex patterns
// but should handle the test case with <value></value>
tagPattern := regexp.MustCompile("<value></value>")
if loc := tagPattern.FindStringIndex(result); loc != nil {
// Replace the empty tag content with our new value
result = result[:loc[0]+7] + newVal + result[loc[1]-8:]
}
}
} else {
// Normal replacement for non-empty capture groups
p.Logger.Printf("Replacing '%s' with '%s' in '%s'", oldVal, newVal, result)
result = strings.Replace(result, oldVal, newVal, 1)
p.Logger.Printf("After replacement: '%s'", result)
}
// Extract a bit of context from the match for better reporting
contextStart := Max(0, strings.Index(match, oldVal)-10)
contextLength := Min(30, len(match)-contextStart)
if contextStart+contextLength > len(match) {
contextLength = len(match) - contextStart
}
contextStr := "..." + match[contextStart:contextStart+contextLength] + "..."
// Log the modification
if p.Logger != nil {
p.Logger.Printf("Modified value [%d]: '%s' → '%s'", i+1, LimitString(oldVal, 30), LimitString(newVal, 30))
}
// Record the modification for summary
modifications = append(modifications, ModificationRecord{
File: filename,
OldValue: oldVal,
NewValue: newVal,
Operation: originalExpr,
Context: fmt.Sprintf("(in %s)", LimitString(contextStr, 30)),
})
}
modificationCount++
return result
})
return result, modificationCount, matchCount, nil
}
// BuildLuaScript creates a complete Lua script from the expression
func BuildLuaScript(luaExpr string) string {
// Auto-prepend v1 for expressions starting with operators
if strings.HasPrefix(luaExpr, "*") ||
strings.HasPrefix(luaExpr, "/") ||
strings.HasPrefix(luaExpr, "+") ||
strings.HasPrefix(luaExpr, "-") ||
strings.HasPrefix(luaExpr, "^") ||
strings.HasPrefix(luaExpr, "%") {
luaExpr = "v1 = v1" + luaExpr
} else if strings.HasPrefix(luaExpr, "=") {
// Handle direct assignment with = operator
luaExpr = "v1 " + luaExpr
}
// Add assignment if needed
if !strings.Contains(luaExpr, "=") {
luaExpr = "v1 = " + luaExpr
}
return luaExpr
}

605
processor/regex_test.go Normal file
View File

@@ -0,0 +1,605 @@
package processor
import (
"regexp"
"strings"
"testing"
)
// TestLogger implements the Logger interface for testing
type TestLogger struct {
T *testing.T // Reference to the test's *testing.T
}
func (l *TestLogger) Printf(format string, v ...interface{}) {
if l.T != nil {
l.T.Logf(format, v...)
}
}
// Helper function to normalize whitespace for comparison
func normalizeWhitespace(s string) string {
// Replace all whitespace with a single space
re := regexp.MustCompile(`\s+`)
return re.ReplaceAllString(strings.TrimSpace(s), " ")
}
func TestSimpleValueMultiplication(t *testing.T) {
content := `
<config>
<item>
<value>100</value>
</item>
</config>
`
expected := `
<config>
<item>
<value>150</value>
</item>
</config>
`
// Create a regex pattern with the (?s) flag for multiline matching
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
processor := NewRegexProcessor(regex, &TestLogger{T: t})
luaExpr := BuildLuaScript("*1.5")
// Enable verbose logging for this test
t.Logf("Running test with regex pattern: %s", regex.String())
t.Logf("Original content: %s", content)
t.Logf("Lua expression: %s", luaExpr)
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "*1.5")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
// Verify match and modification counts
if matchCount != 1 {
t.Errorf("Expected 1 match, got %d", matchCount)
}
if modCount != 1 {
t.Errorf("Expected 1 modification, got %d", modCount)
}
t.Logf("Modified content: %s", modifiedContent)
t.Logf("Expected content: %s", expected)
// Compare normalized content
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
func TestShorthandNotation(t *testing.T) {
content := `
<config>
<item>
<value>100</value>
</item>
</config>
`
expected := `
<config>
<item>
<value>150</value>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("v1 * 1.5") // Use direct assignment syntax
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "v1 * 1.5")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
// Verify match and modification counts
if matchCount != 1 {
t.Errorf("Expected 1 match, got %d", matchCount)
}
if modCount != 1 {
t.Errorf("Expected 1 modification, got %d", modCount)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
func TestShorthandNotationFloats(t *testing.T) {
content := `
<config>
<item>
<value>132.671327</value>
</item>
</config>
`
expected := `
<config>
<item>
<value>176.01681007940928</value>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>(\d*\.?\d+)</value>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("v1 * 1.32671327") // Use direct assignment syntax
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "v1 * 1.32671327")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
// Verify match and modification counts
if matchCount != 1 {
t.Errorf("Expected 1 match, got %d", matchCount)
}
if modCount != 1 {
t.Errorf("Expected 1 modification, got %d", modCount)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
func TestArrayNotation(t *testing.T) {
content := `
<config>
<item>
<value>100</value>
</item>
</config>
`
expected := `
<config>
<item>
<value>150</value>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("v1 = v1 * 1.5") // Use direct assignment syntax
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "v1 = v1 * 1.5")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
// Verify match and modification counts
if matchCount != 1 {
t.Errorf("Expected 1 match, got %d", matchCount)
}
if modCount != 1 {
t.Errorf("Expected 1 modification, got %d", modCount)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
func TestMultipleMatches(t *testing.T) {
content := `
<config>
<item>
<value>100</value>
</item>
<item>
<value>200</value>
</item>
<item> <value>300</value> </item>
</config>
`
expected := `
<config>
<item>
<value>150</value>
</item>
<item>
<value>300</value>
</item>
<item> <value>450</value> </item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("*1.5")
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "*1.5")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
// Verify match and modification counts
if matchCount != 3 {
t.Errorf("Expected 3 matches, got %d", matchCount)
}
if modCount != 3 {
t.Errorf("Expected 3 modifications, got %d", modCount)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
func TestMultipleCaptureGroups(t *testing.T) {
content := `
<config>
<item>
<value>10</value>
<multiplier>5</multiplier>
</item>
</config>
`
expected := `
<config>
<item>
<value>50</value>
<multiplier>5</multiplier>
</item>
</config>
`
// Use (?s) flag to match across multiple lines
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("v1 = v1 * v2") // Use direct assignment syntax
// Verify the regex matches before processing
matches := regex.FindStringSubmatch(content)
if len(matches) <= 1 {
t.Fatalf("Regex didn't match any capture groups in test input: %v", content)
}
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test", "v1 = v1 * v2")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
// Verify match and modification counts
if matchCount != 1 {
t.Errorf("Expected 1 match, got %d", matchCount)
}
if modCount != 1 {
t.Errorf("Expected 1 modification, got %d", modCount)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
func TestModifyingMultipleValues(t *testing.T) {
content := `
<config>
<item>
<value>50</value>
<multiplier>3</multiplier>
<divider>2</divider>
</item>
</config>
`
expected := `
<config>
<item>
<value>75</value>
<multiplier>5</multiplier>
<divider>1</divider>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>.*?<multiplier>(\d+)</multiplier>.*?<divider>(\d+)</divider>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("v1 = v1 * v2 / v3; v2 = min(v2 * 2, 5); v3 = max(1, v3 / 2)")
modifiedContent, modCount, matchCount, err := processor.ProcessContent(content, luaExpr, "test",
"v1 = v1 * v2 / v3; v2 = min(v2 * 2, 5); v3 = max(1, v3 / 2)")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
// Verify match and modification counts
if matchCount != 1 {
t.Errorf("Expected 1 match, got %d", matchCount)
}
if modCount != 1 {
t.Errorf("Expected 1 modification, got %d", modCount)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
// Added from main_test.go
func TestDecimalValues(t *testing.T) {
content := `
<config>
<item>
<value>10.5</value>
<multiplier>2.5</multiplier>
</item>
</config>
`
expected := `
<config>
<item>
<value>26.25</value>
<multiplier>2.5</multiplier>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("v1 = v1 * v2")
modifiedContent, _, _, err := processor.ProcessContent(content, luaExpr, "test", "v1 = v1 * v2")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
// Added from main_test.go
func TestLuaMathFunctions(t *testing.T) {
content := `
<config>
<item>
<value>16</value>
</item>
</config>
`
expected := `
<config>
<item>
<value>4</value>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("v1 = math.sqrt(v1)")
modifiedContent, _, _, err := processor.ProcessContent(content, luaExpr, "test", "v1 = math.sqrt(v1)")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
// Added from main_test.go
func TestDirectAssignment(t *testing.T) {
content := `
<config>
<item>
<value>100</value>
</item>
</config>
`
expected := `
<config>
<item>
<value>0</value>
</item>
</config>
`
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
processor := NewRegexProcessor(regex, &TestLogger{})
luaExpr := BuildLuaScript("=0")
modifiedContent, _, _, err := processor.ProcessContent(content, luaExpr, "test", "=0")
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
normalizedModified := normalizeWhitespace(modifiedContent)
normalizedExpected := normalizeWhitespace(expected)
if normalizedModified != normalizedExpected {
t.Fatalf("Expected modified content to be %q, but got %q", normalizedExpected, normalizedModified)
}
}
// Added from main_test.go
func TestStringAndNumericOperations(t *testing.T) {
tests := []struct {
name string
input string
regexPattern string
luaExpression string
expectedOutput string
expectedMods int
}{
{
name: "Basic numeric multiplication",
input: "<value>42</value>",
regexPattern: "<value>(\\d+)</value>",
luaExpression: "v1 = v1 * 2",
expectedOutput: "<value>84</value>",
expectedMods: 1,
},
{
name: "Basic string manipulation",
input: "<n>test</n>",
regexPattern: "<n>(.*?)</n>",
luaExpression: "s1 = string.upper(s1)",
expectedOutput: "<n>TEST</n>",
expectedMods: 1,
},
{
name: "String concatenation",
input: "<id>abc123</id>",
regexPattern: "<id>(.*?)</id>",
luaExpression: "s1 = s1 .. '_modified'",
expectedOutput: "<id>abc123_modified</id>",
expectedMods: 1,
},
{
name: "Numeric value from string using num()",
input: "<price>19.99</price>",
regexPattern: "<price>(.*?)</price>",
luaExpression: "v1 = num(s1) * 1.2",
expectedOutput: "<price>23.987999999999996</price>",
expectedMods: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Compile the regex pattern with multiline support
pattern := regexp.MustCompile("(?s)" + tt.regexPattern)
processor := NewRegexProcessor(pattern, &TestLogger{})
luaExpr := BuildLuaScript(tt.luaExpression)
// Process with our function
result, modCount, _, err := processor.ProcessContent(tt.input, luaExpr, "test", tt.luaExpression)
if err != nil {
t.Fatalf("Process function failed: %v", err)
}
// Check results
if result != tt.expectedOutput {
t.Errorf("Expected output: %s, got: %s", tt.expectedOutput, result)
}
if modCount != tt.expectedMods {
t.Errorf("Expected %d modifications, got %d", tt.expectedMods, modCount)
}
})
}
}
// Added from main_test.go
func TestEdgeCases(t *testing.T) {
tests := []struct {
name string
input string
regexPattern string
luaExpression string
expectedOutput string
expectedMods int
}{
{
name: "Empty capture group",
input: "<value></value>",
regexPattern: "<value>(.*?)</value>",
luaExpression: "s1 = 'filled'",
expectedOutput: "<value>filled</value>",
expectedMods: 1,
},
{
name: "Non-numeric string with numeric operation",
input: "<value>abc</value>",
regexPattern: "<value>(.*?)</value>",
luaExpression: "v1 = v1 * 2", // This would fail if we didn't handle strings properly
expectedOutput: "<value>abc</value>", // Should remain unchanged
expectedMods: 0, // No modifications
},
{
name: "Invalid number conversion",
input: "<value>abc</value>",
regexPattern: "<value>(.*?)</value>",
luaExpression: "v1 = num(s1) + 10", // num(s1) should return 0
expectedOutput: "<value>10</value>",
expectedMods: 1,
},
{
name: "Multiline string",
input: "<text>Line 1\nLine 2</text>",
regexPattern: "<text>(.*?)</text>",
luaExpression: "s1 = string.gsub(s1, '\\n', ' - ')",
expectedOutput: "<text>Line 1 - Line 2</text>",
expectedMods: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Make sure the regex can match across multiple lines
pattern := regexp.MustCompile("(?s)" + tt.regexPattern)
processor := NewRegexProcessor(pattern, &TestLogger{})
luaExpr := BuildLuaScript(tt.luaExpression)
// Process with our function
result, modCount, _, err := processor.ProcessContent(tt.input, luaExpr, "test", tt.luaExpression)
if err != nil {
t.Fatalf("Process function failed: %v", err)
}
// Check results
if result != tt.expectedOutput {
t.Errorf("Expected output: %s, got: %s", tt.expectedOutput, result)
}
if modCount != tt.expectedMods {
t.Errorf("Expected %d modifications, got %d", tt.expectedMods, modCount)
}
})
}
}
func TestBuildLuaScript(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"*1.5", "v1 = v1*1.5"},
{"/2", "v1 = v1/2"},
{"+10", "v1 = v1+10"},
{"-5", "v1 = v1-5"},
{"^2", "v1 = v1^2"},
{"%2", "v1 = v1%2"},
{"=100", "v1 =100"},
{"v1 * 2", "v1 = v1 * 2"},
{"v1 + v2", "v1 = v1 + v2"},
{"math.max(v1, 100)", "v1 = math.max(v1, 100)"},
// Added from main_test.go
{"s1 .. '_suffix'", "v1 = s1 .. '_suffix'"},
{"v1 * v2", "v1 = v1 * v2"},
{"s1 .. s2", "v1 = s1 .. s2"},
}
for _, tc := range testCases {
result := BuildLuaScript(tc.input)
if result != tc.expected {
t.Errorf("BuildLuaScript(%q): expected %q, got %q", tc.input, tc.expected, result)
}
}
}

454
processor/xml.go Normal file
View File

@@ -0,0 +1,454 @@
package processor
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/antchfx/xmlquery"
lua "github.com/yuin/gopher-lua"
)
// XMLProcessor implements the Processor interface using XPath
type XMLProcessor struct {
Logger Logger
}
// NewXMLProcessor creates a new XMLProcessor
func NewXMLProcessor(logger Logger) *XMLProcessor {
return &XMLProcessor{
Logger: logger,
}
}
// Process implements the Processor interface for XMLProcessor
func (p *XMLProcessor) Process(filename string, pattern string, luaExpr string, originalExpr string) (int, int, error) {
// Use pattern as XPath expression
xpathExpr := pattern
// Read file content
fullPath := filepath.Join(".", filename)
content, err := os.ReadFile(fullPath)
if err != nil {
return 0, 0, fmt.Errorf("error reading file: %v", err)
}
fileContent := string(content)
if p.Logger != nil {
p.Logger.Printf("File %s loaded: %d bytes", fullPath, len(content))
}
// Process the content
modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, xpathExpr, luaExpr, originalExpr)
if err != nil {
return 0, 0, err
}
// If we made modifications, save the file
if modCount > 0 {
err = os.WriteFile(fullPath, []byte(modifiedContent), 0644)
if err != nil {
return 0, 0, fmt.Errorf("error writing file: %v", err)
}
if p.Logger != nil {
p.Logger.Printf("Made %d XML node modifications to %s and saved (%d bytes)",
modCount, fullPath, len(modifiedContent))
}
}
return modCount, matchCount, nil
}
// ToLua implements the Processor interface for XMLProcessor
func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) error {
// Currently not used directly as this is handled in Process
return nil
}
// FromLua implements the Processor interface for XMLProcessor
func (p *XMLProcessor) FromLua(L *lua.LState) (interface{}, error) {
// Currently not used directly as this is handled in Process
return nil, nil
}
// XMLNodeToString converts an XML node to a string representation
func (p *XMLProcessor) XMLNodeToString(node *xmlquery.Node) string {
// Use a simple string representation for now
var sb strings.Builder
// Start tag with attributes
if node.Type == xmlquery.ElementNode {
sb.WriteString("<")
sb.WriteString(node.Data)
// Add attributes
for _, attr := range node.Attr {
sb.WriteString(" ")
sb.WriteString(attr.Name.Local)
sb.WriteString("=\"")
sb.WriteString(attr.Value)
sb.WriteString("\"")
}
// If self-closing
if node.FirstChild == nil {
sb.WriteString("/>")
return sb.String()
}
sb.WriteString(">")
} else if node.Type == xmlquery.TextNode {
// Just write the text content
sb.WriteString(node.Data)
return sb.String()
} else if node.Type == xmlquery.CommentNode {
// Write comment
sb.WriteString("<!--")
sb.WriteString(node.Data)
sb.WriteString("-->")
return sb.String()
}
// Add children
for child := node.FirstChild; child != nil; child = child.NextSibling {
sb.WriteString(p.XMLNodeToString(child))
}
// End tag for elements
if node.Type == xmlquery.ElementNode {
sb.WriteString("</")
sb.WriteString(node.Data)
sb.WriteString(">")
}
return sb.String()
}
// NodeToLuaTable creates a Lua table from an XML node
func (p *XMLProcessor) NodeToLuaTable(L *lua.LState, node *xmlquery.Node) lua.LValue {
nodeTable := L.NewTable()
// Add node name
L.SetField(nodeTable, "name", lua.LString(node.Data))
// Add node type
switch node.Type {
case xmlquery.ElementNode:
L.SetField(nodeTable, "type", lua.LString("element"))
case xmlquery.TextNode:
L.SetField(nodeTable, "type", lua.LString("text"))
case xmlquery.AttributeNode:
L.SetField(nodeTable, "type", lua.LString("attribute"))
case xmlquery.CommentNode:
L.SetField(nodeTable, "type", lua.LString("comment"))
default:
L.SetField(nodeTable, "type", lua.LString("other"))
}
// Add node text content if it's a text node
if node.Type == xmlquery.TextNode {
L.SetField(nodeTable, "content", lua.LString(node.Data))
}
// Add attributes if it's an element node
if node.Type == xmlquery.ElementNode && len(node.Attr) > 0 {
attrsTable := L.NewTable()
for _, attr := range node.Attr {
L.SetField(attrsTable, attr.Name.Local, lua.LString(attr.Value))
}
L.SetField(nodeTable, "attributes", attrsTable)
}
// Add children if any
if node.FirstChild != nil {
childrenTable := L.NewTable()
i := 1
for child := node.FirstChild; child != nil; child = child.NextSibling {
// Skip empty text nodes (whitespace)
if child.Type == xmlquery.TextNode && strings.TrimSpace(child.Data) == "" {
continue
}
childTable := p.NodeToLuaTable(L, child)
childrenTable.RawSetInt(i, childTable)
i++
}
L.SetField(nodeTable, "children", childrenTable)
}
return nodeTable
}
// GetModifiedNode retrieves a modified node from Lua
func (p *XMLProcessor) GetModifiedNode(L *lua.LState, originalNode *xmlquery.Node) (*xmlquery.Node, bool) {
// Check if we have a node global with changes
nodeTable := L.GetGlobal("node")
if nodeTable == lua.LNil || nodeTable.Type() != lua.LTTable {
return originalNode, false
}
// Clone the node since we don't want to modify the original
clonedNode := *originalNode
// For text nodes, check if content was changed
if originalNode.Type == xmlquery.TextNode {
contentField := L.GetField(nodeTable.(*lua.LTable), "content")
if contentField != lua.LNil {
if strContent, ok := contentField.(lua.LString); ok {
if string(strContent) != originalNode.Data {
clonedNode.Data = string(strContent)
return &clonedNode, true
}
}
}
return originalNode, false
}
// For element nodes, attributes might have been changed
if originalNode.Type == xmlquery.ElementNode {
attrsField := L.GetField(nodeTable.(*lua.LTable), "attributes")
if attrsField != lua.LNil && attrsField.Type() == lua.LTTable {
attrsTable := attrsField.(*lua.LTable)
// Check if any attributes changed
changed := false
for _, attr := range originalNode.Attr {
newValue := L.GetField(attrsTable, attr.Name.Local)
if newValue != lua.LNil {
if strValue, ok := newValue.(lua.LString); ok {
if string(strValue) != attr.Value {
// Create a new attribute with the changed value
for i, a := range clonedNode.Attr {
if a.Name.Local == attr.Name.Local {
clonedNode.Attr[i].Value = string(strValue)
changed = true
}
}
}
}
}
}
if changed {
return &clonedNode, true
}
}
}
// No changes detected
return originalNode, false
}
// SetupXMLHelpers adds XML-specific helper functions to Lua
func (p *XMLProcessor) SetupXMLHelpers(L *lua.LState) {
// Helper function to create a new XML node
L.SetGlobal("new_node", L.NewFunction(func(L *lua.LState) int {
nodeName := L.CheckString(1)
nodeTable := L.NewTable()
L.SetField(nodeTable, "name", lua.LString(nodeName))
L.SetField(nodeTable, "type", lua.LString("element"))
L.SetField(nodeTable, "attributes", L.NewTable())
L.SetField(nodeTable, "children", L.NewTable())
L.Push(nodeTable)
return 1
}))
// Helper function to set an attribute
L.SetGlobal("set_attr", L.NewFunction(func(L *lua.LState) int {
nodeTable := L.CheckTable(1)
attrName := L.CheckString(2)
attrValue := L.CheckString(3)
attrsTable := L.GetField(nodeTable, "attributes")
if attrsTable == lua.LNil {
attrsTable = L.NewTable()
L.SetField(nodeTable, "attributes", attrsTable)
}
L.SetField(attrsTable.(*lua.LTable), attrName, lua.LString(attrValue))
return 0
}))
// Helper function to add a child node
L.SetGlobal("add_child", L.NewFunction(func(L *lua.LState) int {
parentTable := L.CheckTable(1)
childTable := L.CheckTable(2)
childrenTable := L.GetField(parentTable, "children")
if childrenTable == lua.LNil {
childrenTable = L.NewTable()
L.SetField(parentTable, "children", childrenTable)
}
childrenTbl := childrenTable.(*lua.LTable)
childrenTbl.RawSetInt(childrenTbl.Len()+1, childTable)
return 0
}))
}
// ProcessContent implements the Processor interface for XMLProcessor
// It processes XML content directly without file I/O
func (p *XMLProcessor) ProcessContent(content string, pattern string, luaExpr string, originalExpr string) (string, int, int, error) {
// Parse the XML document
doc, err := xmlquery.Parse(strings.NewReader(content))
if err != nil {
return "", 0, 0, fmt.Errorf("error parsing XML: %v", err)
}
// Find nodes matching XPath expression
nodes, err := xmlquery.QueryAll(doc, pattern)
if err != nil {
return "", 0, 0, fmt.Errorf("invalid XPath expression: %v", err)
}
// Log what we found
if p.Logger != nil {
p.Logger.Printf("XML mode selected with XPath expression: %s (found %d matching nodes)",
pattern, len(nodes))
}
if len(nodes) == 0 {
if p.Logger != nil {
p.Logger.Printf("No XML nodes matched XPath expression: %s", pattern)
}
return content, 0, 0, nil
}
// Initialize Lua state
L := lua.NewState()
defer L.Close()
// Setup Lua helper functions
if err := InitLuaHelpers(L); err != nil {
return "", 0, 0, err
}
// Register XML-specific helper functions
p.SetupXMLHelpers(L)
// Track modifications
matchCount := len(nodes)
modificationCount := 0
modifiedContent := content
modifications := []ModificationRecord{}
// Process each matching node
for i, node := range nodes {
// Get the original text representation of this node
originalNodeText := p.XMLNodeToString(node)
if p.Logger != nil {
p.Logger.Printf("Found node #%d: %s", i+1, LimitString(originalNodeText, 100))
}
// For text nodes, we'll handle them directly
if node.Type == xmlquery.TextNode && node.Parent != nil {
// If this is a text node, we'll use its value directly
// Get the node's text content
textContent := node.Data
// Set up Lua environment
L.SetGlobal("v1", lua.LNumber(0)) // Default to 0 if not numeric
L.SetGlobal("s1", lua.LString(textContent))
// Try to convert to number if possible
if floatVal, err := strconv.ParseFloat(textContent, 64); err == nil {
L.SetGlobal("v1", lua.LNumber(floatVal))
}
// Execute user's Lua script
if err := L.DoString(luaExpr); err != nil {
if p.Logger != nil {
p.Logger.Printf("Lua execution failed for node #%d: %v", i+1, err)
}
continue // Skip this node on error
}
// Check for modifications
modVal := L.GetGlobal("v1")
if v, ok := modVal.(lua.LNumber); ok {
// If we have a numeric result, convert it to string
newValue := strconv.FormatFloat(float64(v), 'f', -1, 64)
if newValue != textContent {
// Replace the node content in the document
parentStr := p.XMLNodeToString(node.Parent)
newParentStr := strings.Replace(parentStr, textContent, newValue, 1)
modifiedContent = strings.Replace(modifiedContent, parentStr, newParentStr, 1)
modificationCount++
// Record the modification
modifications = append(modifications, ModificationRecord{
File: "",
OldValue: textContent,
NewValue: newValue,
Operation: originalExpr,
Context: fmt.Sprintf("(XPath: %s)", pattern),
})
if p.Logger != nil {
p.Logger.Printf("Modified text node #%d: '%s' -> '%s'",
i+1, LimitString(textContent, 30), LimitString(newValue, 30))
}
}
}
continue // Move to next node
}
// Convert the node to a Lua table
nodeTable := p.NodeToLuaTable(L, node)
// Set the node in Lua global variable for user script
L.SetGlobal("node", nodeTable)
// Execute user's Lua script
if err := L.DoString(luaExpr); err != nil {
if p.Logger != nil {
p.Logger.Printf("Lua execution failed for node #%d: %v", i+1, err)
}
continue // Skip this node on error
}
// Get modified node from Lua
modifiedNode, changed := p.GetModifiedNode(L, node)
if !changed {
if p.Logger != nil {
p.Logger.Printf("Node #%d was not modified by script", i+1)
}
continue
}
// Render the modified node back to XML
modifiedNodeText := p.XMLNodeToString(modifiedNode)
// Replace just this node in the document
if originalNodeText != modifiedNodeText {
modifiedContent = strings.Replace(
modifiedContent,
originalNodeText,
modifiedNodeText,
1)
modificationCount++
// Record the modification for reporting
modifications = append(modifications, ModificationRecord{
File: "",
OldValue: LimitString(originalNodeText, 30),
NewValue: LimitString(modifiedNodeText, 30),
Operation: originalExpr,
Context: fmt.Sprintf("(XPath: %s)", pattern),
})
if p.Logger != nil {
p.Logger.Printf("Modified node #%d", i+1)
}
}
}
if p.Logger != nil && modificationCount > 0 {
p.Logger.Printf("Made %d XML node modifications", modificationCount)
}
return modifiedContent, modificationCount, matchCount, nil
}

345
processor/xml_test.go Normal file
View File

@@ -0,0 +1,345 @@
package processor
import (
"strings"
"testing"
"github.com/antchfx/xmlquery"
)
func TestXMLProcessor_Process_TextNodes(t *testing.T) {
// Test XML file with price tags that we want to modify
testXML := `<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</book>
</catalog>`
// Create an XML processor
processor := NewXMLProcessor(&TestLogger{})
// Process the XML content directly to double all prices
xpathExpr := "//price/text()"
modifiedXML, modCount, matchCount, err := processor.ProcessContent(testXML, xpathExpr, "v1 = v1 * 2", "*2")
if err != nil {
t.Fatalf("Failed to process XML content: %v", err)
}
// Check that we found and modified the correct number of nodes
if matchCount != 2 {
t.Errorf("Expected to match 2 nodes, got %d", matchCount)
}
if modCount != 2 {
t.Errorf("Expected to modify 2 nodes, got %d", modCount)
}
// Check that prices were doubled
if !strings.Contains(modifiedXML, "<price>89.9</price>") {
t.Errorf("Modified content does not contain doubled price 89.9")
}
if !strings.Contains(modifiedXML, "<price>11.9</price>") {
t.Errorf("Modified content does not contain doubled price 11.9")
}
// Verify we can parse the XML after modification
_, err = xmlquery.Parse(strings.NewReader(modifiedXML))
if err != nil {
t.Errorf("Modified XML is not valid: %v", err)
}
}
func TestXMLProcessor_Process_Elements(t *testing.T) {
// Test XML file with elements that we want to modify attributes of
testXML := `<?xml version="1.0" encoding="UTF-8"?>
<items>
<item id="1" value="100" />
<item id="2" value="200" />
<item id="3" value="300" />
</items>`
// Create an XML processor
processor := NewXMLProcessor(&TestLogger{})
// Process the file to modify the value attribute
// We'll create a more complex Lua script that deals with the node table
luaScript := `
-- Get the current value attribute
local valueAttr = node.attributes.value
if valueAttr then
-- Convert to number and add 50
local numValue = tonumber(valueAttr)
if numValue then
-- Update the value in the attributes table
node.attributes.value = tostring(numValue + 50)
end
end
`
// Process the XML content directly
xpathExpr := "//item"
modifiedXML, modCount, matchCount, err := processor.ProcessContent(testXML, xpathExpr, luaScript, "Add 50 to values")
if err != nil {
t.Fatalf("Failed to process XML content: %v", err)
}
// Check that we found and modified the correct number of nodes
if matchCount != 3 {
t.Errorf("Expected to match 3 item nodes, got %d", matchCount)
}
if modCount != 3 {
t.Errorf("Expected to modify 3 nodes, got %d", modCount)
}
// Check that values were increased by 50
if !strings.Contains(modifiedXML, `value="150"`) {
t.Errorf("Modified content does not contain updated value 150")
}
if !strings.Contains(modifiedXML, `value="250"`) {
t.Errorf("Modified content does not contain updated value 250")
}
if !strings.Contains(modifiedXML, `value="350"`) {
t.Errorf("Modified content does not contain updated value 350")
}
// Verify we can parse the XML after modification
_, err = xmlquery.Parse(strings.NewReader(modifiedXML))
if err != nil {
t.Errorf("Modified XML is not valid: %v", err)
}
}
// New test for adding attributes to XML elements
func TestXMLProcessor_AddAttributes(t *testing.T) {
testXML := `<?xml version="1.0" encoding="UTF-8"?>
<root>
<element>Content</element>
<element>Another</element>
</root>`
processor := NewXMLProcessor(&TestLogger{})
// Add a new attribute to each element
luaScript := `
-- Add a new attribute
node.attributes.status = "active"
-- Also add another attribute with a sequential number
node.attributes.index = tostring(_POSITION)
`
xpathExpr := "//element"
modifiedXML, modCount, matchCount, err := processor.ProcessContent(testXML, xpathExpr, luaScript, "Add attributes")
if err != nil {
t.Fatalf("Failed to process XML content: %v", err)
}
// Check counts
if matchCount != 2 {
t.Errorf("Expected to match 2 nodes, got %d", matchCount)
}
if modCount != 2 {
t.Errorf("Expected to modify 2 nodes, got %d", modCount)
}
// Verify the new attributes
if !strings.Contains(modifiedXML, `status="active"`) {
t.Errorf("Modified content does not contain added status attribute")
}
if !strings.Contains(modifiedXML, `index="1"`) && !strings.Contains(modifiedXML, `index="2"`) {
t.Errorf("Modified content does not contain added index attributes")
}
// Verify the XML is valid
_, err = xmlquery.Parse(strings.NewReader(modifiedXML))
if err != nil {
t.Errorf("Modified XML is not valid: %v", err)
}
}
// Test for adding new child elements
func TestXMLProcessor_AddChildElements(t *testing.T) {
testXML := `<?xml version="1.0" encoding="UTF-8"?>
<products>
<product id="1">
<name>Product One</name>
<price>10.99</price>
</product>
<product id="2">
<name>Product Two</name>
<price>20.99</price>
</product>
</products>`
processor := NewXMLProcessor(&TestLogger{})
// Add a new child element to each product
luaScript := `
-- Create a new "discount" child element
local discount = create_node("discount")
-- Calculate discount as 10% of price
local priceText = ""
for _, child in ipairs(node.children) do
if child.name == "price" and child.children[1] then
priceText = child.children[1].data
break
end
end
local price = tonumber(priceText) or 0
local discountValue = price * 0.1
-- Add text content to the discount element
discount.children[1] = {type="text", data=string.format("%.2f", discountValue)}
-- Add the new element as a child
add_child(node, discount)
`
xpathExpr := "//product"
modifiedXML, modCount, matchCount, err := processor.ProcessContent(testXML, xpathExpr, luaScript, "Add discount elements")
if err != nil {
t.Fatalf("Failed to process XML content: %v", err)
}
// Check counts
if matchCount != 2 {
t.Errorf("Expected to match 2 nodes, got %d", matchCount)
}
if modCount != 2 {
t.Errorf("Expected to modify 2 nodes, got %d", modCount)
}
// Verify the new elements
if !strings.Contains(modifiedXML, "<discount>1.10</discount>") {
t.Errorf("Modified content does not contain first discount element")
}
if !strings.Contains(modifiedXML, "<discount>2.10</discount>") {
t.Errorf("Modified content does not contain second discount element")
}
// Verify the XML is valid
_, err = xmlquery.Parse(strings.NewReader(modifiedXML))
if err != nil {
t.Errorf("Modified XML is not valid: %v", err)
}
}
// Test for complex XML transformations
func TestXMLProcessor_ComplexTransformation(t *testing.T) {
testXML := `<?xml version="1.0" encoding="UTF-8"?>
<config>
<settings>
<setting name="timeout" value="30" />
<setting name="retries" value="3" />
<setting name="enabled" value="true" />
</settings>
</config>`
processor := NewXMLProcessor(&TestLogger{})
// Complex transformation that changes attributes based on name
luaScript := `
local name = node.attributes.name
local value = node.attributes.value
if name == "timeout" then
-- Double the timeout
node.attributes.value = tostring(tonumber(value) * 2)
-- Add a unit attribute
node.attributes.unit = "seconds"
elseif name == "retries" then
-- Increase retries by 2
node.attributes.value = tostring(tonumber(value) + 2)
-- Add a comment element as sibling
local comment = create_node("comment")
comment.children[1] = {type="text", data="Increased for reliability"}
-- We can't directly add siblings in this implementation
-- But this would be the place to do it if supported
elseif name == "enabled" and value == "true" then
-- Add a priority attribute for enabled settings
node.attributes.priority = "high"
end
`
xpathExpr := "//setting"
modifiedXML, _, matchCount, err := processor.ProcessContent(testXML, xpathExpr, luaScript, "Transform settings")
if err != nil {
t.Fatalf("Failed to process XML content: %v", err)
}
// Check counts
if matchCount != 3 {
t.Errorf("Expected to match 3 nodes, got %d", matchCount)
}
// Verify the transformed attributes
if !strings.Contains(modifiedXML, `value="60"`) {
t.Errorf("Modified content does not have doubled timeout value")
}
if !strings.Contains(modifiedXML, `unit="seconds"`) {
t.Errorf("Modified content does not have added unit attribute")
}
if !strings.Contains(modifiedXML, `value="5"`) {
t.Errorf("Modified content does not have increased retries value")
}
if !strings.Contains(modifiedXML, `priority="high"`) {
t.Errorf("Modified content does not have added priority attribute")
}
// Verify the XML is valid
_, err = xmlquery.Parse(strings.NewReader(modifiedXML))
if err != nil {
t.Errorf("Modified XML is not valid: %v", err)
}
}
// Test for handling special XML characters
func TestXMLProcessor_SpecialCharacters(t *testing.T) {
testXML := `<?xml version="1.0" encoding="UTF-8"?>
<data>
<item><![CDATA[Special & <characters> "here"]]></item>
<item>Regular & text with <b>markup</b></item>
</data>`
processor := NewXMLProcessor(&TestLogger{})
// Process text nodes, being careful with special characters
luaScript := `
-- For text nodes, replace & with &amp;
s1 = string.gsub(s1, "&([^;])", "&amp;%1")
`
xpathExpr := "//item/text()"
modifiedXML, _, _, err := processor.ProcessContent(testXML, xpathExpr, luaScript, "Handle special chars")
if err != nil {
t.Fatalf("Failed to process XML content: %v", err)
}
// CDATA sections should be preserved
if !strings.Contains(modifiedXML, "<![CDATA[") {
t.Errorf("CDATA section was not preserved")
}
// Verify the XML is valid
_, err = xmlquery.Parse(strings.NewReader(modifiedXML))
if err != nil {
t.Errorf("Modified XML is not valid: %v", err)
}
}