Skip to content

Commit b71651b

Browse files
authored
Merge pull request #202 from alnr/master
fix: type signature for ImportDocumentResponse
2 parents ed319f0 + 8d6e960 commit b71651b

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

typesense/api/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package api
33
type ImportDocumentResponse struct {
44
Success bool `json:"success"`
55
Error string `json:"error"`
6-
Document string `json:"document"`
6+
Document any `json:"document"` // on success: map[string]interface{}; on error: string
77
Id string `json:"id"`
88
}
99

typesense/documents.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package typesense
22

33
import (
4+
"bufio"
45
"bytes"
56
"context"
67
"encoding/json"
78
"errors"
9+
"fmt"
810
"io"
911
"net/http"
1012

@@ -141,18 +143,20 @@ func (d *documents) Import(ctx context.Context, documents []interface{}, params
141143
if err != nil {
142144
return nil, err
143145
}
146+
defer response.Close()
144147

145148
var result []*api.ImportDocumentResponse
146-
jsonDecoder := json.NewDecoder(response)
147-
for jsonDecoder.More() {
149+
scanner := bufio.NewScanner(response)
150+
scanner.Split(bufio.ScanLines)
151+
for scanner.Scan() {
148152
var docResult *api.ImportDocumentResponse
149-
if err := jsonDecoder.Decode(&docResult); err != nil {
150-
return result, errors.New("failed to decode result")
153+
if err := json.Unmarshal(scanner.Bytes(), &docResult); err != nil {
154+
return result, fmt.Errorf("failed to decode result: %w", err)
151155
}
152156
result = append(result, docResult)
153157
}
154158

155-
return result, nil
159+
return result, scanner.Err()
156160
}
157161

158162
func (d *documents) ImportJsonl(ctx context.Context, body io.Reader, params *api.ImportDocumentsParams) (io.ReadCloser, error) {

typesense/import_test.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"io"
88
"net/http"
9-
"reflect"
109
"strings"
1110
"testing"
1211

@@ -30,15 +29,15 @@ func eqReader(r io.Reader) gomock.Matcher {
3029
}
3130

3231
func (m *eqReaderMatcher) Matches(x interface{}) bool {
33-
if _, ok := x.(io.Reader); !ok {
32+
r, ok := x.(io.Reader)
33+
if !ok {
3434
return false
3535
}
36-
r := x.(io.Reader)
3736
allBytes, err := io.ReadAll(r)
3837
if err != nil {
3938
panic(err)
4039
}
41-
return reflect.DeepEqual(allBytes, m.readerBytes)
40+
return bytes.Equal(allBytes, m.readerBytes)
4241
}
4342

4443
func (m *eqReaderMatcher) String() string {
@@ -197,26 +196,38 @@ func TestDocumentsImportOnHttpStatusErrorCodeReturnsError(t *testing.T) {
197196
assert.NotNil(t, err)
198197
}
199198

200-
func TestDocumentsImportWithTwoDocuments(t *testing.T) {
201-
expectedParams := &api.ImportDocumentsParams{
199+
func TestDocumentsImportWithTwoSuccessesAndOneFailure(t *testing.T) {
200+
params := &api.ImportDocumentsParams{
202201
Action: pointer.Any(api.Create),
203202
BatchSize: pointer.Int(40),
204-
}
205-
expectedBody := strings.NewReader(`{"id":"123","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}` +
206-
"\n" + `{"id":"125","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}` + "\n")
207-
expectedResultString := `{"success": true, "id":"123"}` + "\n" + `{"success": false, "id":"125", "error": "Bad JSON.", "document": "[bad doc"}`
203+
ReturnId: pointer.Any(true),
204+
ReturnDoc: pointer.Any(true),
205+
}
206+
expectedBody := `{"id":"123","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}
207+
{"id":"125","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}
208+
"[bad doc"
209+
`
210+
expectedResultString := `{"success": true, "id":"123","document": {"id":"123","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}}
211+
{"success": true, "id":"125", "document": {"id":"125","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}}
212+
{"success": false, "id":"", "error": "Bad JSON.", "document": "[bad doc"}
213+
`
208214
expectedResult := []*api.ImportDocumentResponse{
209-
{Success: true, Id: "123"},
210-
{Success: false, Id: "125", Error: "Bad JSON.", Document: "[bad doc"},
215+
{Success: true, Id: "123", Document: map[string]interface{}{"id": "123", "companyName": "Stark Industries", "numEmployees": float64(5215), "country": "USA"}},
216+
{Success: true, Id: "125", Document: map[string]interface{}{"id": "125", "companyName": "Stark Industries", "numEmployees": float64(5215), "country": "USA"}},
217+
{Success: false, Id: "", Error: "Bad JSON.", Document: "[bad doc"},
211218
}
212219

213220
ctrl := gomock.NewController(t)
214221
defer ctrl.Finish()
215222
mockAPIClient := mocks.NewMockAPIClientInterface(ctrl)
216223

217224
mockAPIClient.EXPECT().
218-
ImportDocumentsWithBody(gomock.Not(gomock.Nil()),
219-
"companies", expectedParams, "application/octet-stream", eqReader(expectedBody)).
225+
ImportDocumentsWithBody(
226+
gomock.Not(gomock.Nil()),
227+
"companies",
228+
params,
229+
"application/octet-stream",
230+
eqReader(strings.NewReader(expectedBody))).
220231
Return(&http.Response{
221232
StatusCode: http.StatusOK,
222233
Body: io.NopCloser(strings.NewReader(expectedResultString)),
@@ -227,10 +238,7 @@ func TestDocumentsImportWithTwoDocuments(t *testing.T) {
227238
documents := []interface{}{
228239
createNewDocument("123"),
229240
createNewDocument("125"),
230-
}
231-
params := &api.ImportDocumentsParams{
232-
Action: pointer.Any(api.Create),
233-
BatchSize: pointer.Int(40),
241+
"[bad doc",
234242
}
235243
result, err := client.Collection("companies").Documents().Import(context.Background(), documents, params)
236244

0 commit comments

Comments
 (0)