Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unmarshal should based on type of golang #137

Merged
merged 9 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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)))

Check warning on line 102 in internal/luai/decode.go

View check run for this annotation

Codecov / codecov/patch

internal/luai/decode.go#L99-L102

Added lines #L99 - L102 were not covered by tests
}
}

Expand All @@ -106,6 +111,15 @@
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 @@
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 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 @@
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 @@
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 @@
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")

Check warning on line 109 in internal/luai/encode.go

View check run for this annotation

Codecov / codecov/patch

internal/luai/encode.go#L106-L109

Added lines #L106 - L109 were not covered by tests
}

}
return table, nil
default:
Expand Down
Loading
Loading