simplify unmarshal (#191)

* simplify unmarshal

* simplify unmarshalError

* rename errorAccumulate -> defaultErrorAccumulator

* update converage
This commit is contained in:
sashabaranov
2023-03-22 09:56:05 +04:00
committed by GitHub
parent a5a945ad14
commit eb68a72bcc
3 changed files with 29 additions and 22 deletions

View File

@@ -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
}