maps always return any
This commit is contained in:
parent
4e59fc6e5c
commit
516d66c2f2
99
wrapper.go
99
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 (
|
||||||
@ -408,18 +375,8 @@ func (s *State) ToTable(index int) (any, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mapType := int(C.sample_map_type(s.L, C.int(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)
|
return s.extractAnyMap(absIdx)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (s *State) extractIntArray(index, length int) []int {
|
func (s *State) extractIntArray(index, length int) []int {
|
||||||
result := make([]int, length)
|
result := make([]int, length)
|
||||||
@ -478,62 +435,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