Skip to content

Commit 9d9805c

Browse files
authored
refactor: Extract func coerceValue from set and remove func set (#149)
1 parent 8d15ff3 commit 9d9805c

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

object.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ func (o *Object) MethodCall(methodName string, args ...Valuer) (*Value, error) {
3535
return fn.Call(o, args...)
3636
}
3737

38+
func coerceValue(iso *Isolate, val interface{}) (*Value, error) {
39+
switch v := val.(type) {
40+
case string, int32, uint32, int64, uint64, float64, bool, *big.Int:
41+
// ignoring error as code cannot reach the error state as we are already
42+
// validating the new value types in this case statement
43+
value, _ := NewValue(iso, v)
44+
return value, nil
45+
case Valuer:
46+
return v.value(), nil
47+
default:
48+
return nil, fmt.Errorf("v8go: unsupported object property type `%T`", v)
49+
}
50+
}
51+
3852
// Set will set a property on the Object to a given value.
3953
// Supports all value types, eg: Object, Array, Date, Set, Map etc
4054
// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int)
@@ -43,35 +57,26 @@ func (o *Object) Set(key string, val interface{}) error {
4357
if len(key) == 0 {
4458
return errors.New("v8go: You must provide a valid property key")
4559
}
46-
return set(o, key, 0, val)
60+
61+
value, err := coerceValue(o.ctx.iso, val)
62+
if err != nil {
63+
return err
64+
}
65+
66+
ckey := C.CString(key)
67+
defer C.free(unsafe.Pointer(ckey))
68+
C.ObjectSet(o.ptr, ckey, value.ptr)
69+
return nil
4770
}
4871

4972
// Set will set a given index on the Object to a given value.
5073
// Supports all value types, eg: Object, Array, Date, Set, Map etc
5174
// If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int)
5275
// then a *Value will be created and set as the value property.
5376
func (o *Object) SetIdx(idx uint32, val interface{}) error {
54-
return set(o, "", idx, val)
55-
}
56-
57-
func set(o *Object, key string, idx uint32, val interface{}) error {
58-
var value *Value
59-
switch v := val.(type) {
60-
case string, int32, uint32, int64, uint64, float64, bool, *big.Int:
61-
// ignoring error as code cannot reach the error state as we are already
62-
// validating the new value types in this case statement
63-
value, _ = NewValue(o.ctx.iso, v)
64-
case Valuer:
65-
value = v.value()
66-
default:
67-
return fmt.Errorf("v8go: unsupported object property type `%T`", v)
68-
}
69-
70-
if len(key) > 0 {
71-
ckey := C.CString(key)
72-
defer C.free(unsafe.Pointer(ckey))
73-
C.ObjectSet(o.ptr, ckey, value.ptr)
74-
return nil
77+
value, err := coerceValue(o.ctx.iso, val)
78+
if err != nil {
79+
return err
7580
}
7681

7782
C.ObjectSetIdx(o.ptr, C.uint32_t(idx), value.ptr)

object_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ func TestObjectSet(t *testing.T) {
6262
if err := obj.Set("a", 0); err == nil {
6363
t.Error("expected error but got <nil>")
6464
}
65-
obj.SetIdx(10, "ten")
65+
if err := obj.SetIdx(10, "ten"); err != nil {
66+
t.Errorf("unexpected error: %v", err)
67+
}
68+
if err := obj.SetIdx(10, t); err == nil {
69+
t.Error("expected error but got <nil>")
70+
}
6671
if ten, _ := ctx.RunScript("foo[10]", ""); ten.String() != "ten" {
6772
t.Errorf("unexpected value: %q", ten)
6873
}

0 commit comments

Comments
 (0)