Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
14009697f0 | |||
f47d36eb8b | |||
516d66c2f2 |
2
DOCS.md
2
DOCS.md
@ -109,7 +109,7 @@ if err != nil {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### ToTable(index int) (any, error)
|
### ToTable(index int) (any, error)
|
||||||
Converts a Lua table to optimal Go type ([]int, []string, map[string]any, etc.).
|
Converts a Lua table to optimal Go type; arrays or `map[string]any`.
|
||||||
```go
|
```go
|
||||||
table, err := L.ToTable(-1)
|
table, err := L.ToTable(-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -116,7 +116,7 @@ L.PushValue(stuff) // Handles all Go types automatically
|
|||||||
|
|
||||||
// Lua → Go with automatic type detection
|
// Lua → Go with automatic type detection
|
||||||
L.GetGlobal("some_table")
|
L.GetGlobal("some_table")
|
||||||
result, err := L.ToTable(-1) // Returns optimal Go type ([]int, map[string]string, etc.)
|
result, err := L.ToTable(-1) // Returns optimal Go type (typed array, or map[string]any)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Table Builder
|
### Table Builder
|
||||||
|
103
wrapper.go
103
wrapper.go
@ -71,39 +71,6 @@ static int sample_array_type(lua_State *L, int index, int count) {
|
|||||||
if (all_bools) return 4;
|
if (all_bools) return 4;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sample_map_type(lua_State *L, int index) {
|
|
||||||
int all_string_vals = 1;
|
|
||||||
int all_int_vals = 1;
|
|
||||||
int all_int_keys = 1;
|
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
lua_pushnil(L);
|
|
||||||
while (lua_next(L, index) && count < 5) {
|
|
||||||
if (lua_type(L, -2) != LUA_TSTRING) {
|
|
||||||
all_int_keys = 0;
|
|
||||||
} else {
|
|
||||||
const char *key = lua_tostring(L, -2);
|
|
||||||
char *endptr;
|
|
||||||
strtol(key, &endptr, 10);
|
|
||||||
if (*endptr != '\0') all_int_keys = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int val_type = lua_type(L, -1);
|
|
||||||
if (val_type != LUA_TSTRING) all_string_vals = 0;
|
|
||||||
if (val_type != LUA_TNUMBER || !is_integer(L, -1)) all_int_vals = 0;
|
|
||||||
|
|
||||||
lua_pop(L, 1);
|
|
||||||
count++;
|
|
||||||
|
|
||||||
if (!all_string_vals && !all_int_vals && !all_int_keys) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (all_int_keys) return 4;
|
|
||||||
if (all_string_vals) return 1;
|
|
||||||
if (all_int_vals) return 2;
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
@ -239,6 +206,8 @@ func (s *State) PushValue(v any) error {
|
|||||||
s.PushNumber(val)
|
s.PushNumber(val)
|
||||||
case string:
|
case string:
|
||||||
s.PushString(val)
|
s.PushString(val)
|
||||||
|
case []byte:
|
||||||
|
s.PushString(string(val))
|
||||||
case []int:
|
case []int:
|
||||||
return s.pushIntSlice(val)
|
return s.pushIntSlice(val)
|
||||||
case []string:
|
case []string:
|
||||||
@ -408,17 +377,7 @@ func (s *State) ToTable(index int) (any, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mapType := int(C.sample_map_type(s.L, C.int(absIdx)))
|
return s.extractAnyMap(absIdx)
|
||||||
switch mapType {
|
|
||||||
case 1: // map[string]string
|
|
||||||
return s.extractStringMap(absIdx)
|
|
||||||
case 2: // map[string]int
|
|
||||||
return s.extractIntMap(absIdx)
|
|
||||||
case 4: // map[int]any
|
|
||||||
return s.extractIntKeyMap(absIdx)
|
|
||||||
default: // map[string]any
|
|
||||||
return s.extractAnyMap(absIdx)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) extractIntArray(index, length int) []int {
|
func (s *State) extractIntArray(index, length int) []int {
|
||||||
@ -478,62 +437,6 @@ func (s *State) extractAnyArray(index, length int) []any {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) extractStringMap(index int) (map[string]string, error) {
|
|
||||||
result := make(map[string]string)
|
|
||||||
s.PushNil()
|
|
||||||
for s.Next(index) {
|
|
||||||
if s.GetType(-2) == TypeString {
|
|
||||||
key := s.ToString(-2)
|
|
||||||
value := s.ToString(-1)
|
|
||||||
result[key] = value
|
|
||||||
}
|
|
||||||
s.Pop(1)
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *State) extractIntMap(index int) (map[string]int, error) {
|
|
||||||
result := make(map[string]int)
|
|
||||||
s.PushNil()
|
|
||||||
for s.Next(index) {
|
|
||||||
if s.GetType(-2) == TypeString {
|
|
||||||
key := s.ToString(-2)
|
|
||||||
value := int(s.ToNumber(-1))
|
|
||||||
result[key] = value
|
|
||||||
}
|
|
||||||
s.Pop(1)
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *State) extractIntKeyMap(index int) (map[int]any, error) {
|
|
||||||
result := make(map[int]any)
|
|
||||||
s.PushNil()
|
|
||||||
for s.Next(index) {
|
|
||||||
var key int
|
|
||||||
switch s.GetType(-2) {
|
|
||||||
case TypeString:
|
|
||||||
if k, err := strconv.Atoi(s.ToString(-2)); err == nil {
|
|
||||||
key = k
|
|
||||||
} else {
|
|
||||||
s.Pop(1)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
case TypeNumber:
|
|
||||||
key = int(s.ToNumber(-2))
|
|
||||||
default:
|
|
||||||
s.Pop(1)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if value, err := s.ToValue(-1); err == nil {
|
|
||||||
result[key] = value
|
|
||||||
}
|
|
||||||
s.Pop(1)
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *State) extractAnyMap(index int) (map[string]any, error) {
|
func (s *State) extractAnyMap(index int) (map[string]any, error) {
|
||||||
result := make(map[string]any)
|
result := make(map[string]any)
|
||||||
s.PushNil()
|
s.PushNil()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user