-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
70 lines (64 loc) · 1.63 KB
/
helper.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
package main
/*
#cgo LDFLAGS: -L/usr/local/lib -llua
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdlib.h>
*/
import "C"
import (
"unsafe"
)
// go_to_lua converts a arbitrary go value into a Lua value pushed onto the
// stack, it's main purpose here is to convert a generic unmarshaled JSON
// value into a Lua value
func go_to_lua(L *C.lua_State, data interface{}) {
if d, ok := data.(map[string]interface{}); ok == true {
if len(d) > 0 {
C.lua_createtable(L, 0, 0)
for k, v := range d {
key := C.CString(k)
defer C.free(unsafe.Pointer(key))
C.lua_pushstring(L, key)
go_to_lua(L, v)
C.lua_settable(L, -3)
}
} else {
C.lua_pushnil(L)
}
} else if d, ok := data.(map[int]interface{}); ok == true {
if len(d) > 0 {
C.lua_createtable(L, 0, 0)
for k, v := range d {
C.lua_pushinteger(L, C.longlong(k))
go_to_lua(L, v)
C.lua_settable(L, -3)
}
} else {
C.lua_pushnil(L)
}
} else if val, ok := data.(string); ok == true {
value := C.CString(val)
defer C.free(unsafe.Pointer(value))
C.lua_pushstring(L, value)
} else if val, ok := data.(int64); ok == true {
C.lua_pushinteger(L, C.longlong(val))
} else if val, ok := data.(float64); ok == true {
C.lua_pushnumber(L, C.double(val))
} else if val, ok := data.(bool); ok == true {
if val == true {
C.lua_pushboolean(L, 1)
} else {
C.lua_pushboolean(L, 0)
}
} else if val, ok := data.(map[string]interface{}); ok == true {
go_to_lua(L, val)
} else if val, ok := data.(map[int]interface{}); ok == true {
go_to_lua(L, val)
} else if data == nil {
C.lua_pushnil(L)
} else {
C.lua_pushnil(L)
}
}