Skip to content

Commit

Permalink
(fix): Allow pointers to bytes in PreCodec modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkaseman committed Dec 14, 2024
1 parent edc5dee commit 264dac3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/codec/precodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewPreCodec(fields map[string]string, codecs map[string]types.RemoteCodec)
}

m.modifyFieldForInput = func(_ string, field *reflect.StructField, _ string, typeDef string) error {
if field.Type != reflect.SliceOf(reflect.TypeFor[uint8]()) {
if field.Type != reflect.SliceOf(reflect.TypeFor[uint8]()) && field.Type != reflect.PointerTo(reflect.SliceOf(reflect.TypeFor[uint8]())) {
return fmt.Errorf("can only decode []byte from on-chain: %s", field.Type)
}

Expand Down
21 changes: 20 additions & 1 deletion pkg/codec/precodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type testStructOn struct {
Bid int
}

type testStructOnPointer struct {
Ask *[]byte
Bid int
}

type nestedTestStructOn struct {
Report []byte
FeedID [32]byte
Expand Down Expand Up @@ -122,7 +127,7 @@ func TestPreCodec(t *testing.T) {
assert.Equal(t, reflect.TypeOf(int(0)), field1.Type)
})

t.Run("RetypeToOffChain works on pointers", func(t *testing.T) {
t.Run("RetypeToOffChain works on pointers to type", func(t *testing.T) {
offChainType, err := preCodec.RetypeToOffChain(reflect.PointerTo(reflect.TypeOf(testStructOn{})), "")
require.NoError(t, err)
assert.Equal(t, reflect.Ptr, offChainType.Kind())
Expand All @@ -136,6 +141,20 @@ func TestPreCodec(t *testing.T) {
assert.Equal(t, reflect.TypeOf(int(0)), field1.Type)
})

t.Run("RetypeToOffChain works on pointers", func(t *testing.T) {
offChainType, err := preCodec.RetypeToOffChain(reflect.PointerTo(reflect.TypeOf(testStructOnPointer{})), "")
require.NoError(t, err)
assert.Equal(t, reflect.Ptr, offChainType.Kind())
elem := offChainType.Elem()
require.Equal(t, 2, elem.NumField())
field0 := elem.Field(0)
assert.Equal(t, "Ask", field0.Name)
assert.Equal(t, reflect.TypeOf(int(0)), field0.Type)
field1 := elem.Field(1)
assert.Equal(t, "Bid", field1.Name)
assert.Equal(t, reflect.TypeOf(int(0)), field1.Type)
})

t.Run("RetypeToOffChain works on slices", func(t *testing.T) {
offChainType, err := preCodec.RetypeToOffChain(reflect.SliceOf(reflect.TypeOf(testStructOn{})), "")
require.NoError(t, err)
Expand Down

0 comments on commit 264dac3

Please sign in to comment.