Try to include xml node children in lua table

This commit is contained in:
2025-03-26 02:50:28 +01:00
parent 1a4b4f76f2
commit 66a522aa12
2 changed files with 47 additions and 36 deletions

View File

@@ -43,10 +43,11 @@ func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr strin
} }
defer L.Close() defer L.Close()
err = p.ToLua(L, node) table, err := p.ToLua(L, node)
if err != nil { if err != nil {
return content, modCount, matchCount, fmt.Errorf("error converting to Lua: %v", err) return content, modCount, matchCount, fmt.Errorf("error converting to Lua: %v", err)
} }
L.SetGlobal("v", table)
err = L.DoString(BuildLuaScript(luaExpr)) err = L.DoString(BuildLuaScript(luaExpr))
if err != nil { if err != nil {
@@ -92,11 +93,11 @@ func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr strin
} }
// ToLua converts XML node values to Lua variables // ToLua converts XML node values to Lua variables
func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) error { func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) (lua.LValue, error) {
// Check if data is an xmlquery.Node // Check if data is an xmlquery.Node
node, ok := data.(*xmlquery.Node) node, ok := data.(*xmlquery.Node)
if !ok { if !ok {
return fmt.Errorf("expected xmlquery.Node, got %T", data) return nil, fmt.Errorf("expected xmlquery.Node, got %T", data)
} }
// Create a simple table with essential data // Create a simple table with essential data
@@ -107,17 +108,25 @@ func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) error {
L.SetField(table, "name", lua.LString(node.Data)) L.SetField(table, "name", lua.LString(node.Data))
L.SetField(table, "value", lua.LString(node.InnerText())) L.SetField(table, "value", lua.LString(node.InnerText()))
// Add attributes if any // Add children if any
children := L.NewTable()
for child := node.FirstChild; child != nil; child = child.NextSibling {
childTable, err := p.ToLua(L, child)
if err == nil {
children.Append(childTable)
}
}
L.SetField(table, "children", children)
attrs := L.NewTable()
if len(node.Attr) > 0 { if len(node.Attr) > 0 {
attrs := L.NewTable()
for _, attr := range node.Attr { for _, attr := range node.Attr {
L.SetField(attrs, attr.Name.Local, lua.LString(attr.Value)) L.SetField(attrs, attr.Name.Local, lua.LString(attr.Value))
} }
L.SetField(table, "attr", attrs)
} }
L.SetField(table, "attr", attrs)
L.SetGlobal("v", table) return table, nil
return nil
} }
// FromLua gets modified values from Lua // FromLua gets modified values from Lua

View File

@@ -1242,34 +1242,34 @@ func TestXMLProcessor_Process_TableBasedStructureCreation(t *testing.T) {
local summary = "" local summary = ""
-- Process each child option -- Process each child option
if v.settings and v.settings.option then local settings = v.children[1]
local options = v.settings.option local options = settings.children
-- If there's just one option, wrap it in a table -- if settings and options then
if options._attr then -- if options.attr then
options = {options} -- options = {options}
end -- end
--
for i, opt in ipairs(options) do -- for i, opt in ipairs(options) do
count = count + 1 -- count = count + 1
if opt._attr.name == "debug" then -- if opt.attr.name == "debug" then
summary = summary .. "Debug: " .. (opt._attr.value == "true" and "ON" or "OFF") -- summary = summary .. "Debug: " .. (opt.attr.value == "true" and "ON" or "OFF")
elseif opt._attr.name == "log_level" then -- elseif opt.attr.name == "log_level" then
summary = summary .. "Logging: " .. opt._attr.value -- summary = summary .. "Logging: " .. opt.attr.value
end -- end
--
if i < #options then -- if i < #options then
summary = summary .. ", " -- summary = summary .. ", "
end -- end
end -- end
end -- end
-- Create a new calculated section -- Create a new calculated section
v.calculated = { -- v.children[2] = {
stats = { -- stats = {
count = tostring(count), -- count = tostring(count),
summary = summary -- summary = summary
} -- }
} -- }
` `
result, modCount, matchCount, err := p.ProcessContent(content, "/data", luaExpr) result, modCount, matchCount, err := p.ProcessContent(content, "/data", luaExpr)
@@ -1555,10 +1555,11 @@ func TestXMLToLua(t *testing.T) {
} }
// Convert to Lua // Convert to Lua
err := processor.ToLua(L, root) table, err := processor.ToLua(L, root)
if err != nil { if err != nil {
t.Fatalf("Failed to convert to Lua: %v", err) t.Fatalf("Failed to convert to Lua: %v", err)
} }
L.SetGlobal("v", table)
// Verify the result // Verify the result
luaTable := L.GetGlobal("v") luaTable := L.GetGlobal("v")
@@ -1605,10 +1606,11 @@ func TestXMLToLua(t *testing.T) {
} }
// Convert to Lua // Convert to Lua
err := processor.ToLua(L, street) table, err := processor.ToLua(L, street)
if err != nil { if err != nil {
t.Fatalf("Failed to convert to Lua: %v", err) t.Fatalf("Failed to convert to Lua: %v", err)
} }
L.SetGlobal("v", table)
// Verify the result // Verify the result
luaTable := L.GetGlobal("v") luaTable := L.GetGlobal("v")