Fix some go linter warnings

This commit is contained in:
2025-11-15 16:42:46 +01:00
parent 4fb25d0463
commit 5f1fdfa6c1
8 changed files with 84 additions and 99 deletions

View File

@@ -30,7 +30,7 @@ func normalizeWhitespace(s string) string {
return re.ReplaceAllString(strings.TrimSpace(s), " ")
}
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
func APIAdaptor(content string, regex string, lua string) (string, int, int, error) {
command := utils.ModifyCommand{
Regex: regex,
Lua: lua,
@@ -79,7 +79,7 @@ func TestSimpleValueMultiplication(t *testing.T) {
</item>
</config>`
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(\d+)</value>`, "v1 = v1*1.5")
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(\d+)</value>`, "v1 = v1*1.5")
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
@@ -100,7 +100,7 @@ func TestShorthandNotation(t *testing.T) {
</item>
</config>`
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(\d+)</value>`, "v1*1.5")
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(\d+)</value>`, "v1*1.5")
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
@@ -121,7 +121,7 @@ func TestShorthandNotationFloats(t *testing.T) {
</item>
</config>`
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(\d+\.\d+)</value>`, "v1*1.5")
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(\d+\.\d+)</value>`, "v1*1.5")
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
@@ -146,7 +146,7 @@ func TestArrayNotation(t *testing.T) {
</prices>
</config>`
result, mods, matches, err := ApiAdaptor(content, `(?s)<price>(\d+)</price>`, "v1*2")
result, mods, matches, err := APIAdaptor(content, `(?s)<price>(\d+)</price>`, "v1*2")
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches)
@@ -167,7 +167,7 @@ func TestMultipleNumericMatches(t *testing.T) {
<entry>400</entry>
</data>`
result, mods, matches, err := ApiAdaptor(content, `<entry>(\d+)</entry>`, "v1*2")
result, mods, matches, err := APIAdaptor(content, `<entry>(\d+)</entry>`, "v1*2")
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 3, matches, "Expected 3 matches, got %d", matches)
@@ -186,7 +186,7 @@ func TestMultipleStringMatches(t *testing.T) {
<name>Mary_modified</name>
</data>`
result, mods, matches, err := ApiAdaptor(content, `<name>([A-Za-z]+)</name>`, `s1 = s1 .. "_modified"`)
result, mods, matches, err := APIAdaptor(content, `<name>([A-Za-z]+)</name>`, `s1 = s1 .. "_modified"`)
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
@@ -205,7 +205,7 @@ func TestStringUpperCase(t *testing.T) {
<user>MARY</user>
</users>`
result, mods, matches, err := ApiAdaptor(content, `<user>([A-Za-z]+)</user>`, `s1 = string.upper(s1)`)
result, mods, matches, err := APIAdaptor(content, `<user>([A-Za-z]+)</user>`, `s1 = string.upper(s1)`)
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
@@ -224,7 +224,7 @@ func TestStringConcatenation(t *testing.T) {
<product>Banana_fruit</product>
</products>`
result, mods, matches, err := ApiAdaptor(content, `<product>([A-Za-z]+)</product>`, `s1 = s1 .. "_fruit"`)
result, mods, matches, err := APIAdaptor(content, `<product>([A-Za-z]+)</product>`, `s1 = s1 .. "_fruit"`)
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 2, matches, "Expected 2 matches, got %d", matches)
@@ -254,7 +254,7 @@ func TestDecimalValues(t *testing.T) {
regex := regexp.MustCompile(`(?s)<value>([0-9.]+)</value>.*?<multiplier>([0-9.]+)</multiplier>`)
luaExpr := BuildLuaScript("v1 = v1 * v2")
result, _, _, err := ApiAdaptor(content, regex.String(), luaExpr)
result, _, _, err := APIAdaptor(content, regex.String(), luaExpr)
assert.NoError(t, err, "Error processing content: %v", err)
normalizedModified := normalizeWhitespace(result)
@@ -282,7 +282,7 @@ func TestLuaMathFunctions(t *testing.T) {
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
luaExpr := BuildLuaScript("v1 = math.sqrt(v1)")
modifiedContent, _, _, err := ApiAdaptor(content, regex.String(), luaExpr)
modifiedContent, _, _, err := APIAdaptor(content, regex.String(), luaExpr)
assert.NoError(t, err, "Error processing content: %v", err)
normalizedModified := normalizeWhitespace(modifiedContent)
@@ -310,7 +310,7 @@ func TestDirectAssignment(t *testing.T) {
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
luaExpr := BuildLuaScript("=0")
modifiedContent, _, _, err := ApiAdaptor(content, regex.String(), luaExpr)
modifiedContent, _, _, err := APIAdaptor(content, regex.String(), luaExpr)
assert.NoError(t, err, "Error processing content: %v", err)
normalizedModified := normalizeWhitespace(modifiedContent)
@@ -369,7 +369,7 @@ func TestStringAndNumericOperations(t *testing.T) {
luaExpr := BuildLuaScript(tt.luaExpression)
// Process with our function
result, modCount, _, err := ApiAdaptor(tt.input, pattern, luaExpr)
result, modCount, _, err := APIAdaptor(tt.input, pattern, luaExpr)
assert.NoError(t, err, "Process function failed: %v", err)
// Check results
@@ -430,7 +430,7 @@ func TestEdgeCases(t *testing.T) {
luaExpr := BuildLuaScript(tt.luaExpression)
// Process with our function
result, modCount, _, err := ApiAdaptor(tt.input, pattern, luaExpr)
result, modCount, _, err := APIAdaptor(tt.input, pattern, luaExpr)
assert.NoError(t, err, "Process function failed: %v", err)
// Check results
@@ -453,7 +453,7 @@ func TestNamedCaptureGroups(t *testing.T) {
</item>
</config>`
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(?<amount>\d+)</value>`, "amount = amount * 2")
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(?<amount>\d+)</value>`, "amount = amount * 2")
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
@@ -474,7 +474,7 @@ func TestNamedCaptureGroupsNum(t *testing.T) {
</item>
</config>`
result, mods, matches, err := ApiAdaptor(content, `(?s)<value>(?<amount>!num)</value>`, "amount = amount * 2")
result, mods, matches, err := APIAdaptor(content, `(?s)<value>(?<amount>!num)</value>`, "amount = amount * 2")
assert.NoError(t, err, "Error processing content: %v", err)
assert.Equal(t, 1, matches, "Expected 1 match, got %d", matches)
@@ -495,7 +495,7 @@ func TestMultipleNamedCaptureGroups(t *testing.T) {
<quantity>15</quantity>
</product>`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`(?s)<name>(?<prodName>[^<]+)</name>.*?<price>(?<prodPrice>\d+\.\d+)</price>.*?<quantity>(?<prodQty>\d+)</quantity>`,
`prodName = string.upper(prodName)
prodPrice = round(prodPrice + 8, 2)
@@ -518,7 +518,7 @@ func TestMixedIndexedAndNamedCaptures(t *testing.T) {
<data>VALUE</data>
</entry>`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`(?s)<id>(\d+)</id>.*?<data>(?<dataField>[^<]+)</data>`,
`v1 = v1 * 2
dataField = string.upper(dataField)`)
@@ -550,7 +550,7 @@ func TestComplexNestedNamedCaptures(t *testing.T) {
</contact>
</person>`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`(?s)<details>.*?<name>(?<fullName>[^<]+)</name>.*?<age>(?<age>\d+)</age>`,
`fullName = string.upper(fullName) .. " (" .. age .. ")"`)
@@ -571,7 +571,7 @@ func TestNamedCaptureWithVariableReadback(t *testing.T) {
<mana>300</mana>
</stats>`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`(?s)<health>(?<hp>\d+)</health>.*?<mana>(?<mp>\d+)</mana>`,
`hp = hp * 1.5
mp = mp * 1.5`)
@@ -587,7 +587,7 @@ func TestNamedCaptureWithSpecialCharsInName(t *testing.T) {
expected := `<data value="84" min="10" max="100" />`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<data value="(?<val_1>\d+)"`,
`val_1 = val_1 * 2`)
@@ -602,7 +602,7 @@ func TestEmptyNamedCapture(t *testing.T) {
expected := `<tag attr="default" />`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`attr="(?<value>.*?)"`,
`value = value == "" and "default" or value`)
@@ -617,7 +617,7 @@ func TestMultipleNamedCapturesInSameLine(t *testing.T) {
expected := `<rect x="20" y="40" width="200" height="100" />`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`x="(?<x>\d+)" y="(?<y>\d+)" width="(?<w>\d+)" height="(?<h>\d+)"`,
`x = x * 2
y = y * 2
@@ -641,7 +641,7 @@ func TestConditionalNamedCapture(t *testing.T) {
<item status="inactive" count="10" />
`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<item status="(?<status>[^"]+)" count="(?<count>\d+)"`,
`count = status == "active" and count * 2 or count`)
@@ -662,7 +662,7 @@ func TestLuaFunctionsOnNamedCaptures(t *testing.T) {
<user name="JANE SMITH" role="admin" />
`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<user name="(?<name>[^"]+)" role="(?<role>[^"]+)"`,
`-- Capitalize first letters for regular users
if role == "user" then
@@ -692,7 +692,7 @@ func TestNamedCaptureWithMath(t *testing.T) {
<item price="19.99" quantity="3" total="59.97" />
`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<item price="(?<price>\d+\.\d+)" quantity="(?<qty>\d+)"!any$`,
`-- Calculate and add total
replacement = string.format('<item price="%s" quantity="%s" total="%.2f" />',
@@ -712,7 +712,7 @@ func TestNamedCaptureWithGlobals(t *testing.T) {
expected := `<temp unit="F">77</temp>`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<temp unit="(?<unit>[CF]?)">(?<value>\d+)</temp>`,
`if unit == "C" then
value = value * 9/5 + 32
@@ -739,7 +739,7 @@ func TestMixedDynamicAndNamedCaptures(t *testing.T) {
<color rgb="0,255,0" name="GREEN" hex="#00FF00" />
`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<color rgb="(?<r>\d+),(?<g>\d+),(?<b>\d+)" name="(?<colorName>[^"]+)" />`,
`-- Uppercase the name
colorName = string.upper(colorName)
@@ -765,7 +765,7 @@ func TestNamedCapturesWithMultipleReferences(t *testing.T) {
expected := `<text format="uppercase" length="11">HELLO WORLD</text>`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<text>(?<content>[^<]+)</text>`,
`local uppercaseContent = string.upper(content)
local contentLength = string.len(content)
@@ -783,7 +783,7 @@ func TestNamedCaptureWithJsonData(t *testing.T) {
expected := `<data>{"name":"JOHN","age":30}</data>`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<data>(?<json>\{.*?\})</data>`,
`-- Parse JSON (simplified, assumes valid JSON)
local name = json:match('"name":"([^"]+)"')
@@ -813,7 +813,7 @@ func TestNamedCaptureInXML(t *testing.T) {
</product>
`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`(?s)<price currency="(?<currency>[^"]+)">(?<price>\d+\.\d+)</price>.*?<stock>(?<stock>\d+)</stock>`,
`-- Add 20% to price if USD
if currency == "USD" then
@@ -870,7 +870,7 @@ func TestComprehensiveNamedCaptures(t *testing.T) {
</products>
`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`(?s)<product sku="(?<sku>[^"]+)" status="(?<status>[^"]+)"[^>]*>\s*<name>(?<product_name>[^<]+)</name>\s*<price currency="(?<currency>[^"]+)">(?<price>\d+\.\d+)</price>\s*<quantity>(?<qty>\d+)</quantity>`,
`-- Only process in-stock items
if status == "in-stock" then
@@ -924,7 +924,7 @@ func TestVariousNamedCaptureFormats(t *testing.T) {
</data>
`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`<entry id="(?<id_num>\d+)" value="(?<val>\d+)"(?: status="(?<status>[^"]*)")? />`,
`-- Prefix the ID with "ID-"
id_num = "ID-" .. id_num
@@ -963,7 +963,7 @@ func TestSimpleNamedCapture(t *testing.T) {
expected := `<product name="WIDGET" price="19.99"/>`
result, mods, matches, err := ApiAdaptor(content,
result, mods, matches, err := APIAdaptor(content,
`name="(?<product_name>[^"]+)"`,
`product_name = string.upper(product_name)`)