Update example file with json commands

This commit is contained in:
2025-12-19 11:30:38 +01:00
parent 09cdc91761
commit b309e3e6f0
3 changed files with 138 additions and 22 deletions

View File

@@ -1,6 +1,9 @@
# Global variables (no name/regex/lua/files - only modifiers)
[[commands]]
modifiers = { foobar = 4, multiply = 1.5, prefix = 'NEW_', enabled = true }
# Global variables - available to all commands
[modifiers]
foobar = 4
multiply = 1.5
prefix = 'NEW_'
enabled = true
# Multi-regex example using variable in Lua
[[commands]]
@@ -99,21 +102,123 @@ regex = '(?P<key>[A-Za-z0-9_]+)\s*='
lua = 'key = prefix .. key; return true'
files = ['**/*.properties']
# JSON mode examples
# JSON mode examples - modify single field
[[commands]]
name = 'JSONArrayMultiply'
name = 'JSONModifyField'
json = true
lua = 'for i, item in ipairs(data.items) do data.items[i].value = item.value * 2 end; return true'
lua = '''
data.value = 84
modified = true
'''
files = ['data/**/*.json']
# JSON mode - add new field
[[commands]]
name = 'JSONObjectUpdate'
name = 'JSONAddField'
json = true
lua = 'data.version = "2.0.0"; data.enabled = true; return true'
lua = '''
data.newField = "added"
modified = true
'''
files = ['config/**/*.json']
# JSON mode - modify nested fields
[[commands]]
name = 'JSONNestedModify'
json = true
lua = 'if data.settings and data.settings.performance then data.settings.performance.multiplier = data.settings.performance.multiplier * 1.5 end; return true'
lua = '''
if data.config and data.config.settings then
data.config.settings.enabled = true
data.config.settings.timeout = 60
modified = true
end
'''
files = ['settings/**/*.json']
# JSON mode - modify array elements
[[commands]]
name = 'JSONArrayMultiply'
json = true
lua = '''
if data.items then
for i, item in ipairs(data.items) do
data.items[i].value = item.value * multiply
end
modified = true
end
'''
files = ['data/**/*.json']
# JSON mode - modify object version
[[commands]]
name = 'JSONObjectUpdate'
json = true
lua = '''
data.version = "2.0.0"
data.enabled = enabled
modified = true
'''
files = ['config/**/*.json']
# JSON mode - surgical editing of specific row
[[commands]]
name = 'JSONSurgicalEdit'
json = true
lua = '''
if data.Rows and data.Rows[1] then
data.Rows[1].Weight = 999
modified = true
end
'''
files = ['items/**/*.json']
# CSV processing example - read, modify, write
[[commands]]
name = 'CSVProcess'
regex = '(?P<csv>!any)'
lua = '''
local rows = fromCSV(csv, { hasheader = true })
for i, row in ipairs(rows) do
if row.Value then
row.Value = num(row.Value) * multiply
end
end
replacement = toCSV(rows, { hasheader = true })
return true
'''
files = ['data/**/*.csv']
# CSV processing with custom delimiter (TSV)
[[commands]]
name = 'TSVProcess'
regex = '(?P<tsv>!any)'
lua = '''
local rows = fromCSV(tsv, { delimiter = "\t", hasheader = true, hascomments = true })
for i, row in ipairs(rows) do
if row.Price then
row.Price = num(row.Price) * 1.1
end
end
replacement = toCSV(rows, { delimiter = "\t", hasheader = true })
return true
'''
files = ['data/**/*.tsv']
# CSV processing - modify specific columns
[[commands]]
name = 'CSVModifyColumns'
regex = '(?P<csv>!any)'
lua = '''
local rows = fromCSV(csv, { hasheader = true })
for i, row in ipairs(rows) do
if row.Name then
row.Name = prefix .. row.Name
end
if row.Status then
row.Status = upper(row.Status)
end
end
replacement = toCSV(rows, { hasheader = true })
return true
'''
files = ['exports/**/*.csv']

View File

@@ -70,8 +70,10 @@ func TestTOMLGlobalModifiers(t *testing.T) {
defer os.RemoveAll(tmpDir)
// Create TOML content with global modifiers
tomlContent := `[[commands]]
modifiers = { multiplier = 3, prefix = "TEST_", enabled = true }
tomlContent := `[modifiers]
multiplier = 3
prefix = "TEST_"
enabled = true
[[commands]]
name = "UseGlobalModifiers"
@@ -116,8 +118,8 @@ func TestTOMLMultilineRegex(t *testing.T) {
defer os.RemoveAll(tmpDir)
// Create TOML content with multiline regex using literal strings
tomlContent := `[[commands]]
modifiers = { factor = 2.5 }
tomlContent := `[modifiers]
factor = 2.5
[[commands]]
name = "MultilineTest"
@@ -301,8 +303,9 @@ func TestTOMLEndToEndIntegration(t *testing.T) {
defer os.RemoveAll(tmpDir)
// Create comprehensive TOML content
tomlContent := `[[commands]]
modifiers = { multiplier = 4, base_value = 100 }
tomlContent := `[modifiers]
multiplier = 4
base_value = 100
[[commands]]
name = "IntegrationTest"
@@ -475,19 +478,27 @@ func TestYAMLToTOMLConversion(t *testing.T) {
tomlContent := string(tomlData)
assert.Contains(t, tomlContent, `name = "ConversionTest"`, "TOML should contain first command name")
assert.Contains(t, tomlContent, `name = "AnotherTest"`, "TOML should contain second command name")
assert.Contains(t, tomlContent, `name = "GlobalModifiers"`, "TOML should contain global modifiers command")
assert.Contains(t, tomlContent, `[modifiers]`, "TOML should contain modifiers section")
assert.Contains(t, tomlContent, `multiplier = 2.5`, "TOML should contain multiplier")
assert.Contains(t, tomlContent, `prefix = "CONV_"`, "TOML should contain prefix")
// Test that converted TOML loads correctly
commands, modifiers, err := utils.LoadCommandsFromTomlFiles("test.toml")
assert.NoError(t, err, "Should load converted TOML without error")
assert.Len(t, commands, 2, "Should load 2 commands from converted TOML")
assert.Len(t, modifiers, 2, "Should load 2 modifiers from converted TOML")
assert.Len(t, commands, 3, "Should load 3 commands from converted TOML")
assert.Len(t, modifiers, 0, "Should have no top-level modifiers")
// Verify modifiers
assert.Equal(t, 2.5, modifiers["multiplier"], "Should preserve multiplier value")
assert.Equal(t, "CONV_", modifiers["prefix"], "Should preserve prefix value")
// Find the modifier command entry
var modifierCmd *utils.ModifyCommand
for i := range commands {
if commands[i].Name == "GlobalModifiers" {
modifierCmd = &commands[i]
break
}
}
assert.NotNil(t, modifierCmd, "Should find GlobalModifiers command")
assert.Equal(t, 2.5, modifierCmd.Modifiers["multiplier"], "Should preserve multiplier value")
assert.Equal(t, "CONV_", modifierCmd.Modifiers["prefix"], "Should preserve prefix value")
// Test skip functionality - run conversion again
err = utils.ConvertYAMLToTOML("test.yml")

View File

@@ -450,7 +450,7 @@ func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, map[string]
// TOML structure for commands array and top-level modifiers
var tomlData struct {
Modifiers map[string]interface{} `toml:"modifiers,omitempty"`
Commands []ModifyCommand `toml:"commands"`
Commands []ModifyCommand `toml:"commands"`
// Also support direct array without wrapper
DirectCommands []ModifyCommand `toml:"-"`
}