forked from vadv/gopher-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
65 lines (57 loc) · 1.43 KB
/
decoder.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
package yaml
import (
"github.com/alexjx/gopher-lua-libs/io"
lua "github.com/yuin/gopher-lua"
"gopkg.in/yaml.v2"
)
const (
yamlDecoderType = "yaml.Decoder"
)
func CheckYAMLDecoder(L *lua.LState, n int) *yaml.Decoder {
ud := L.CheckUserData(n)
if decoder, ok := ud.Value.(*yaml.Decoder); ok {
return decoder
}
L.ArgError(n, yamlDecoderType+" expected")
return nil
}
func LVYAMLDecoder(L *lua.LState, decoder *yaml.Decoder) lua.LValue {
ud := L.NewUserData()
ud.Value = decoder
L.SetMetatable(ud, L.GetTypeMetatable(yamlDecoderType))
return ud
}
func yamlDecoderDecode(L *lua.LState) int {
decoder := CheckYAMLDecoder(L, 1)
L.Pop(L.GetTop())
var value interface{}
if err := decoder.Decode(&value); err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(fromYAML(L, value))
return 1
}
func yamlDecoderSetStrict(L *lua.LState) int {
decoder := CheckYAMLDecoder(L, 1)
strict := L.CheckBool(2)
L.Pop(L.GetTop())
decoder.SetStrict(strict)
return 0
}
func registerYAMLDecoder(L *lua.LState) {
mt := L.NewTypeMetatable(yamlDecoderType)
L.SetGlobal(yamlDecoderType, mt)
L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
"decode": yamlDecoderDecode,
"set_strict": yamlDecoderSetStrict,
}))
}
func newYAMLDecoder(L *lua.LState) int {
reader := io.CheckIOReader(L, 1)
L.Pop(L.GetTop())
decoder := yaml.NewDecoder(reader)
L.Push(LVYAMLDecoder(L, decoder))
return 1
}