-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathencoder.go
64 lines (57 loc) · 1.4 KB
/
encoder.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
package yaml
import (
"github.com/vadv/gopher-lua-libs/io"
lua "github.com/yuin/gopher-lua"
"gopkg.in/yaml.v2"
)
const (
yamlEncoderType = "yaml.Encoder"
)
func CheckYAMLEncoder(L *lua.LState, n int) *yaml.Encoder {
ud := L.CheckUserData(n)
if encoder, ok := ud.Value.(*yaml.Encoder); ok {
return encoder
}
L.ArgError(n, yamlEncoderType+" expected")
return nil
}
func LVYAMLEncoder(L *lua.LState, encoder *yaml.Encoder) lua.LValue {
ud := L.NewUserData()
ud.Value = encoder
L.SetMetatable(ud, L.GetTypeMetatable(yamlEncoderType))
return ud
}
func yamlEncoderEncode(L *lua.LState) int {
encoder := CheckYAMLEncoder(L, 1)
arg := L.CheckAny(2)
L.Pop(L.GetTop())
var value interface{}
err := L.GPCall(func(L *lua.LState) int {
visited := make(map[*lua.LTable]bool)
value = toYAML(L, visited, arg)
return 0
}, lua.LNil)
if err != nil {
L.Push(lua.LString(err.Error()))
return 1
}
if err = encoder.Encode(value); err != nil {
L.Push(lua.LString(err.Error()))
return 1
}
return 0
}
func registerYAMLEncoder(L *lua.LState) {
mt := L.NewTypeMetatable(yamlEncoderType)
L.SetGlobal(yamlEncoderType, mt)
L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
"encode": yamlEncoderEncode,
}))
}
func newYAMLEncoder(L *lua.LState) int {
writer := io.CheckIOWriter(L, 1)
L.Pop(L.GetTop())
encoder := yaml.NewEncoder(writer)
L.Push(LVYAMLEncoder(L, encoder))
return 1
}