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: refactor built-in json with easyjson #582

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 0 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ You can run integration test and linter check by command:
make test
```

### EasyJson <!-- omit in TOC -->

[`easyjson`](https://github.com/mailru/easyjson) is a package used for optimizing marshal/unmarshal Go structs to/from JSON.
It takes the `types.go` file as an input, and auto-generates `types_easyjson.go` with optimized
marshalling and unmarshalling methods for this SDK.

If for any reason `types.go` is modified, this file should be regenerated by running easyjson again.

```shell
make easyjson
```

## Git Guidelines

### Git Branches <!-- omit in TOC -->
Expand Down
67 changes: 19 additions & 48 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,39 +161,15 @@ func (c *client) sendRequest(
buf := c.bufferPool.Get().(*bytes.Buffer)
buf.Reset()

if b, ok := rawRequest.([]byte); ok {
buf.Write(b)
body = buf
} else if reader, ok := rawRequest.(io.Reader); ok {
// If the request body is an io.Reader then stream it directly
body = reader
} else {
// Otherwise convert it to JSON
var (
data []byte
err error
)
if marshaler, ok := rawRequest.(json.Marshaler); ok {
data, err = marshaler.MarshalJSON()
if err != nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to marshal with MarshalJSON: %w", err))
}
if data == nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
errors.New("MarshalJSON returned nil data"))
}
} else {
data, err = json.Marshal(rawRequest)
if err != nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to marshal with json.Marshal: %w", err))
}
}
buf.Write(data)
body = buf
data, err := json.Marshal(rawRequest)
if err != nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to marshal with json.Marshal: %w", err))
}

buf.Write(data)
body = buf

if !c.contentEncoding.IsZero() {
body, err = c.encoder.Encode(body)
if err != nil {
Expand Down Expand Up @@ -314,31 +290,26 @@ func (c *client) handleStatusCode(req *internalRequest, statusCode int, body []b
return nil
}

func (c *client) handleResponse(req *internalRequest, body []byte, internalError *Error) (err error) {
func (c *client) handleResponse(req *internalRequest, body []byte, internalError *Error) error {
if req.withResponse != nil {
if !c.contentEncoding.IsZero() {
if err := c.encoder.Decode(body, req.withResponse); err != nil {
return internalError.WithErrCode(ErrCodeResponseUnmarshalBody, err)
}
} else {
internalError.ResponseToString = string(body)
return nil
}

if internalError.ResponseToString == nullBody {
req.withResponse = nil
return nil
}
internalError.ResponseToString = string(body)

var err error
if resp, ok := req.withResponse.(json.Unmarshaler); ok {
err = resp.UnmarshalJSON(body)
req.withResponse = resp
} else {
err = json.Unmarshal(body, req.withResponse)
}
if err != nil {
return internalError.WithErrCode(ErrCodeResponseUnmarshalBody, err)
}
if internalError.ResponseToString == nullBody {
req.withResponse = nil
return nil
}

if err := json.Unmarshal(body, req.withResponse); err != nil {
return internalError.WithErrCode(ErrCodeResponseUnmarshalBody, err)
}
}

return nil
}
Loading
Loading