Skip to content

Commit

Permalink
fix: add pointer-string support to fromJSON & preserve interfaces
Browse files Browse the repository at this point in the history
This commit enables fromJSON to accept both string and *string arguments by
performing a runtime check on the provided argument. It also refactors the
deref.Type function to preserve interface types, ensuring methods sets are
not lost when unwrapping pointers.

Changes:
- fromJSON now handles `string` and `*string` distinctly, returning an error
  if the pointer is nil or if an unsupported type is given.
- deref.Type preserves the Kind() == reflect.Interface immediately instead
  of unwrapping further, which prevents losing interface method sets.
- Updated unit tests to confirm that interface-wrapped pointers are unwrapped
  correctly (e.g., reflect.String) and interface types remain interfaces.

Signed-off-by: Ville Vesilehto <[email protected]>
  • Loading branch information
thevilledev committed Jan 27, 2025
1 parent 86798a6 commit 846b310
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
20 changes: 15 additions & 5 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,27 @@ var Builtins = []*Function{
Name: "fromJSON",
Func: func(args ...any) (any, error) {
var v any
jsonStr := args[0]
if strPtr, ok := jsonStr.(*string); ok {
jsonStr = *strPtr
var jsonStr string

switch arg := args[0].(type) {
case string:
jsonStr = arg
case *string:
if arg == nil {
return nil, fmt.Errorf("nil string pointer")
}
jsonStr = *arg
default:
return nil, fmt.Errorf("expected string or *string, got %T", args[0])
}
err := json.Unmarshal([]byte(jsonStr.(string)), &v)

err := json.Unmarshal([]byte(jsonStr), &v)
if err != nil {
return nil, err
}
return v, nil
},
Types: types(new(func(string) any)),
Types: types(new(func(string) any), new(func(*string) any)),
},
{
Name: "toBase64",
Expand Down
19 changes: 19 additions & 0 deletions internal/deref/deref.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,28 @@ func Type(t reflect.Type) reflect.Type {
if t == nil {
return nil
}

// Preserve interface types immediately to maintain type information
// This handles both empty (interface{}) and non-empty (e.g., io.Reader) interfaces
if t.Kind() == reflect.Interface {
return t
}

// Iteratively unwrap pointer types until we reach a non-pointer
// or encounter an interface type that needs preservation
for t.Kind() == reflect.Ptr {
t = t.Elem()
if t == nil {
return nil
}
// Stop unwrapping if we hit an interface type to preserve its type information
// This ensures interface method sets are not lost
if t.Kind() == reflect.Interface {
return t
}
}

// Return the final unwrapped type, which could be any non-pointer, non-interface type
return t
}

Expand Down
17 changes: 17 additions & 0 deletions internal/deref/deref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ func TestType_nil(t *testing.T) {
assert.Nil(t, deref.Type(nil))
}

func TestType_interface_wrapped_pointer(t *testing.T) {
t.Run("one level", func(t *testing.T) {
str := "hello"
var iface any = &str
dt := deref.Type(reflect.TypeOf(iface))
assert.Equal(t, reflect.String, dt.Kind())
})

t.Run("two levels", func(t *testing.T) {
str := "hello"
strPtr := &str
var iface any = &strPtr
dt := deref.Type(reflect.TypeOf(iface))
assert.Equal(t, reflect.String, dt.Kind())
})
}

func TestValue(t *testing.T) {
a := uint(42)
b := &a
Expand Down

0 comments on commit 846b310

Please sign in to comment.