Skip to content

Commit

Permalink
fix: fixed nil payload behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
murillovaz committed Sep 9, 2023
1 parent e5fa4d9 commit 0bd12c6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
22 changes: 17 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,31 @@ func NewRequest(method string, url string, body interface{}, parserType ...parse
}, nil
}

func isPayloadEmpty(payload any) bool {
if value, ok := payload.(*any); ok {
if *value == nil {
return true
}
}

return false
}

func (c *DynaClient[T]) Do(req *DynaRequest) (*T, *DynaResponse, error) {
encoder := c.DynaParser.Encode
if req.DynaEncoder != nil {
encoder = req.DynaEncoder.Encode
}

body, err := encoder(req.payload)
if !isPayloadEmpty(req.payload) {
body, err := encoder(req.payload)

if err != nil {
return nil, nil, err
}
if err != nil {
return nil, nil, err
}

req.Body = body
req.Body = body
}

response, err := c.Client.Do(req.Request)

Expand Down
3 changes: 0 additions & 3 deletions pkg/parser/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ func Decode[T any](reader io.Reader) (*T, error) {
}

func Encode(payload any) (io.ReadCloser, error) {
if payload == nil {
return nil, nil
}
b, err := js.Marshal(payload)

if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/parser/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ func Decode[T any](reader io.Reader) (*T, error) {
}

func Encode(payload any) (io.ReadCloser, error) {
if payload == nil {
return nil, nil
}
payloadValue := *payload.(*any)

return io.NopCloser(bytes.NewReader([]byte(payloadValue.(string)))), nil
Expand Down
4 changes: 0 additions & 4 deletions pkg/parser/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ func Decode[T any](reader io.Reader) (*T, error) {
}

func Encode(payload any) (io.ReadCloser, error) {
if payload == nil {
return nil, nil
}

b, err := xml.Marshal(payload)

if err != nil {
Expand Down

0 comments on commit 0bd12c6

Please sign in to comment.