Skip to content

Commit

Permalink
ocf: encoder should implement writer interface (#144)
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Brunsgaard <[email protected]>
  • Loading branch information
brunsgaard authored Jan 24, 2022
1 parent 9c35b95 commit 0447e89
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 6 additions & 5 deletions ocf/ocf.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,20 @@ func NewEncoder(s string, w io.Writer, opts ...EncoderFunc) (*Encoder, error) {
// therefore the caller is responsible for encoding the bytes. No error will be
// thrown if the bytes does not conform to the schema given to NewEncoder, but
// the final ocf data will be corrupted.
func (e *Encoder) Write(v []byte) error {
if _, err := e.buf.Write(v); err != nil {
return err
func (e *Encoder) Write(p []byte) (n int, err error) {
n, err = e.buf.Write(p)
if err != nil {
return n, err
}

e.count++
if e.count >= e.blockLength {
if err := e.writerBlock(); err != nil {
return err
return n, err
}
}

return e.writer.Error
return n, e.writer.Error
}

// Encode writes the Avro encoding of v to the stream.
Expand Down
3 changes: 2 additions & 1 deletion ocf/ocf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,13 @@ func TestEncoder_Write(t *testing.T) {
encodedBytes, err := avro.Marshal(avro.MustParse(schema), record)
require.NoError(t, err)

err = enc.Write(encodedBytes)
n, err := enc.Write(encodedBytes)
require.NoError(t, err)

err = enc.Close()
require.NoError(t, err)

require.Equal(t, n, len(encodedBytes))
require.Equal(t, 957, buf.Len())
}

Expand Down

0 comments on commit 0447e89

Please sign in to comment.