Repository: sashabaranov/go-openai
Pull request #191 (2023-03-22 05:40:26)

simplify unmarshal

codecov[bot]: ## [Codecov](https://codecov.io/gh/sashabaranov/go-openai/pull/191?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#191](https://codecov.io/gh/sashabaranov/go-openai/pull/191?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (c27588d) into [master](https://codecov.io/gh/sashabaranov/go-openai/commit/a5a945ad1466cfe22e190060b06f9820034af0a0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (a5a945a) will **decrease** coverage by `0.36%`.
> The diff coverage is `81.81%`.

> :exclamation: Current head c27588d differs from pull request most recent head c263b38. Consider uploading reports for the commit c263b38 to get more accurate results

```diff
@@            Coverage Diff             @@
##           master     #191      +/-   ##
==========================================
- Coverage   71.12%   70.77%   -0.36%     
==========================================
  Files          21       21              
  Lines         568      568              
==========================================
- Hits          404      402       -2     
- Misses        125      126       +1     
- Partials       39       40       +1     
```


| [Impacted Files](https://codecov.io/gh/sashabaranov/go-openai/pull/191?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [error\_accumulator.go](https://codecov.io/gh/sashabaranov/go-openai/pull/191?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-ZXJyb3JfYWNjdW11bGF0b3IuZ28=) | `85.00% <75.00%> (-10.24%)` | :arrow_down: |
| [stream\_reader.go](https://codecov.io/gh/sashabaranov/go-openai/pull/191?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3RyZWFtX3JlYWRlci5nbw==) | `89.18% <100.00%> (+0.30%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/error_accumulator.go b/error_accumulator.go
index e75086e..ca6cec6 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/stream_reader.go b/stream_reader.go
index 07500a5..aa06f00 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
 	}


Test patch:
diff --git a/error_accumulator_test.go b/error_accumulator_test.go
index d4008c0..4dabc1e 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{},
 	}