From eb68a72bcc4de666dbda36d769ad19a0958ddcd3 Mon Sep 17 00:00:00 2001 From: sashabaranov <677093+sashabaranov@users.noreply.github.com> Date: Wed, 22 Mar 2023 09:56:05 +0400 Subject: [PATCH] simplify unmarshal (#191) * simplify unmarshal * simplify unmarshalError * rename errorAccumulate -> defaultErrorAccumulator * update converage --- error_accumulator.go | 28 ++++++++++++++-------------- error_accumulator_test.go | 18 ++++++++++++------ stream_reader.go | 5 +++-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/error_accumulator.go b/error_accumulator.go index e75086e67..ca6cec6e3 100644 --- a/error_accumulator.go +++ b/error_accumulator.go @@ -8,7 +8,7 @@ import ( type errorAccumulator interface { write(p []byte) error - unmarshalError() (*ErrorResponse, error) + unmarshalError() *ErrorResponse } type errorBuffer interface { @@ -17,19 +17,19 @@ type errorBuffer interface { Bytes() []byte } -type errorAccumulate struct { +type defaultErrorAccumulator struct { buffer errorBuffer unmarshaler unmarshaler } func newErrorAccumulator() errorAccumulator { - return &errorAccumulate{ + return &defaultErrorAccumulator{ buffer: &bytes.Buffer{}, unmarshaler: &jsonUnmarshaler{}, } } -func (e *errorAccumulate) write(p []byte) error { +func (e *defaultErrorAccumulator) write(p []byte) error { _, err := e.buffer.Write(p) if err != nil { return fmt.Errorf("error accumulator write error, %w", err) @@ -37,15 +37,15 @@ func (e *errorAccumulate) write(p []byte) error { return nil } -func (e *errorAccumulate) unmarshalError() (*ErrorResponse, error) { - var err error - if e.buffer.Len() > 0 { - var errRes ErrorResponse - err = e.unmarshaler.unmarshal(e.buffer.Bytes(), &errRes) - if err != nil { - return nil, err - } - return &errRes, nil +func (e *defaultErrorAccumulator) unmarshalError() (errResp *ErrorResponse) { + if e.buffer.Len() == 0 { + return } - return nil, err + + err := e.unmarshaler.unmarshal(e.buffer.Bytes(), &errResp) + if err != nil { + errResp = nil + } + + return } diff --git a/error_accumulator_test.go b/error_accumulator_test.go index d4008c06a..4dabc1ef9 100644 --- a/error_accumulator_test.go +++ b/error_accumulator_test.go @@ -36,23 +36,29 @@ func (*failingUnMarshaller) unmarshal(_ []byte, _ any) error { } func TestErrorAccumulatorReturnsUnmarshalerErrors(t *testing.T) { - accumulator := &errorAccumulate{ + accumulator := &defaultErrorAccumulator{ buffer: &bytes.Buffer{}, unmarshaler: &failingUnMarshaller{}, } + respErr := accumulator.unmarshalError() + if respErr != nil { + t.Fatalf("Did not return nil with empty buffer: %v", respErr) + } + err := accumulator.write([]byte("{")) if err != nil { t.Fatalf("%+v", err) } - _, err = accumulator.unmarshalError() - if !errors.Is(err, errTestUnmarshalerFailed) { - t.Fatalf("Did not return error when unmarshaler failed: %v", err) + + respErr = accumulator.unmarshalError() + if respErr != nil { + t.Fatalf("Did not return nil when unmarshaler failed: %v", respErr) } } func TestErrorByteWriteErrors(t *testing.T) { - accumulator := &errorAccumulate{ + accumulator := &defaultErrorAccumulator{ buffer: &failingErrorBuffer{}, unmarshaler: &jsonUnmarshaler{}, } @@ -78,7 +84,7 @@ func TestErrorAccumulatorWriteErrors(t *testing.T) { if err != nil { t.Fatal(err) } - stream.errAccumulator = &errorAccumulate{ + stream.errAccumulator = &defaultErrorAccumulator{ buffer: &failingErrorBuffer{}, unmarshaler: &jsonUnmarshaler{}, } diff --git a/stream_reader.go b/stream_reader.go index 07500a5d3..aa06f00ae 100644 --- a/stream_reader.go +++ b/stream_reader.go @@ -33,8 +33,9 @@ func (stream *streamReader[T]) Recv() (response T, err error) { waitForData: line, err := stream.reader.ReadBytes('\n') if err != nil { - if errRes, _ := stream.errAccumulator.unmarshalError(); errRes != nil { - err = fmt.Errorf("error, %w", errRes.Error) + respErr := stream.errAccumulator.unmarshalError() + if respErr != nil { + err = fmt.Errorf("error, %w", respErr.Error) } return }