Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(audio): fix audioTextResponse decode #638

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,14 @@ func decodeResponse(body io.Reader, v any) error {
return nil
}

if result, ok := v.(*string); ok {
return decodeString(body, result)
switch o := v.(type) {
case *string:
return decodeString(body, o)
case *audioTextResponse:
return decodeString(body, &o.Text)
default:
return json.NewDecoder(body).Decode(v)
}
return json.NewDecoder(body).Decode(v)
}

func decodeString(body io.Reader, output *string) error {
Expand Down
47 changes: 39 additions & 8 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"fmt"
"io"
"net/http"
"reflect"
"testing"

"github.com/sashabaranov/go-openai/internal/test"
Expand Down Expand Up @@ -37,43 +38,73 @@
}
}

func TestDecodeResponse(t *testing.T) {

Check failure on line 41 in client_test.go

View workflow job for this annotation

GitHub Actions / Sanity check

cognitive complexity 23 of func `TestDecodeResponse` is high (> 20) (gocognit)
stringInput := ""

testCases := []struct {
name string
value interface{}
expected interface{}
body io.Reader
hasError bool
}{
{
name: "nil input",
value: nil,
body: bytes.NewReader([]byte("")),
name: "nil input",
value: nil,
body: bytes.NewReader([]byte("")),
expected: nil,
},
{
name: "string input",
value: &stringInput,
body: bytes.NewReader([]byte("test")),
name: "string input",
value: &stringInput,
body: bytes.NewReader([]byte("test")),
expected: "test",
},
{
name: "map input",
value: &map[string]interface{}{},
body: bytes.NewReader([]byte(`{"test": "test"}`)),
expected: map[string]interface{}{
"test": "test",
},
},
{
name: "reader return error",
value: &stringInput,
body: &errorReader{err: errors.New("dummy")},
hasError: true,
},
{
name: "audio text input",
value: &audioTextResponse{},
body: bytes.NewReader([]byte("test")),
expected: audioTextResponse{
Text: "test",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := decodeResponse(tc.body, tc.value)
if (err != nil) != tc.hasError {
t.Errorf("Unexpected error: %v", err)
if tc.hasError {

Check failure on line 90 in client_test.go

View workflow job for this annotation

GitHub Actions / Sanity check

`if tc.hasError` has complex nested blocks (complexity: 9) (nestif)
if err == nil {
t.Error("Unexpected nil error")
}
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can reduce the amount of else by using early return pattern:

if something {
// code
    return
}

// else block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! It would be better to use github.com/stretchr/testify/assert for testing, as it eliminates the need for if/else statements for assertions. However, I didn't use it because it doesn't have any external dependencies. Would you like to use it or keep it dependency-free?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WqyJh staying dependency free is important for the project, so let's not use the library. We have an internal lib of helpers though: https://github.com/sashabaranov/go-openai/blob/master/internal/test/checks/checks.go

if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if tc.expected != nil {
v := reflect.ValueOf(tc.value).Elem().Interface()
if !reflect.DeepEqual(v, tc.expected) {
t.Errorf("Unexpected value: %v, expected: %v", v, tc.expected)
}
} else {

Check failure on line 103 in client_test.go

View workflow job for this annotation

GitHub Actions / Sanity check

elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
if tc.value != tc.expected {
t.Errorf("Unexpected value: %v, expected: %v", tc.value, tc.expected)
}
}
}
})
}
Expand Down
Loading