Skip to content

Commit 461716f

Browse files
authored
Merge pull request #25 from fastschema/refactor.rename_symbols_and_enhance_go_value_passthrough
refactor: rename symbols and enhance go value passthrough
2 parents b747fb1 + 09df7fa commit 461716f

File tree

12 files changed

+246
-187
lines changed

12 files changed

+246
-187
lines changed

common.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (ac *JsArrayToGoConverter[T]) convertToInterface(jsArray *Array, jsLen int6
369369
for i := range jsLen {
370370
jsElem := jsArray.Get(i)
371371

372-
goElem, convErr := jsValueToGo[any](ac.tracker, jsElem)
372+
goElem, convErr := toGoValue[any](ac.tracker, jsElem)
373373

374374
if !jsElem.IsFunction() {
375375
jsElem.Free()
@@ -398,7 +398,7 @@ func (ac *JsArrayToGoConverter[T]) convertToSlice(jsArray *Array, jsLen int64) (
398398
for i := range jsLen {
399399
jsElem := jsArray.Get(i)
400400
elemSample := reflect.New(elemType).Elem().Interface()
401-
goElem, convErr := jsValueToGo(ac.tracker, jsElem, elemSample)
401+
goElem, convErr := toGoValue(ac.tracker, jsElem, elemSample)
402402

403403
if !jsElem.IsFunction() {
404404
jsElem.Free()
@@ -433,7 +433,7 @@ func (ac *JsArrayToGoConverter[T]) convertToArray(jsArray *Array, jsLen int64) (
433433
for i := range jsLen {
434434
jsElem := jsArray.Get(i)
435435
elemSample := reflect.New(elemType).Elem().Interface()
436-
goElem, convErr := jsValueToGo(ac.tracker, jsElem, elemSample)
436+
goElem, convErr := toGoValue(ac.tracker, jsElem, elemSample)
437437

438438
if !jsElem.IsFunction() {
439439
jsElem.Free()
@@ -593,7 +593,7 @@ func processTempValue[T any](prefix string, temp any, err error, samples ...T) (
593593
return tempT, nil
594594
}
595595

596-
return v, newInvalidGoTargetErr(GetGoTypeName(sample), temp)
596+
return v, newInvalidGoTypeErr(GetGoTypeName(sample), temp)
597597
}
598598

599599
valueT, _ := temp.(T)
@@ -769,7 +769,7 @@ func isGoStruct(goType reflect.Type) bool {
769769
// VerifyGoFunc validates that a function signature is compatible with JS conversion.
770770
func VerifyGoFunc(fnType reflect.Type, sample any) error {
771771
if fnType == nil || fnType.Kind() != reflect.Func {
772-
return newInvalidGoTargetErr("function", sample)
772+
return newInvalidGoTypeErr("function", sample)
773773
}
774774

775775
// Validate that all return values are convertible to JS

common_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,14 @@ func TestCreateGoBindFuncType(t *testing.T) {
536536
t.Run("non_function", func(t *testing.T) {
537537
_, err := qjs.CreateGoBindFuncType("not a function")
538538
assert.Error(t, err)
539-
assert.Contains(t, err.Error(), "expected GO target")
539+
assert.Contains(t, err.Error(), "expected GO type")
540540
assert.Contains(t, err.Error(), "function")
541541
})
542542

543543
t.Run("integer", func(t *testing.T) {
544544
_, err := qjs.CreateGoBindFuncType(42)
545545
assert.Error(t, err)
546-
assert.Contains(t, err.Error(), "expected GO target")
546+
assert.Contains(t, err.Error(), "expected GO type")
547547
assert.Contains(t, err.Error(), "function")
548548
})
549549
})

context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestContextValueCreation(t *testing.T) {
4343
fmt.Printf("context check: is nil=%t, value=%v\n", ctx == nil, ctx)
4444
return num * 2
4545
}
46-
jsFuncWithContext := must(qjs.ToJSValue(ctx, goFuncWithContext))
46+
jsFuncWithContext := must(qjs.ToJsValue(ctx, goFuncWithContext))
4747
defer jsFuncWithContext.Free()
4848
ctx.Global().SetPropertyStr("funcWithContext", jsFuncWithContext)
4949

errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ func newArgConversionErr(index int, err error) error {
108108
return fmt.Errorf("cannot convert JS function argument at index %d: %w", index, err)
109109
}
110110

111-
func newInvalidGoTargetErr(expect string, got any) error {
112-
return fmt.Errorf("expected GO target %s, got %T", expect, got)
111+
func newInvalidGoTypeErr(expect string, got any) error {
112+
return fmt.Errorf("expected GO type %s, got %T", expect, got)
113113
}
114114

115115
func newInvalidJsInputErr(kind string, input *Value) (err error) {

functojs.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func FuncToJS(c *Context, v any) (_ *Value, err error) {
3030
}
3131

3232
if rtype.Kind() != reflect.Func {
33-
return nil, newInvalidGoTargetErr("function", v)
33+
return nil, newInvalidGoTypeErr("function", v)
3434
}
3535

3636
if rval.IsNil() {
@@ -114,7 +114,7 @@ func handlePointerArgument(jsArg *Value, argType reflect.Type) (reflect.Value, e
114114
underlyingType := argType.Elem()
115115
zeroVal := reflect.New(underlyingType).Elem()
116116

117-
goVal, err := JsValueToGo(jsArg, zeroVal.Interface())
117+
goVal, err := ToGoValue(jsArg, zeroVal.Interface())
118118
if err != nil {
119119
return reflect.Value{}, newJsToGoErr(jsArg, err, "function param pointer to "+jsArg.Type())
120120
}
@@ -184,7 +184,7 @@ func JsArgToGo(jsArg *Value, argType reflect.Type) (reflect.Value, error) {
184184

185185
goZeroVal := CreateNonNilSample(argType)
186186

187-
goVal, err := JsValueToGo(jsArg, goZeroVal)
187+
goVal, err := ToGoValue(jsArg, goZeroVal)
188188
if err != nil {
189189
return reflect.Value{}, newJsToGoErr(jsArg, err, "function param "+jsArg.Type())
190190
}
@@ -241,7 +241,7 @@ func GoFuncResultToJs(c *Context, results []reflect.Value) (*Value, error) {
241241

242242
// Single remaining value -> return that value
243243
if len(remaining) == 1 {
244-
return ToJSValue(c, remaining[0].Interface())
244+
return ToJsValue(c, remaining[0].Interface())
245245
}
246246

247247
// Multiple remaining values -> return as JS array
@@ -250,12 +250,12 @@ func GoFuncResultToJs(c *Context, results []reflect.Value) (*Value, error) {
250250
jsValues[i] = result.Interface()
251251
}
252252

253-
return ToJSValue(c, jsValues)
253+
return ToJsValue(c, jsValues)
254254
}
255255

256256
// Single return value -> return that value
257257
if len(results) == 1 {
258-
return ToJSValue(c, results[0].Interface())
258+
return ToJsValue(c, results[0].Interface())
259259
}
260260

261261
// Multiple return values -> return as JS array
@@ -264,5 +264,5 @@ func GoFuncResultToJs(c *Context, results []reflect.Value) (*Value, error) {
264264
jsValues[i] = result.Interface()
265265
}
266266

267-
return ToJSValue(c, jsValues)
267+
return ToJsValue(c, jsValues)
268268
}

functojs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestBasicConversion(t *testing.T) {
5858
t.Run("InvalidTypes", func(t *testing.T) {
5959
_, err := qjs.FuncToJS(ctx, "not a function")
6060
require.Error(t, err)
61-
assert.Contains(t, err.Error(), "expected GO target function")
61+
assert.Contains(t, err.Error(), "expected GO type function")
6262
})
6363

6464
t.Run("FunctionTypes", func(t *testing.T) {

0 commit comments

Comments
 (0)