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

refactor: added alternative object converter func #16

Merged
merged 1 commit into from
Aug 22, 2022
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
95 changes: 95 additions & 0 deletions ugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,101 @@ func ToObject(v interface{}) (ret Object, err error) {
return
}

// ToObjectAlt is analogous to ToObject but it will always convert signed integers to
// Int and unsigned integers to Uint. It is an alternative to ToObject.
// Note that, this function is subject to change in the future.
func ToObjectAlt(v interface{}) (ret Object, err error) {
switch v := v.(type) {
case nil:
ret = Undefined
case string:
ret = String(v)
case bool:
if v {
ret = True
} else {
ret = False
}
case int:
ret = Int(v)
case int64:
ret = Int(v)
case uint64:
ret = Uint(v)
case float64:
ret = Float(v)
case float32:
ret = Float(v)
case int32:
ret = Int(v)
case int16:
ret = Int(v)
case int8:
ret = Int(v)
case uint:
ret = Uint(v)
case uint32:
ret = Uint(v)
case uint16:
ret = Uint(v)
case uint8:
ret = Uint(v)
case uintptr:
ret = Uint(v)
case []byte:
if v != nil {
ret = Bytes(v)
} else {
ret = Bytes{}
}
case map[string]interface{}:
m := make(Map, len(v))
for vk, vv := range v {
vo, err := ToObjectAlt(vv)
if err != nil {
return nil, err
}
m[vk] = vo
}
ret = m
case map[string]Object:
if v != nil {
ret = Map(v)
} else {
ret = Map{}
}
case []interface{}:
arr := make(Array, len(v))
for i, vv := range v {
obj, err := ToObjectAlt(vv)
if err != nil {
return nil, err
}
arr[i] = obj
}
ret = arr
case []Object:
if v != nil {
ret = Array(v)
} else {
ret = Array{}
}
case Object:
ret = v
case CallableFunc:
if v != nil {
ret = &Function{Value: v}
} else {
ret = Undefined
}
case error:
ret = &Error{Message: v.Error(), Cause: v}
default:
err = fmt.Errorf("cannot convert to object: %T", v)
}
return
}

// ToInterface tries to convert an Object o to an interface{} value.
func ToInterface(o Object) (ret interface{}) {
switch o := o.(type) {
Expand Down
65 changes: 65 additions & 0 deletions ugo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,68 @@ func TestToInterface(t *testing.T) {
})
}
}

func TestToObjectAlt(t *testing.T) {
err := errors.New("test error")
fn := func(...Object) (Object, error) { return nil, nil }

testCases := []struct {
iface interface{}
want Object
wantErr bool
}{
{iface: nil, want: Undefined},
{iface: "a", want: String("a")},
{iface: int64(-1), want: Int(-1)},
{iface: int32(-1), want: Int(-1)},
{iface: int16(-1), want: Int(-1)},
{iface: int8(-1), want: Int(-1)},
{iface: int(1), want: Int(1)},
{iface: uint(1), want: Uint(1)},
{iface: uint64(1), want: Uint(1)},
{iface: uint32(1), want: Uint(1)},
{iface: uint16(1), want: Uint(1)},
{iface: uint8(1), want: Uint(1)},
{iface: uintptr(1), want: Uint(1)},
{iface: true, want: True},
{iface: false, want: False},
{iface: rune(1), want: Int(1)},
{iface: byte(2), want: Uint(2)},
{iface: float64(1), want: Float(1)},
{iface: float32(1), want: Float(1)},
{iface: []byte(nil), want: Bytes{}},
{iface: []byte("a"), want: Bytes{'a'}},
{iface: map[string]Object(nil), want: Map{}},
{iface: map[string]Object{"a": Int(1)}, want: Map{"a": Int(1)}},
{iface: map[string]interface{}{"a": 1}, want: Map{"a": Int(1)}},
{iface: map[string]interface{}{"a": uint32(1)}, want: Map{"a": Uint(1)}},
{iface: []Object(nil), want: Array{}},
{iface: []Object{Int(1), Char('a')}, want: Array{Int(1), Char('a')}},
{iface: []interface{}{Int(1), Char('a')}, want: Array{Int(1), Char('a')}},
{iface: []interface{}{uint32(1)}, want: Array{Uint(1)}},
{iface: Object(nil), want: Undefined},
{iface: String("a"), want: String("a")},
{iface: CallableFunc(nil), want: Undefined},
{iface: fn, want: &Function{Value: fn}},
{iface: err, want: &Error{Message: err.Error(), Cause: err}},
{iface: error(nil), want: Undefined},
{iface: struct{}{}, wantErr: true},
}

for _, tC := range testCases {
t.Run(fmt.Sprintf("%[1]T:%[1]v", tC.iface), func(t *testing.T) {
got, err := ToObjectAlt(tC.iface)
if (err != nil) != tC.wantErr {
t.Errorf("ToObjectAlt() error = %v, wantErr %v", err, tC.wantErr)
return
}
if fn, ok := tC.iface.(CallableFunc); ok && fn != nil {
require.NotNil(t, tC.want.(*Function).Value)
return
}
if !reflect.DeepEqual(got, tC.want) {
t.Errorf("ToObjectAlt() = %[1]v (%[1]T), want %[2]v (%[2]T)", got, tC.want)
}
})
}
}