gocart

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssignPrimitive added in v0.1.0

func AssignPrimitive(value reflect.Value, str string) error

AssignPrimitive is used when decoding, to convert the str to the appropriate primitive and then assign it to value

func AssignPrimitives added in v0.1.0

func AssignPrimitives(value reflect.Value, strs []string) error

AssignPrimitives is used when decoding, to convert the strs to the appropriate primitives and then assign them to value

func EncodePrimitive added in v0.1.0

func EncodePrimitive(value reflect.Value) (string, error)

EncodePrimitive encodes the value of value as a string

func EncodePrimitives added in v0.1.0

func EncodePrimitives(value reflect.Value) ([]string, error)

EncodePrimitives encodes the value(s) of value as a string and if multiple separates them by a "," (see url.Values)

Types

type BinarySerializer added in v0.1.0

type BinarySerializer struct {
	// contains filtered or unexported fields
}

func (*BinarySerializer) Deserialize added in v0.1.0

func (receiver *BinarySerializer) Deserialize(data []byte, headers http.Header) (*[]byte, error)

func (*BinarySerializer) Serialize added in v0.1.0

func (receiver *BinarySerializer) Serialize(body *[]byte, headers http.Header) ([]byte, error)

func (*BinarySerializer) Type added in v0.1.0

func (receiver *BinarySerializer) Type() *goflag.Type

type Cart added in v0.0.4

type Cart interface {
	goflag.EndpointFlag
	WithInfo(fn func(info *CartInformation)) Cart
}

Cart represents a gotrac.EndpointFlag that automatically can (de)serialize the request/response

func A added in v0.0.5

func A[TInput any, TOutput any](h CartFunc[TInput, TOutput]) Cart

A is used to define a Cart that does NOT deserialize it's input and does NOT serialize the output.

However, TInput and TOutput can still be specified to allow for meta-fields!

func I added in v0.0.5

func I[TInput any, TOutput any](input Serializer[TInput], h CartFunc[TInput, TOutput]) Cart

I is used to define a Cart that deserializes it's input but does NOT serialize the output.

However, TOutput can still be specified to allow for meta-fields!

func IO added in v0.0.5

func IO[TInput any, TOutput any](input Serializer[TInput], output Serializer[TOutput], h CartFunc[TInput, TOutput]) Cart

IO is used to define a Cart that deserializes it's input and serializes the output.

func O added in v0.0.5

func O[TInput any, TOutput any](output Serializer[TOutput], h CartFunc[TInput, TOutput]) Cart

O is used to define a Cart that does NOT deserialize it's input but does serialize the output.

However, TInput can still be specified to allow for meta-fields!

type CartFunc added in v0.0.4

type CartFunc[TInput any, TOutput any] func(request *Request[TInput], writer HeaderWriter) (*TOutput, error)

CartFunc is the actual handler that gets the deserialized request (and a HeaderWriter) and returns the response body that will then be serialized by the cart.

type CartInformation added in v0.0.4

type CartInformation struct {
	// contains filtered or unexported fields
}

func (*CartInformation) WithDescription added in v0.0.4

func (actor *CartInformation) WithDescription(description string) *CartInformation

func (*CartInformation) WithHidden added in v0.0.4

func (actor *CartInformation) WithHidden(hidden bool) *CartInformation

func (*CartInformation) WithSummary added in v0.0.4

func (actor *CartInformation) WithSummary(summary string) *CartInformation

type Decoder added in v0.1.0

type Decoder interface {
	Decode(field reflect.StructField) ([]string, error)
}

Decoder reads data from HTTP headers, the query, formdata, etc. The returned strings are converted to the proper primitive types. If the receiving field is not an array, but multiple values are provided the first one will be used

func NewFormDecorder added in v0.1.0

func NewFormDecorder(request *http.Request) Decoder

func NewHeaderDecoder added in v0.1.0

func NewHeaderDecoder(request *http.Request) Decoder

func NewPathDecoder added in v0.1.0

func NewPathDecoder(request *http.Request) Decoder

func NewQueryDecoder added in v0.1.0

func NewQueryDecoder(request *http.Request) Decoder

type DecoderFactory added in v0.1.0

type DecoderFactory func(request *http.Request) Decoder

type Encoder added in v0.1.0

type Encoder interface {
	Encode(value reflect.Value, field reflect.StructField) error
}

Encoder sets data on the return headers and cookies

func NewHeaderEncoder added in v0.1.0

func NewHeaderEncoder(writer http.ResponseWriter) Encoder

type EncoderFactory added in v0.1.0

type EncoderFactory func(writer http.ResponseWriter) Encoder

type HeaderDecoder added in v0.1.0

type HeaderDecoder struct {
	// contains filtered or unexported fields
}

func (*HeaderDecoder) Decode added in v0.1.0

func (dec *HeaderDecoder) Decode(field reflect.StructField) ([]string, error)

