1
0
forked from MTSR/mapserver
mapserver/server/luaparser/luaparser.go

59 lines
972 B
Go
Raw Normal View History

2019-02-08 15:24:54 +03:00
package luaparser
import (
2019-02-08 18:02:24 +03:00
"errors"
"github.com/yuin/gopher-lua"
"strconv"
2019-02-08 15:24:54 +03:00
)
func New() *LuaParser {
2019-02-08 18:02:24 +03:00
p := LuaParser{
state: lua.NewState(lua.Options{SkipOpenLibs: true}),
}
2019-02-08 15:24:54 +03:00
2019-02-08 18:02:24 +03:00
return &p
2019-02-08 15:24:54 +03:00
}
type LuaParser struct {
2019-02-08 18:02:24 +03:00
state *lua.LState
2019-02-08 15:24:54 +03:00
}
func (this *LuaParser) ParseMap(expr string) (map[string]interface{}, error) {
2019-02-08 18:02:24 +03:00
result := make(map[string]interface{})
err := this.state.DoString(expr)
if err != nil {
return result, err
}
lv := this.state.Get(-1)
tbl, ok := lv.(*lua.LTable)
if !ok {
return result, errors.New("parsing failed")
}
tbl.ForEach(func(k, v lua.LValue) {
key, ok := k.(lua.LString)
if !ok {
return
}
boolValue, ok := v.(lua.LBool)
if ok {
result[key.String()] = boolValue == lua.LTrue
}
intValue, ok := v.(lua.LNumber)
if ok {
result[key.String()], _ = strconv.Atoi(intValue.String())
}
strValue, ok := v.(lua.LString)
if ok {
result[key.String()] = strValue.String()
}
})
return result, nil
2019-02-08 15:24:54 +03:00
}