From 0bd12c60aa89c157d6673eec1c4c258fe9c91d72 Mon Sep 17 00:00:00 2001 From: MurilloVaz Date: Sat, 9 Sep 2023 05:16:32 -0300 Subject: [PATCH] fix: fixed nil payload behavior --- pkg/client/client.go | 22 +++++++++++++++++----- pkg/parser/json/json.go | 3 --- pkg/parser/text/text.go | 3 --- pkg/parser/xml/xml.go | 4 ---- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 4cc34ee..7539a23 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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) diff --git a/pkg/parser/json/json.go b/pkg/parser/json/json.go index 5bf647a..cd0fcef 100644 --- a/pkg/parser/json/json.go +++ b/pkg/parser/json/json.go @@ -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 { diff --git a/pkg/parser/text/text.go b/pkg/parser/text/text.go index 36165c2..a8f819c 100644 --- a/pkg/parser/text/text.go +++ b/pkg/parser/text/text.go @@ -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 diff --git a/pkg/parser/xml/xml.go b/pkg/parser/xml/xml.go index cdced48..b68fbf9 100644 --- a/pkg/parser/xml/xml.go +++ b/pkg/parser/xml/xml.go @@ -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 {