Skip to content

Commit

Permalink
Add de/serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Sep 21, 2023
1 parent eef9f97 commit b6b90c0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 45 deletions.
10 changes: 7 additions & 3 deletions body.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,21 @@ func BodyBytes(b []byte) BodyGetter {
}
}

// BodyJSON is a BodyGetter that marshals a JSON object.
func BodyJSON(v any) BodyGetter {
func BodySerializer(s Serializer, v any) BodyGetter {
return func() (io.ReadCloser, error) {
b, err := jsonMarshal(v)
b, err := s(v)
if err != nil {
return nil, err
}
return rc(bytes.NewReader(b)), nil
}
}

// BodyJSON is a BodyGetter that marshals a JSON object.
func BodyJSON(v any) BodyGetter {
return BodySerializer(JSONSerializer, v)
}

// BodyForm is a BodyGetter that builds an encoded form body.
func BodyForm(data url.Values) BodyGetter {
return func() (r io.ReadCloser, err error) {
Expand Down
10 changes: 10 additions & 0 deletions builder_extras.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func (rb *Builder) BodyBytes(b []byte) *Builder {
return rb.Body(BodyBytes(b))
}

func (rb *Builder) BodySerializer(s Serializer, v any) *Builder {
return rb.
Body(BodySerializer(s, v))
}

// BodyJSON sets the Builder's request body to the marshaled JSON.
// It also sets ContentType to "application/json".
func (rb *Builder) BodyJSON(v any) *Builder {
Expand Down Expand Up @@ -169,6 +174,11 @@ func (rb *Builder) CheckPeek(n int, f func([]byte) error) *Builder {
return rb.AddValidator(CheckPeek(n, f))
}

func (rb *Builder) ToDeserializer(d Deserializer, v any) *Builder {
return rb.
Handle(ToDeserializer(d, v))
}

// ToJSON sets the Builder to decode a response as a JSON object
func (rb *Builder) ToJSON(v any) *Builder {
return rb.Handle(ToJSON(v))
Expand Down
10 changes: 7 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,24 @@ func consumeBody(res *http.Response) (err error) {
return err
}

// ToJSON decodes a response as a JSON object.
func ToJSON(v any) ResponseHandler {
func ToDeserializer(d Deserializer, v any) ResponseHandler {
return func(res *http.Response) error {
data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if err = jsonUnmarshal(data, v); err != nil {
if err = d(data, v); err != nil {
return err
}
return nil
}
}

// ToJSON decodes a response as a JSON object.
func ToJSON(v any) ResponseHandler {
return ToDeserializer(JSONDeserializer, v)
}

// ToString writes the response body to the provided string pointer.
func ToString(sp *string) ResponseHandler {
return func(res *http.Response) error {
Expand Down
23 changes: 4 additions & 19 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,10 @@ import (
"encoding/json"
)

type jsonMarshaller = func(v any) ([]byte, error)
type jsonUnmarshaller = func(data []byte, v any) error
type Serializer = func(v any) ([]byte, error)
type Deserializer = func(data []byte, v any) error

var (
jsonMarshal = json.Marshal
jsonUnmarshal = json.Unmarshal
JSONSerializer = json.Marshal
JSONDeserializer = json.Unmarshal
)

func SetJSONUnmarshaller(j jsonUnmarshaller) {
jsonUnmarshal = j
}

func SetJSONMarshaller(j jsonMarshaller) {
jsonMarshal = j
}

// SetJSONSerializers is a function to set global json.Marshal/json.Unmarshal function
// For faster serialization/deserialization you can use functions from, for instance, https://github.com/goccy/go-json
func SetJSONSerializers(marshaller jsonMarshaller, unmarshaller jsonUnmarshaller) {
jsonMarshal = marshaller
jsonUnmarshal = unmarshaller
}
9 changes: 1 addition & 8 deletions reqxml/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ package reqxml

import (
"encoding/xml"
"io"

"github.com/carlmjohnson/requests"
)

// Body is a BodyGetter that marshals a XML object.
func Body(v any) requests.BodyGetter {
return func() (io.ReadCloser, error) {
b, err := xml.Marshal(v)
if err != nil {
return nil, err
}
return requests.BodyBytes(b)()
}
return requests.BodySerializer(xml.Marshal, v)
}

// BodyConfig sets the Builder's request body to the marshaled XML.
Expand Down
13 changes: 1 addition & 12 deletions reqxml/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@ package reqxml

import (
"encoding/xml"
"io"
"net/http"

"github.com/carlmjohnson/requests"
)

// To decodes a response as an XML object.
func To(v any) requests.ResponseHandler {
return func(res *http.Response) error {
data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if err = xml.Unmarshal(data, v); err != nil {
return err
}
return nil
}
return requests.ToDeserializer(xml.Unmarshal, v)
}

0 comments on commit b6b90c0

Please sign in to comment.