-
Notifications
You must be signed in to change notification settings - Fork 23
/
gluamapper.go
108 lines (96 loc) · 2.73 KB
/
gluamapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// gluamapper provides an easy way to map GopherLua tables to Go structs.
package gluamapper
import (
"errors"
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/yuin/gopher-lua"
"regexp"
"strings"
)
// Option is a configuration that is used to create a new mapper.
type Option struct {
// Function to convert a lua table key to Go's one. This defaults to "ToUpperCamelCase".
NameFunc func(string) string
// Returns error if unused keys exist.
ErrorUnused bool
// A struct tag name for lua table keys . This defaults to "gluamapper"
TagName string
}
// Mapper maps a lua table to a Go struct pointer.
type Mapper struct {
Option Option
}
// NewMapper returns a new mapper.
func NewMapper(opt Option) *Mapper {
if opt.NameFunc == nil {
opt.NameFunc = ToUpperCamelCase
}
if opt.TagName == "" {
opt.TagName = "gluamapper"
}
return &Mapper{opt}
}
// Map maps the lua table to the given struct pointer.
func (mapper *Mapper) Map(tbl *lua.LTable, st interface{}) error {
opt := mapper.Option
mp, ok := ToGoValue(tbl, opt).(map[interface{}]interface{})
if !ok {
return errors.New("arguments #1 must be a table, but got an array")
}
config := &mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: st,
TagName: opt.TagName,
ErrorUnused: opt.ErrorUnused,
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(mp)
}
// Map maps the lua table to the given struct pointer with default options.
func Map(tbl *lua.LTable, st interface{}) error {
return NewMapper(Option{}).Map(tbl, st)
}
// Id is an Option.NameFunc that returns given string as-is.
func Id(s string) string {
return s
}
var camelre = regexp.MustCompile(`_([a-z])`)
// ToUpperCamelCase is an Option.NameFunc that converts strings from snake case to upper camel case.
func ToUpperCamelCase(s string) string {
return strings.ToUpper(string(s[0])) + camelre.ReplaceAllStringFunc(s[1:len(s)], func(s string) string { return strings.ToUpper(s[1:len(s)]) })
}
// ToGoValue converts the given LValue to a Go object.
func ToGoValue(lv lua.LValue, opt Option) interface{} {
switch v := lv.(type) {
case *lua.LNilType:
return nil
case lua.LBool:
return bool(v)
case lua.LString:
return string(v)
case lua.LNumber:
return float64(v)
case *lua.LTable:
maxn := v.MaxN()
if maxn == 0 { // table
ret := make(map[interface{}]interface{})
v.ForEach(func(key, value lua.LValue) {
keystr := fmt.Sprint(ToGoValue(key, opt))
ret[opt.NameFunc(keystr)] = ToGoValue(value, opt)
})
return ret
} else { // array
ret := make([]interface{}, 0, maxn)
for i := 1; i <= maxn; i++ {
ret = append(ret, ToGoValue(v.RawGetInt(i), opt))
}
return ret
}
default:
return v
}
}