Skip to content

Commit 8d515e6

Browse files
committed
Clarify and verify option hook behavior
1 parent dfa7321 commit 8d515e6

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

internal/decoder/decoder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type Decoder struct {
1818
}
1919

2020
type decoderOptions struct {
21-
// Reserved for future options
21+
// Intentionally empty for now. DecoderOption callbacks are still invoked so
22+
// adding options in a future release is non-breaking.
2223
}
2324

2425
// DecoderOption configures a Decoder.

internal/decoder/decoder_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,21 @@ func ExampleDecoder_PeekKind() {
556556
func TestDecoderOptions(t *testing.T) {
557557
buffer := []byte{0x44, 't', 'e', 's', 't'} // String "test"
558558
dd := NewDataDecoder(buffer)
559+
optionCalled := false
560+
option := func(*decoderOptions) {
561+
optionCalled = true
562+
}
559563

560-
// Test that options infrastructure works (even with no current options)
564+
// Test that options infrastructure works (even with no current options).
561565
decoder1 := NewDecoder(dd, 0)
562566
require.NotNil(t, decoder1)
563567

564-
// Test that passing empty options slice works
568+
// Test that passing options invokes each option callback.
569+
decoderWithOption := NewDecoder(dd, 0, option)
570+
require.NotNil(t, decoderWithOption)
571+
require.True(t, optionCalled)
572+
573+
// Test that passing empty options slice works.
565574
decoder2 := NewDecoder(dd, 0)
566575
require.NotNil(t, decoder2)
567576
}

reader.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ func (m Metadata) BuildTime() time.Time {
202202
return time.Unix(int64(m.BuildEpoch), 0)
203203
}
204204

205-
type readerOptions struct{}
205+
type readerOptions struct {
206+
// Intentionally empty for now. ReaderOption callbacks are still invoked so
207+
// adding options in a future release is non-breaking.
208+
}
206209

207210
// ReaderOption are options for [Open] and [OpenBytes].
208211
//

reader_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ func TestReaderBytes(t *testing.T) {
6868
}
6969
}
7070

71+
func TestOpenBytesAppliesReaderOptions(t *testing.T) {
72+
optionCalled := false
73+
_, err := OpenBytes(nil, func(*readerOptions) {
74+
optionCalled = true
75+
})
76+
require.Error(t, err)
77+
require.True(t, optionCalled)
78+
}
79+
7180
func TestReaderLeaks(t *testing.T) {
7281
collected := make(chan struct{})
7382

0 commit comments

Comments
 (0)