Skip to content

Commit

Permalink
Limit the amount of supported types
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Aug 7, 2024
1 parent 7909cc7 commit 46aae28
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions typecast.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "fmt"

// TODO: make sure all supported types covered in functions.
type SupportedTypes interface {
bool | string | int | int16 | int32 | int64 | float32 | float64 | *Array | *Hash
bool | string | int | float32 | float64 | *Array | *Hash
}

func ToGo[T SupportedTypes](value Value) T {
Expand All @@ -19,9 +19,11 @@ func ToGo[T SupportedTypes](value Value) T {
case string:
str := C.mrb_obj_as_string(value.GRuby().state, value.CValue())
result = C.GoString(C._go_RSTRING_PTR(str))
case int, int16, int32, int64:
case int:
result = int(C._go_mrb_fixnum(value.CValue()))
case float64, float32:
case float32:
result = float32(C._go_mrb_float(value.CValue()))
case float64:
result = float64(C._go_mrb_float(value.CValue()))
case *Array:
result = &Array{value}
Expand All @@ -45,10 +47,12 @@ func ToRuby[T SupportedTypes](grb *GRuby, value T) Value {
cstr := C.CString(tVal)
defer freeStr(cstr)
return grb.value(C.mrb_str_new_cstr(grb.state, cstr))
case int, int16, int32, int64:
return grb.value(C.mrb_fixnum_value(C.mrb_int(tVal.(int)))) //nolint:forcetypeassert
case float64, float32:
return grb.value(C.mrb_float_value(grb.state, C.mrb_float(C.long(tVal.(float32))))) //nolint:forcetypeassert
case int:
return grb.value(C.mrb_fixnum_value(C.mrb_int(tVal)))
case float32:
return grb.value(C.mrb_float_value(grb.state, C.mrb_float(C.long(tVal))))
case float64:
return grb.value(C.mrb_float_value(grb.state, C.mrb_float(C.long(tVal))))
// TODO: generic array and hash support
case []string:
ary := NewArray(grb)
Expand Down

0 comments on commit 46aae28

Please sign in to comment.