simplify unmarshal (#191)

* simplify unmarshal

* simplify unmarshalError

* rename errorAccumulate -> defaultErrorAccumulator

* update converage
This commit is contained in:
sashabaranov
2023-03-22 09:56:05 +04:00
committed by GitHub
parent a5a945ad14
commit eb68a72bcc
3 changed files with 29 additions and 22 deletions

View File

@@ -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
}

View File

@@ -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{},
}

View File

@@ -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
}