Skip to content

Commit

Permalink
fix: unmarshal should based on type of golang (#137)
Browse files Browse the repository at this point in the history
* fix: unmarshal should based on type of golang

* fix: encoding map[int] will cause error

* chore: update comment

* test: update testcase

* test: update testcase

* Update internal/luai/example_test.go

* test: fix testcases

* test: fix testcases

---------

Co-authored-by: Han Li <[email protected]>
  • Loading branch information
bytemain and aooohan authored Mar 25, 2024
1 parent f8de0c2 commit 44443bf
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 323 deletions.
33 changes: 24 additions & 9 deletions internal/luai/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,18 @@ func indirect(v reflect.Value) reflect.Value {

func storeLiteral(value reflect.Value, lvalue lua.LValue) {
value = indirect(value)
switch lvalue.Type() {
case lua.LTString:

switch value.Kind() {
case reflect.String:
value.SetString(lvalue.String())
case lua.LTNumber:
value.SetInt(int64(lvalue.(lua.LNumber)))
case lua.LTBool:
case reflect.Bool:
value.SetBool(bool(lvalue.(lua.LBool)))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value.SetInt(int64(lvalue.(lua.LNumber)))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
value.SetUint(uint64(lvalue.(lua.LNumber)))
case reflect.Float32, reflect.Float64:
value.SetFloat(float64(lvalue.(lua.LNumber)))
}
}

Expand All @@ -106,6 +111,15 @@ func objectInterface(lvalue *lua.LTable) any {
return v
}

// To unmarshal lua obj into an interface value,
// Unmarshal stores one of these in the interface value:
//
// - bool, for LTBool
// - float64, for LTNumber
// - string, for LTString
// - []interface{}, for LTTable arrays
// - map[string]interface{}, for LTTable objects
// - nil for LTNil
func valueInterface(lvalue lua.LValue) any {
switch lvalue.Type() {
case lua.LTTable:
Expand All @@ -117,7 +131,7 @@ func valueInterface(lvalue lua.LValue) any {
case lua.LTString:
return lvalue.String()
case lua.LTNumber:
return int(lvalue.(lua.LNumber))
return float64(lvalue.(lua.LNumber))
case lua.LTBool:
return bool(lvalue.(lua.LBool))
}
Expand All @@ -134,11 +148,10 @@ func arrayInterface(lvalue *lua.LTable) any {
}

func unmarshalWorker(value lua.LValue, reflected reflect.Value) error {
reflected = indirect(reflected)

switch value.Type() {
case lua.LTTable:
reflected = indirect(reflected)
tagMap := make(map[string]int)

switch reflected.Kind() {
case reflect.Interface:
Expand All @@ -147,7 +160,7 @@ func unmarshalWorker(value lua.LValue, reflected reflect.Value) error {
result := valueInterface(value)
reflected.Set(reflect.ValueOf(result))
}
// map[T1]T2 where T1 is string, an integer type
// map[T1]T2 where T1 is string or an integer type
case reflect.Map:
t := reflected.Type()
keyType := t.Key()
Expand Down Expand Up @@ -239,6 +252,8 @@ func unmarshalWorker(value lua.LValue, reflected reflect.Value) error {
reflected.Set(reflect.MakeSlice(reflected.Type(), 0, 0))
}
case reflect.Struct:
tagMap := make(map[string]int)

for i := 0; i < reflected.NumField(); i++ {
fieldTypeField := reflected.Type().Field(i)
tag := fieldTypeField.Tag.Get("luai")
Expand Down
12 changes: 11 additions & 1 deletion internal/luai/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ func Marshal(state *lua.LState, v any) (lua.LValue, error) {
return nil, err
}

table.RawSetString(key.String(), value)
switch key.Kind() {
case reflect.String:
table.RawSetString(key.String(), value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
table.RawSetInt(int(key.Int()), value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
table.RawSetInt(int(key.Uint()), value)
default:
return nil, errors.New("marshal: unsupported type " + key.Kind().String() + " for key")
}

}
return table, nil
default:
Expand Down
Loading

0 comments on commit 44443bf

Please sign in to comment.