Skip to content

Commit

Permalink
refactor: Extract func coerceValue from set and remove func set
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith committed Sep 8, 2021
1 parent 1c2cf24 commit 711901e
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ func (o *Object) MethodCall(methodName string, args ...Valuer) (*Value, error) {
return getValue(fn.ctx, rtn), getError(rtn)
}

func coerceValue(iso *Isolate, val interface{}) (*Value, error) {
switch v := val.(type) {
case string, int32, uint32, int64, uint64, float64, bool, *big.Int:
// ignoring error as code cannot reach the error state as we are already
// validating the new value types in this case statement
value, _ := NewValue(iso, v)
return value, nil
case Valuer:
return v.value(), nil
default:
return nil, fmt.Errorf("v8go: unsupported object property type `%T`", v)
}
}

// Set will set a property on the Object to a given value.
// Supports all value types, eg: Object, Array, Date, Set, Map etc
// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int)
Expand All @@ -55,35 +69,26 @@ func (o *Object) Set(key string, val interface{}) error {
if len(key) == 0 {
return errors.New("v8go: You must provide a valid property key")
}
return set(o, key, 0, val)

value, err := coerceValue(o.ctx.iso, val)
if err != nil {
return err
}

ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
C.ObjectSet(o.ptr, ckey, value.ptr)
return nil
}

// Set will set a given index on the Object to a given value.
// Supports all value types, eg: Object, Array, Date, Set, Map etc
// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int)
// then a *Value will be created and set as the value property.
func (o *Object) SetIdx(idx uint32, val interface{}) error {
return set(o, "", idx, val)
}

func set(o *Object, key string, idx uint32, val interface{}) error {
var value *Value
switch v := val.(type) {
case string, int32, uint32, int64, uint64, float64, bool, *big.Int:
// ignoring error as code cannot reach the error state as we are already
// validating the new value types in this case statement
value, _ = NewValue(o.ctx.iso, v)
case Valuer:
value = v.value()
default:
return fmt.Errorf("v8go: unsupported object property type `%T`", v)
}

if len(key) > 0 {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
C.ObjectSet(o.ptr, ckey, value.ptr)
return nil
value, err := coerceValue(o.ctx.iso, val)
if err != nil {
return err
}

C.ObjectSetIdx(o.ptr, C.uint32_t(idx), value.ptr)
Expand Down

0 comments on commit 711901e

Please sign in to comment.