type HeaderEncoder added in v0.1.0

type HeaderEncoder struct {
	// contains filtered or unexported fields
}

func (*HeaderEncoder) Encode added in v0.1.0

func (enc *HeaderEncoder) Encode(value reflect.Value, field reflect.StructField) error

type HeaderWriter

type HeaderWriter interface {
	// Header returns the meta map that will be sent by
	// [ResponseWriter.WriteHeader]. The [Header] map also is the mechanism with which
	// [Handler] implementations can set HTTP trailers.
	//
	// Changing the meta map after a call to [ResponseWriter.WriteHeader] (or
	// [ResponseWriter.Write]) has no effect unless the HTTP status code was of the
	// 1xx class or the modified headers are trailers.
	//
	// There are two ways to set Trailers. The preferred way is to
	// predeclare in the headers which trailers you will later
	// send by setting the "Trailer" meta to the names of the
	// trailer keys which will come later. In This case, those
	// keys of the Header map are treated as if they were
	// trailers. See the example. The second way, for trailer
	// keys not known to the [Handler] until after the first [ResponseWriter.Write],
	// is to prefix the [Header] map keys with the [TrailerPrefix]
	// constant value.
	//
	// To suppress automatic response headers (such as "Date"), set
	// their value to nil.
	Header() http.Header

	// WriteHeader sends an HTTP response meta with the provided
	// status code.
	//
	// If WriteHeader is not called explicitly, the first call to Write
	// will trigger an implicit WriteHeader(gocart.StatusOK).
	// Thus explicit calls to WriteHeader are mainly used to
	// send error codes or 1xx informational responses.
	//
	// The provided code must be a valid HTTP 1xx-5xx status code.
	// Any number of 1xx headers may be written, followed by at most
	// one 2xx-5xx meta. 1xx headers are sent immediately, but 2xx-5xx
	// headers may be buffered. Use the Flusher interface to send
	// buffered data. The meta map is cleared when 2xx-5xx headers are
	// sent, but not with 1xx headers.
	//
	// The server will automatically send a 100 (Continue) meta
	// on the first read from the request body if the request has
	// an "Expect: 100-continue" meta.
	WriteHeader(statusCode int)
}

HeaderWriter allows a CartFunc to write and read to the response header without being able to write the body. This helps to ensure the proper write flow, i.e. headers can not be written after the body.

type MarshalSerializer added in v0.1.0

type MarshalSerializer[T any] struct {
	// contains filtered or unexported fields
}

MarshalSerializer implements the Serializer interface using the go-convention marshal/unmarshal functions. It is for example used for Json, Yaml and Xml

func (*MarshalSerializer[T]) Deserialize added in v0.1.0

func (j *MarshalSerializer[T]) Deserialize(data []byte, headers http.Header) (*T, error)

func (*MarshalSerializer[T]) Serialize added in v0.1.0

func (j *MarshalSerializer[T]) Serialize(body *T, headers http.Header) ([]byte, error)

func (*MarshalSerializer[T]) Type added in v0.1.0

func (j *MarshalSerializer[T]) Type() *goflag.Type

type PathDecoder added in v0.1.0

type PathDecoder struct {
	// contains filtered or unexported fields
}

func (*PathDecoder) Decode added in v0.1.0

func (dec *PathDecoder) Decode(field reflect.StructField) ([]string, error)

type Request

type Request[TBody any] struct {
	http.Request
	// contains filtered or unexported fields
}

Request is a http.Request that also contains the already deserialized body, which can then be retrieved by calling Body().

func (*Request[TBody]) Body

func (r *Request[TBody]) Body() *TBody

Body returns a pointer to the already deserialized body.

type Serializer added in v0.1.0

type Serializer[T any] interface {
	Serialize(body *T, headers http.Header) ([]byte, error)
	Deserialize(data []byte, headers http.Header) (*T, error)
	Type() *goflag.Type
}

Serializer determines how the body of the http.Request should be decoded. Common types can be constructed using Json, Yaml, Xml, Binary, etc. However, the Serializer can also be implemented by you!

func Binary

func Binary(contentType ...string) Serializer[[]byte]

Binary Serializer just passes through the body, but it does allow you to set the MIME type yourself!

func Json

func Json[T any]() Serializer[T]

Json Serializer to decode the http.Request`s body

func Xml added in v0.1.0

func Xml[T any]() Serializer[T]

Xml Serializer to decode the http.Request`s body

func Yaml added in v0.1.0

func Yaml[T any]() Serializer[T]

Yaml Serializer to decode the http.Request`s body

type UrlValuesDecoder added in v0.1.0

type UrlValuesDecoder struct {
	// contains filtered or unexported fields
}

func (*UrlValuesDecoder) Decode added in v0.1.0

func (dec *UrlValuesDecoder) Decode(field reflect.StructField) ([]string, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL