Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Handle aliases in slices #784

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/values/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ func Test_BytesUnwrapTo(t *testing.T) {
require.NoError(t, err)
assert.Nil(t, bp)
}

type alias uint8

func Test_BytesUnwrapToAlias(t *testing.T) {
underlying := []byte("hello")
bn := &Bytes{Underlying: underlying}
bp := []alias{}
err := bn.UnwrapTo(&bp)
require.NoError(t, err)

got := []byte{}
for _, b := range bp {
got = append(got, byte(b))
}
assert.Equal(t, underlying, got)
}
34 changes: 32 additions & 2 deletions pkg/values/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,40 @@ func unwrapTo[T any](underlying T, to any) error {
rTo := reflect.ValueOf(to)
rUnderlying := reflect.ValueOf(underlying)
underlyingPtr := reflect.PointerTo(rUnderlying.Type())
if rTo.Kind() != reflect.Pointer || !rTo.CanConvert(underlyingPtr) {
if rTo.Kind() != reflect.Pointer {
return fmt.Errorf("cannot unwrap to value of type: %T", to)
}
reflect.Indirect(rTo.Convert(underlyingPtr)).Set(rUnderlying)

if rTo.CanConvert(underlyingPtr) {
reflect.Indirect(rTo.Convert(underlyingPtr)).Set(rUnderlying)
return nil
}

rToVal := reflect.Indirect(rTo)
if rToVal.Kind() == reflect.Slice && rUnderlying.Kind() == reflect.Slice {
newList := reflect.MakeSlice(rToVal.Type(), rUnderlying.Len(), rUnderlying.Len())
for i := 0; i < rUnderlying.Len(); i++ {
el := rUnderlying.Index(i)
toEl := newList.Index(i)

if toEl.Kind() == reflect.Ptr {
err := unwrapTo(el.Interface(), toEl.Interface())
if err != nil {
return err
}
} else {
err := unwrapTo(el.Interface(), toEl.Addr().Interface())
if err != nil {
return err
}
}
}

rToVal.Set(newList)
return nil
}

return fmt.Errorf("cannot unwrap to value of type: %T", to)
}

return nil
Expand Down
79 changes: 79 additions & 0 deletions pkg/values/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,82 @@ func Test_Copy(t *testing.T) {
}
}
}

type aliasByte []byte
type aliasString string
type aliasInt int
type aliasMap map[string]any
type aliasSingleByte uint8

func Test_Aliases(t *testing.T) {
testCases := []struct {
name string
val func() any
alias func() any
convert func(any) any
}{
{
name: "alias to []byte",
val: func() any { return []byte("string") },
alias: func() any { return aliasByte([]byte{}) },
},
{
name: "simple aliases",
val: func() any { return "string" },
alias: func() any { return aliasString("") },
},
{
name: "aliasByte -> []byte",
val: func() any { return []byte("string") },
alias: func() any { return aliasByte([]byte{}) },
},
{
name: "[]aliasSingleByte -> []byte",
val: func() any { return []byte("string") },
alias: func() any { return []aliasSingleByte{} },
},
{
name: "int",
val: func() any { return 2 },
alias: func() any { return aliasInt(0) },
convert: func(a any) any { return int(a.(int64)) },
},
{
name: "[][]byte -> []aliasByte",
val: func() any { return [][]byte{[]byte("hello")} },
alias: func() any { return []aliasByte{} },
convert: func(a any) any {
to := [][]byte{}
for _, v := range a.([]interface{}) {
to = append(to, v.([]byte))
}

return to
},
},
{
name: "aliasMap -> map[string]any",
val: func() any { return map[string]any{} },
alias: func() any { return aliasMap{} },
convert: func(a any) any { return map[string]any(a.(aliasMap)) },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, so the unwrap method works for aliases of complex types like map[string]any as well

},
}

for _, tc := range testCases {
t.Run(tc.name, func(st *testing.T) {
v := tc.val()
wv, err := Wrap(v)
require.NoError(t, err)

a := tc.alias()
err = wv.UnwrapTo(&a)
require.NoError(t, err)

if tc.convert != nil {
assert.Equal(t, tc.convert(a), v)
} else {
assert.Equal(t, a, v)
}
})
}
}
Loading