Skip to content

Commit 59516d4

Browse files
authored
removed tables and fixed arrays data structure (#10)
1 parent d38b998 commit 59516d4

6 files changed

Lines changed: 235 additions & 120 deletions

File tree

convert.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func ValueOf(v any) Value {
2828
return v
2929
case Table:
3030
return v
31-
case Tables:
31+
case Array:
3232
return v
3333
case int:
3434
return Number(v)
@@ -88,10 +88,8 @@ func ValueOf(v any) Value {
8888
return Strings(v)
8989
case map[string]any:
9090
return mapAsTable(v)
91-
case []map[string]any:
92-
return mapsAsTables(v)
9391
case []any:
94-
return asArray(v)
92+
return sliceAsArray(v)
9593
case nil:
9694
return Nil{}
9795
default:
@@ -109,7 +107,7 @@ func ValueOf(v any) Value {
109107
}
110108
}
111109

112-
// ValueOf converts a value to a LUA-friendly one.
110+
// resultOf converts a value to a LUA-friendly one.
113111
func resultOf(v lua.LValue) Value {
114112
switch v := v.(type) {
115113
case lua.LNumber:
@@ -119,20 +117,25 @@ func resultOf(v lua.LValue) Value {
119117
case lua.LBool:
120118
return Bool(v)
121119
case *lua.LTable:
120+
121+
// slice cases
122122
if top := v.RawGetInt(1); top != nil {
123123
switch top.Type() {
124-
case lua.LTNil:
125-
return asTable(v)
126124
case lua.LTNumber:
127125
return asNumbers(v)
128126
case lua.LTString:
129127
return asStrings(v)
130128
case lua.LTBool:
131129
return asBools(v)
132130
case lua.LTTable:
133-
return asTables(v)
131+
return asArrays(v)
134132
}
135133
}
134+
// map case
135+
tbl := asTable(v)
136+
if len(tbl) > 0 {
137+
return tbl
138+
}
136139
return Nil{}
137140
case *lua.LUserData:
138141
return ValueOf(v.Value)
@@ -163,21 +166,25 @@ func asBools(t *lua.LTable) (out Bools) {
163166
}
164167

165168
func asTable(t *lua.LTable) Table {
166-
out := make(Table, t.Len())
169+
out := make(Table)
167170
t.ForEach(func(k, v lua.LValue) {
168-
out[k.String()] = resultOf(v)
171+
if k.Type() == lua.LTString {
172+
out[k.String()] = resultOf(v)
173+
}
169174
})
170175
return out
171176
}
172177

173-
func asTables(t *lua.LTable) (out Tables) {
174-
t.ForEach(func(_, v lua.LValue) {
175-
out = append(out, asTable(v.(*lua.LTable)))
178+
func asArrays(t *lua.LTable) Array {
179+
out, index := make(Array, t.Len()), 0
180+
t.ForEach(func(_ lua.LValue, v lua.LValue) {
181+
out[index] = resultOf(v)
182+
index += 1
176183
})
177-
return
184+
return out
178185
}
179186

180-
func asArray(input []any) Array {
187+
func sliceAsArray(input []any) Array {
181188
arr := make(Array, 0, len(input))
182189
for _, v := range input {
183190
arr = append(arr, ValueOf(v))
@@ -193,14 +200,6 @@ func mapAsTable(input map[string]any) Table {
193200
return t
194201
}
195202

196-
func mapsAsTables(input []map[string]any) Tables {
197-
t := make(Tables, 0, len(input))
198-
for _, v := range input {
199-
t = append(t, mapAsTable(v))
200-
}
201-
return t
202-
}
203-
204203
// --------------------------------------------------------------------
205204

206205
// lvalueOf converts the script input into a valid lua value

convert_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/assert"
10+
lua "github.com/yuin/gopher-lua"
1011
)
1112

1213
func TestValueOf(t *testing.T) {
@@ -60,3 +61,57 @@ func TestValueOf(t *testing.T) {
6061
assert.NotEmpty(t, tc.output.String())
6162
}
6263
}
64+
65+
func TestResultOfArray(t *testing.T) {
66+
mp := []map[string]any{
67+
{"hello": []float64{1, 2, 3}},
68+
{"next": true},
69+
{"hello": "world"},
70+
}
71+
l := lua.NewState()
72+
val := lvalueOf(l, mp)
73+
res := resultOf(val)
74+
array, ok := res.(Array)
75+
assert.True(t, ok)
76+
assert.Len(t, array, 3)
77+
78+
resMp := array.Native().([]any)
79+
for i, val := range resMp {
80+
assert.Equal(t, mp[i], val.(map[string]any))
81+
}
82+
}
83+
84+
func TestResultComplexMap(t *testing.T) {
85+
mp := []map[string]any{
86+
{"map": []map[string]any{
87+
{"e": "f", "g": "h"}},
88+
},
89+
}
90+
l := lua.NewState()
91+
val := lvalueOf(l, mp)
92+
res := resultOf(val)
93+
array, ok := res.(Array)
94+
assert.True(t, ok)
95+
resMp := array.Native().([]any)
96+
assert.Len(t, resMp, len(mp))
97+
}
98+
99+
func TestResultOfMap(t *testing.T) {
100+
mp := map[string]any{
101+
"string": "aj",
102+
"numbers": []float64{1, 2, 3},
103+
"bool": true,
104+
}
105+
l := lua.NewState()
106+
val := lvalueOf(l, mp)
107+
res := resultOf(val)
108+
table, ok := res.(Table)
109+
assert.True(t, ok)
110+
assert.Len(t, table, 3)
111+
resMp, ok := table.Native().(map[string]any)
112+
assert.True(t, ok)
113+
114+
for _, key := range []string{"string", "numbers", "bool"} {
115+
assert.Equal(t, mp[key], resMp[key])
116+
}
117+
}

module.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ func (g *fngen) generate() lua.LGFunction {
103103
state.RaiseError(err.Interface().(error).Error())
104104
return 0
105105
}
106-
107106
state.Push(lvalueOf(state, out[0].Interface()))
108107
return 1
109108
}
@@ -190,7 +189,7 @@ func isValid(rt reflect.Type, at int) bool {
190189
case typeStrings:
191190
case typeBools:
192191
case typeTable:
193-
case typeTables:
192+
case typeArray:
194193
default:
195194
return false
196195
}

module_test.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func testModule() Module {
2828
must(m.Register("sleep", sleep))
2929
must(m.Register("joinMap", joinMap))
3030
must(m.Register("enrich", enrich))
31-
must(m.Register("batch", batch))
3231
must(m.Register("error", errorfunc))
3332
must(m.Register("error1", errorfunc1))
3433
return m
@@ -80,10 +79,6 @@ func enrich(name String, request Table) (Table, error) {
8079
return request, nil
8180
}
8281

83-
func batch(batch Tables) (Tables, error) {
84-
return batch, nil
85-
}
86-
8782
func Test_Join(t *testing.T) {
8883
s, err := newScript("fixtures/join.lua")
8984
assert.NoError(t, err)
@@ -180,25 +175,6 @@ func TestEnrich(t *testing.T) {
180175
}, out.(Table).Native())
181176
}
182177

183-
func TestBatch(t *testing.T) {
184-
s, err := newScript("fixtures/batch.lua")
185-
assert.NoError(t, err)
186-
187-
input := []map[string]any{
188-
{"A": "apples"}, {"B": "oranges"},
189-
}
190-
191-
out, err := s.Run(context.Background(), input)
192-
193-
assert.NoError(t, err)
194-
assert.Equal(t, TypeTables, out.Type())
195-
assert.EqualValues(t, input, out.(Tables).Native())
196-
assert.Equal(t, Tables{
197-
{"A": String("apples")},
198-
{"B": String("oranges")},
199-
}, out)
200-
}
201-
202178
func TestErrorMessage(t *testing.T) {
203179
s, err := newScript("fixtures/error.lua")
204180
assert.NoError(t, err)
@@ -238,23 +214,6 @@ func TestEnrichComplexTable(t *testing.T) {
238214
}, v)
239215
}
240216

241-
func TestEnrichComplexBatch(t *testing.T) {
242-
s, err := newScript("fixtures/batch.lua")
243-
assert.NoError(t, err)
244-
245-
v, err := s.Run(context.Background(), []map[string][]float64{{
246-
"A": {1, 2, 3},
247-
"B": {1, 2, 3},
248-
}})
249-
250-
assert.NoError(t, err)
251-
assert.NotNil(t, v)
252-
assert.Equal(t, Tables{{
253-
"A": Numbers{1, 2, 3},
254-
"B": Numbers{1, 2, 3},
255-
}}, v)
256-
}
257-
258217
func TestUserdata(t *testing.T) {
259218
s, err := FromString("sandbox", `
260219
function main(request)

value.go

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424
typeStrings = reflect.TypeOf(Strings(nil))
2525
typeBools = reflect.TypeOf(Bools(nil))
2626
typeTable = reflect.TypeOf(Table(nil))
27-
typeTables = reflect.TypeOf(Tables(nil))
27+
typeArray = reflect.TypeOf(Array(nil))
2828
)
2929

3030
var typeMap = map[reflect.Type]Type{
@@ -35,7 +35,7 @@ var typeMap = map[reflect.Type]Type{
3535
typeNumbers: TypeNumbers,
3636
typeBools: TypeBools,
3737
typeTable: TypeTable,
38-
typeTables: TypeTables,
38+
typeArray: TypeArray,
3939
}
4040

4141
// Type represents a type of the value
@@ -51,7 +51,6 @@ const (
5151
TypeNumbers
5252
TypeStrings
5353
TypeTable
54-
TypeTables
5554
TypeArray
5655
)
5756

@@ -314,41 +313,6 @@ func (v *Table) UnmarshalJSON(b []byte) error {
314313

315314
// --------------------------------------------------------------------
316315

317-
// Tables represents the array of tables
318-
type Tables []Table
319-
320-
// Type returns the type of the value
321-
func (v Tables) Type() Type {
322-
return TypeTables
323-
}
324-
325-
// String returns the string representation of the value
326-
func (v Tables) String() string {
327-
return fmt.Sprintf("%v", "(array of tables)")
328-
}
329-
330-
// Native returns value casted to native type
331-
func (v Tables) Native() any {
332-
var out []map[string]any
333-
for _, elem := range v {
334-
if tbl, ok := elem.Native().(map[string]any); ok {
335-
out = append(out, tbl)
336-
}
337-
}
338-
return out
339-
}
340-
341-
// lvalue converts the value to a LUA value
342-
func (v Tables) lvalue(state *lua.LState) lua.LValue {
343-
tbl := new(lua.LTable)
344-
for _, item := range v {
345-
tbl.Append(lvalueOf(state, item))
346-
}
347-
return tbl
348-
}
349-
350-
// --------------------------------------------------------------------
351-
352316
// Array represents the array of values
353317
type Array []Value
354318

@@ -364,11 +328,9 @@ func (v Array) String() string {
364328

365329
// Native returns value casted to native type
366330
func (v Array) Native() any {
367-
var out []map[string]any
368-
for _, elem := range v {
369-
if tbl, ok := elem.Native().(map[string]any); ok {
370-
out = append(out, tbl)
371-
}
331+
out := make([]any, len(v))
332+
for i, elem := range v {
333+
out[i] = elem.Native()
372334
}
373335
return out
374336
}

0 commit comments

Comments
 (0)