Skip to content

Commit 62eb4be

Browse files
authored
move marshaller and unmarshaler into internal pkg (#304) (#325)
1 parent 980504b commit 62eb4be

12 files changed

+57
-46
lines changed

chat_stream.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bufio"
55
"context"
66
"net/http"
7+
8+
utils "github.com/sashabaranov/go-openai/internal"
79
)
810

911
type ChatCompletionStreamChoiceDelta struct {
@@ -65,7 +67,7 @@ func (c *Client) CreateChatCompletionStream(
6567
reader: bufio.NewReader(resp.Body),
6668
response: resp,
6769
errAccumulator: newErrorAccumulator(),
68-
unmarshaler: &jsonUnmarshaler{},
70+
unmarshaler: &utils.JSONUnmarshaler{},
6971
},
7072
}
7173
return

error_accumulator.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
8+
utils "github.com/sashabaranov/go-openai/internal"
79
)
810

911
type errorAccumulator interface {
@@ -19,13 +21,13 @@ type errorBuffer interface {
1921

2022
type defaultErrorAccumulator struct {
2123
buffer errorBuffer
22-
unmarshaler unmarshaler
24+
unmarshaler utils.Unmarshaler
2325
}
2426

2527
func newErrorAccumulator() errorAccumulator {
2628
return &defaultErrorAccumulator{
2729
buffer: &bytes.Buffer{},
28-
unmarshaler: &jsonUnmarshaler{},
30+
unmarshaler: &utils.JSONUnmarshaler{},
2931
}
3032
}
3133

@@ -42,7 +44,7 @@ func (e *defaultErrorAccumulator) unmarshalError() (errResp *ErrorResponse) {
4244
return
4345
}
4446

45-
err := e.unmarshaler.unmarshal(e.buffer.Bytes(), &errResp)
47+
err := e.unmarshaler.Unmarshal(e.buffer.Bytes(), &errResp)
4648
if err != nil {
4749
errResp = nil
4850
}

error_accumulator_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"testing"
99

10+
utils "github.com/sashabaranov/go-openai/internal"
1011
"github.com/sashabaranov/go-openai/internal/test"
1112
"github.com/sashabaranov/go-openai/internal/test/checks"
1213
)
@@ -33,7 +34,7 @@ func (b *failingErrorBuffer) Bytes() []byte {
3334
return []byte{}
3435
}
3536

36-
func (*failingUnMarshaller) unmarshal(_ []byte, _ any) error {
37+
func (*failingUnMarshaller) Unmarshal(_ []byte, _ any) error {
3738
return errTestUnmarshalerFailed
3839
}
3940

@@ -62,7 +63,7 @@ func TestErrorAccumulatorReturnsUnmarshalerErrors(t *testing.T) {
6263
func TestErrorByteWriteErrors(t *testing.T) {
6364
accumulator := &defaultErrorAccumulator{
6465
buffer: &failingErrorBuffer{},
65-
unmarshaler: &jsonUnmarshaler{},
66+
unmarshaler: &utils.JSONUnmarshaler{},
6667
}
6768
err := accumulator.write([]byte("{"))
6869
if !errors.Is(err, errTestErrorAccumulatorWriteFailed) {
@@ -91,7 +92,7 @@ func TestErrorAccumulatorWriteErrors(t *testing.T) {
9192

9293
stream.errAccumulator = &defaultErrorAccumulator{
9394
buffer: &failingErrorBuffer{},
94-
unmarshaler: &jsonUnmarshaler{},
95+
unmarshaler: &utils.JSONUnmarshaler{},
9596
}
9697

9798
_, err = stream.Recv()

files_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package openai //nolint:testpackage // testing private field
22

33
import (
4-
. "github.com/sashabaranov/go-openai/internal"
4+
utils "github.com/sashabaranov/go-openai/internal"
55
"github.com/sashabaranov/go-openai/internal/test"
66
"github.com/sashabaranov/go-openai/internal/test/checks"
77

@@ -86,7 +86,7 @@ func TestFileUploadWithFailingFormBuilder(t *testing.T) {
8686
config.BaseURL = ""
8787
client := NewClientWithConfig(config)
8888
mockBuilder := &mockFormBuilder{}
89-
client.createFormBuilder = func(io.Writer) FormBuilder {
89+
client.createFormBuilder = func(io.Writer) utils.FormBuilder {
9090
return mockBuilder
9191
}
9292

internal/marshaller.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package openai
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
type Marshaller interface {
8+
Marshal(value any) ([]byte, error)
9+
}
10+
11+
type JSONMarshaller struct{}
12+
13+
func (jm *JSONMarshaller) Marshal(value any) ([]byte, error) {
14+
return json.Marshal(value)
15+
}

internal/unmarshaler.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package openai
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
type Unmarshaler interface {
8+
Unmarshal(data []byte, v any) error
9+
}
10+
11+
type JSONUnmarshaler struct{}
12+
13+
func (jm *JSONUnmarshaler) Unmarshal(data []byte, v any) error {
14+
return json.Unmarshal(data, v)
15+
}

marshaller.go

-15
This file was deleted.

request_builder.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import (
44
"bytes"
55
"context"
66
"net/http"
7+
8+
utils "github.com/sashabaranov/go-openai/internal"
79
)
810

911
type requestBuilder interface {
1012
build(ctx context.Context, method, url string, request any) (*http.Request, error)
1113
}
1214

1315
type httpRequestBuilder struct {
14-
marshaller marshaller
16+
marshaller utils.Marshaller
1517
}
1618

1719
func newRequestBuilder() *httpRequestBuilder {
1820
return &httpRequestBuilder{
19-
marshaller: &jsonMarshaller{},
21+
marshaller: &utils.JSONMarshaller{},
2022
}
2123
}
2224

@@ -26,7 +28,7 @@ func (b *httpRequestBuilder) build(ctx context.Context, method, url string, requ
2628
}
2729

2830
var reqBytes []byte
29-
reqBytes, err := b.marshaller.marshal(request)
31+
reqBytes, err := b.marshaller.Marshal(request)
3032
if err != nil {
3133
return nil, err
3234
}

request_builder_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type (
1919
failingMarshaller struct{}
2020
)
2121

22-
func (*failingMarshaller) marshal(_ any) ([]byte, error) {
22+
func (*failingMarshaller) Marshal(_ any) ([]byte, error) {
2323
return []byte{}, errTestMarshallerFailed
2424
}
2525

stream.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"errors"
77
"net/http"
8+
9+
utils "github.com/sashabaranov/go-openai/internal"
810
)
911

1012
var (
@@ -54,7 +56,7 @@ func (c *Client) CreateCompletionStream(
5456
reader: bufio.NewReader(resp.Body),
5557
response: resp,
5658
errAccumulator: newErrorAccumulator(),
57-
unmarshaler: &jsonUnmarshaler{},
59+
unmarshaler: &utils.JSONUnmarshaler{},
5860
},
5961
}
6062
return

stream_reader.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
10+
utils "github.com/sashabaranov/go-openai/internal"
911
)
1012

1113
type streamable interface {
@@ -19,7 +21,7 @@ type streamReader[T streamable] struct {
1921
reader *bufio.Reader
2022
response *http.Response
2123
errAccumulator errorAccumulator
22-
unmarshaler unmarshaler
24+
unmarshaler utils.Unmarshaler
2325
}
2426

2527
func (stream *streamReader[T]) Recv() (response T, err error) {
@@ -63,7 +65,7 @@ waitForData:
6365
return
6466
}
6567

66-
err = stream.unmarshaler.unmarshal(line, &response)
68+
err = stream.unmarshaler.Unmarshal(line, &response)
6769
return
6870
}
6971

unmarshaler.go

-15
This file was deleted.

0 commit comments

Comments
 (0)