Documentation
¶
Index ¶
- func AssignPrimitive(value reflect.Value, str string) error
- func AssignPrimitives(value reflect.Value, strs []string) error
- func EncodePrimitive(value reflect.Value) (string, error)
- func EncodePrimitives(value reflect.Value) ([]string, error)
- type BinarySerializer
- type Cart
- func A[TInput any, TOutput any](h CartFunc[TInput, TOutput]) Cart
- func I[TInput any, TOutput any](input Serializer[TInput], h CartFunc[TInput, TOutput]) Cart
- func IO[TInput any, TOutput any](input Serializer[TInput], output Serializer[TOutput], ...) Cart
- func O[TInput any, TOutput any](output Serializer[TOutput], h CartFunc[TInput, TOutput]) Cart
- type CartFunc
- type CartInformation
- type Decoder
- type DecoderFactory
- type Encoder
- type EncoderFactory
- type HeaderDecoder
- type HeaderEncoder
- type HeaderWriter
- type MarshalSerializer
- type PathDecoder
- type Request
- type Serializer
- type UrlValuesDecoder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssignPrimitive ¶ added in v0.1.0
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
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
EncodePrimitive encodes the value of value as a string
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 (*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
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.
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 NewHeaderDecoder ¶ added in v0.1.0
func NewPathDecoder ¶ added in v0.1.0
func NewQueryDecoder ¶ added in v0.1.0
type DecoderFactory ¶ added in v0.1.0
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 ¶
Request is a http.Request that also contains the already deserialized body, which can then be retrieved by calling 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 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)