Skip to content

Commit c6f199d

Browse files
committed
refactor(rlp): remove top-level DecodeStructFields()
1 parent b3f6c5b commit c6f199d

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

rlp/list.libevm.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func EncodeListToBuffer[T any](b EncoderBuffer, vals []T) error {
5454
// optional "fields" are treated identically to those tagged with
5555
// `rlp:"optional"`.
5656
//
57-
// See the example for [DecodeStructFields].
57+
// See the example for [Stream.DecodeStructFields].
5858
func EncodeStructFields(w io.Writer, required, optional []any) error {
5959
includeOptional, err := optionalFieldInclusionFlags(optional)
6060
if err != nil {
@@ -147,11 +147,6 @@ func DecodeList[T any](s *Stream) ([]*T, error) {
147147
//
148148
// Typically, the arguments to this function mirror those passed to
149149
// [EncodeStructFields] except for being pointers. See the example.
150-
func DecodeStructFields(r io.Reader, required, optional []any) error {
151-
s := NewStream(r, 0)
152-
return s.DecodeStructFields(required, optional)
153-
}
154-
155150
func (s *Stream) DecodeStructFields(required, optional []any) error {
156151
return s.FromList(func() error {
157152
for _, v := range required {

rlp/list.libevm_test.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,38 +114,40 @@ func TestStructFieldHelpers(t *testing.T) {
114114
})
115115

116116
t.Run("DecodeStructFields", func(t *testing.T) {
117+
s := NewStream(bytes.NewReader(wantRLP), 0)
117118
var got foo
118-
err := DecodeStructFields(
119-
bytes.NewReader(wantRLP),
119+
err := s.DecodeStructFields(
120120
[]any{&got.A, &got.B, &got.C},
121121
[]any{&got.D, &got.E, &got.F},
122122
)
123-
require.NoError(t, err, "DecodeStructFields(...)")
123+
require.NoError(t, err, "Stream.DecodeStructFields(...)")
124124

125125
var want foo
126126
err = DecodeBytes(wantRLP, &want)
127127
require.NoError(t, err, "DecodeBytes(...)")
128128

129-
assert.Equal(t, want, got, "DecodeBytes(..., [original struct]) vs DecodeStructFields(...)")
129+
assert.Equal(t, want, got, "DecodeBytes(..., [original struct]) vs Stream.DecodeStructFields(...)")
130130
})
131131
})
132132
}
133133
}
134134

135135
//nolint:testableexamples // Demonstrating code equivalence, not outputs.
136-
func ExampleDecodeStructFields() {
136+
func ExampleStream_DecodeStructFields() {
137137
type inner struct {
138138
X uint64
139139
}
140140

141141
type outer struct {
142142
A uint64
143-
B *inner `rlp:"optional"`
143+
B *inner `rlp:"nil"`
144+
C *inner `rlp:"optional"`
144145
}
145146

146147
val := outer{
147148
A: 42,
148-
B: &inner{X: 99},
149+
B: &inner{X: 42},
150+
C: &inner{X: 99},
149151
}
150152

151153
// Errors are dropped for brevity for the sake of the example only.
@@ -154,24 +156,26 @@ func ExampleDecodeStructFields() {
154156
// is equivalent to
155157
_ = EncodeStructFields(
156158
io.Discard,
157-
[]any{val.A},
158-
[]any{val.B},
159+
[]any{val.A, val.B},
160+
[]any{val.C},
159161
)
160162

161163
r := bytes.NewReader(nil /*arbitrary RLP buffer*/)
162164
var decoded outer
163165
_ = Decode(r, &decoded)
164166
// is equivalent to
165-
_ = DecodeStructFields(
166-
r,
167-
[]any{&val.A},
168-
[]any{&val.B},
167+
_ = NewStream(r, 0).DecodeStructFields(
168+
[]any{
169+
&val.A,
170+
Nillable(&val.B),
171+
},
172+
[]any{&val.C},
169173
)
170174

171175
// Note the parallels between the arguments passed to
172-
// {En,De}codeStructFields() and that, when decoding an optional field, a
173-
// pointer to the _field_ is required even though in this example it will be
174-
// a `**inner`.
176+
// {En,De}codeStructFields() and that, when decoding an optional or
177+
// `rlp:"nil`-tagged field, a pointer to the _field_ is required even though
178+
// in this example it will be a `**inner`.
175179
}
176180

177181
func TestNillable(t *testing.T) {
@@ -218,19 +222,19 @@ func TestNillable(t *testing.T) {
218222
err = DecodeBytes(rlp, &want)
219223
require.NoErrorf(t, err, "DecodeBytes(%#x, %T)", rlp, &want)
220224

225+
s := NewStream(bytes.NewReader(rlp), 0)
221226
got := corruptInitialValue()
222-
err = DecodeStructFields(
223-
bytes.NewReader(rlp),
227+
err = s.DecodeStructFields(
224228
[]any{
225229
Nillable(&got.A),
226230
Nillable(&got.B),
227231
Nillable(&got.C),
228232
},
229233
nil,
230234
)
231-
require.NoError(t, err, "DecodeStructFields(...)")
235+
require.NoError(t, err, "Stream.DecodeStructFields(...)")
232236

233-
assert.Equal(t, want, got, "DecodeBytes(...) vs DecodeStructFields(...)")
237+
assert.Equal(t, want, got, "DecodeBytes(...) vs Stream.DecodeStructFields([fields wrapped in Nillable()])")
234238
})
235239
}
236240
}

0 commit comments

Comments
 (0)