-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtypes.go
135 lines (116 loc) · 2.57 KB
/
types.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package lua
import (
"errors"
"io"
"net/http"
"sort"
"strconv"
"sync"
glua "github.com/yuin/gopher-lua"
)
type Table struct {
Data map[string]interface{}
}
type List struct {
Data []interface{}
}
type HttpResponse struct {
Once *sync.Once
R *http.Response
body string
}
func (h *HttpResponse) Close() {
if h == nil || h.R == nil || h.R.Body == nil {
return
}
h.R.Body.Close()
h.R.Body = nil
}
func (h *HttpResponse) Body() string {
h.Once.Do(func() {
b, _ := io.ReadAll(h.R.Body)
h.Close()
h.body = string(b)
})
return h.body
}
func (h *HttpResponse) Header(k string) string {
return h.R.Header.Get(k)
}
func (h *HttpResponse) Headers(k string) []string {
return h.R.Header.Values(k)
}
type NativeValue = glua.LValue
type NativeNumber = glua.LNumber
type NativeString = glua.LString
type NativeBool = glua.LBool
type NativeUserData = glua.LUserData
type NativeTable = glua.LTable
func ParseToTable(k, v NativeValue, acc map[string]interface{}) {
switch v.Type() {
case glua.LTString:
acc[k.String()] = v.String()
case glua.LTBool:
acc[k.String()] = glua.LVAsBool(v)
case glua.LTNumber:
f := float64(v.(NativeNumber))
if f == float64(int64(v.(NativeNumber))) {
acc[k.String()] = int(v.(NativeNumber))
} else {
acc[k.String()] = f
}
case glua.LTUserData:
userV := v.(*NativeUserData)
if userV.Value == nil {
acc[k.String()] = nil
} else {
switch v := userV.Value.(type) {
case *Table:
acc[k.String()] = v.Data
case *List:
acc[k.String()] = v.Data
}
}
case glua.LTTable:
res := map[string]interface{}{}
v.(*NativeTable).ForEach(func(k, v NativeValue) {
ParseToTable(k, v, res)
})
// Check if all the keys are integers and convert to slice
acc[k.String()] = res
t, err := tryConvertToSlice(res)
if err == nil {
acc[k.String()] = t
}
}
}
func MapNativeTable(t *NativeTable) (interface{}, bool) {
res := map[string]interface{}{}
t.ForEach(func(k, v NativeValue) {
ParseToTable(k, v, res)
})
// Check if all the keys are integers and convert to slice
at, err := tryConvertToSlice(res)
if err == nil {
return at, true
}
return res, false
}
func tryConvertToSlice(input map[string]interface{}) ([]interface{}, error) {
keys := make([]int, 0, len(input))
values := map[int]interface{}{}
for k, v := range input {
ik, err := strconv.Atoi(k)
if err != nil {
return nil, errors.New("non-integer key, cannot convert")
}
keys = append(keys, ik)
values[ik] = v
}
sort.Ints(keys)
var result []interface{}
for _, k := range keys {
result = append(result, values[k])
}
return result, nil
}