Fix reading lua variables
This commit is contained in:
@@ -81,33 +81,13 @@ func (p *RegexProcessor) FromLua(L *lua.LState) (interface{}, error) {
|
|||||||
sLuaVal := L.GetGlobal(sVarName)
|
sLuaVal := L.GetGlobal(sVarName)
|
||||||
|
|
||||||
// First check string variables (s1, s2, etc.) as they should have priority
|
// First check string variables (s1, s2, etc.) as they should have priority
|
||||||
if sLuaVal != lua.LNil {
|
if sLuaVal.Type() != lua.LTNil {
|
||||||
if sStr, ok := sLuaVal.(lua.LString); ok {
|
modifications[i] = sLuaVal.String()
|
||||||
newStrVal := string(sStr)
|
|
||||||
modifications[i] = newStrVal
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then check numeric variables (v1, v2, etc.)
|
// Then check numeric variables (v1, v2, etc.)
|
||||||
if vLuaVal != lua.LNil {
|
if vLuaVal.Type() != lua.LTNil {
|
||||||
switch v := vLuaVal.(type) {
|
modifications[i] = vLuaVal.String()
|
||||||
case lua.LNumber:
|
|
||||||
// Convert numeric value to string
|
|
||||||
newNumVal := strconv.FormatFloat(float64(v), 'f', -1, 64)
|
|
||||||
modifications[i] = newNumVal
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,6 +106,10 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
|||||||
return "", 0, 0, fmt.Errorf("error compiling pattern: %v", err)
|
return "", 0, 0, fmt.Errorf("error compiling pattern: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previous := luaExpr
|
||||||
|
luaExpr = BuildLuaScript(luaExpr)
|
||||||
|
fmt.Printf("Changing Lua expression from: %s to: %s\n", previous, luaExpr)
|
||||||
|
|
||||||
L := lua.NewState()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
@@ -151,28 +135,32 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
|||||||
captures := compiledPattern.FindStringSubmatch(match)
|
captures := compiledPattern.FindStringSubmatch(match)
|
||||||
if len(captures) <= 1 {
|
if len(captures) <= 1 {
|
||||||
// No capture groups, return unchanged
|
// No capture groups, return unchanged
|
||||||
|
fmt.Println("No capture groups for lua to chew on")
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pass the captures to Lua environment
|
|
||||||
if err := p.ToLua(L, captures); err != nil {
|
if err := p.ToLua(L, captures); err != nil {
|
||||||
|
fmt.Println("Error setting Lua variables:", err)
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the user's Lua code
|
// Execute the user's Lua code
|
||||||
if err := L.DoString(luaExpr); err != nil {
|
if err := L.DoString(luaExpr); err != nil {
|
||||||
|
fmt.Println("Error executing Lua code:", err)
|
||||||
return match // Return unchanged on error
|
return match // Return unchanged on error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get modifications from Lua
|
// Get modifications from Lua
|
||||||
modResult, err := p.FromLua(L)
|
modResult, err := p.FromLua(L)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("Error getting modifications:", err)
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply modifications to the matched text
|
// Apply modifications to the matched text
|
||||||
modsMap, ok := modResult.(map[int]string)
|
modsMap, ok := modResult.(map[int]string)
|
||||||
if !ok || len(modsMap) == 0 {
|
if !ok || len(modsMap) == 0 {
|
||||||
|
fmt.Println("No modifications to apply")
|
||||||
return match // No changes
|
return match // No changes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user