Skip to content

Commit

Permalink
Docs: More serializer documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Oct 6, 2023
1 parent 2c42bf3 commit 1393bf3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
8 changes: 5 additions & 3 deletions body.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func BodyBytes(b []byte) BodyGetter {
}
}

// BodySerializer is a BodyGetter
// that uses the provided [Serializer]
// to build the body of a request from v.
func BodySerializer(s Serializer, v any) BodyGetter {
return func() (io.ReadCloser, error) {
b, err := s(v)
Expand All @@ -53,9 +56,8 @@ func BodySerializer(s Serializer, v any) BodyGetter {
}
}

// BodyJSON is a BodyGetter that marshals a JSON object.
//
// It uses [JSONSerializer] to marshal the object.
// BodyJSON is a [BodySerializer]
// that uses [JSONSerializer] to marshal the object.
func BodyJSON(v any) BodyGetter {
return BodySerializer(JSONSerializer, v)
}
Expand Down
6 changes: 5 additions & 1 deletion builder_extras.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (rb *Builder) BodyBytes(b []byte) *Builder {
return rb.Body(BodyBytes(b))
}

// BodySerializer sets the Builder's request body
// to the serialized object.
func (rb *Builder) BodySerializer(s Serializer, v any) *Builder {
return rb.
Body(BodySerializer(s, v))
Expand Down Expand Up @@ -175,12 +177,14 @@ func (rb *Builder) CheckPeek(n int, f func([]byte) error) *Builder {
return rb.AddValidator(CheckPeek(n, f))
}

// ToDeserializer sets the Builder to decode a response into v
// using a [Deserializer].
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
// ToJSON sets the Builder to decode a response as a JSON object.
//
// It uses [JSONDeserializer] to unmarshal the object.
func (rb *Builder) ToJSON(v any) *Builder {
Expand Down
1 change: 1 addition & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func consumeBody(res *http.Response) (err error) {
return err
}

// ToDeserializer decodes a response into v using a [Deserializer].
func ToDeserializer(d Deserializer, v any) ResponseHandler {
return func(res *http.Response) error {
data, err := io.ReadAll(res.Body)
Expand Down

0 comments on commit 1393bf3

Please sign in to comment.