Documentation ¶
Index ¶
- Constants
- type Encoding
- func (r *Encoding) Bind(req *http.Request, v any) error
- func (r *Encoding) BindQuery(req *http.Request, v any) error
- func (r *Encoding) BindUri(raws url.Values, v any) error
- func (r *Encoding) Delete(mime string) error
- func (r *Encoding) Encode(contentType string, v any) ([]byte, error)
- func (r *Encoding) EncodeQuery(v any) (url.Values, error)
- func (r *Encoding) EncodeURL(athTemplate string, msg any, needQuery bool) string
- func (r *Encoding) Get(mime string) codec.Marshaler
- func (r *Encoding) InboundForRequest(req *http.Request) (string, codec.Marshaler)
- func (r *Encoding) InboundForResponse(resp *http.Response) codec.Marshaler
- func (r *Encoding) OutboundForRequest(req *http.Request) codec.Marshaler
- func (r *Encoding) Register(mime string, marshaler codec.Marshaler) error
- func (r *Encoding) Render(w http.ResponseWriter, req *http.Request, v any) error
Constants ¶
const ( // MIMEURI is special form query. MIMEQuery = "__MIME__/QUERY" // MIMEURI is special form uri. MIMEURI = "__MIME__/URI" // MIMEWildcard is the fallback special MIME type used for requests which do not match // a registered MIME type. MIMEWildcard = "*" MIMEJSON = "application/json" MIMEHTML = "text/html" MIMEXML = "application/xml" MIMEXML2 = "text/xml" MIMEPlain = "text/plain" MIMEPOSTForm = "application/x-www-form-urlencoded" MIMEMultipartPOSTForm = "multipart/form-data" MIMEPROTOBUF = "application/x-protobuf" MIMEMSGPACK = "application/x-msgpack" MIMEMSGPACK2 = "application/msgpack" MIMEYAML = "application/x-yaml" MIMETOML = "application/toml" )
Content-Type MIME of the most common data formats.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Encoding ¶
type Encoding struct {
// contains filtered or unexported fields
}
Encoding is a mapping from MIME types to Marshalers.
func New ¶
func New() *Encoding
New encoding with default Marshalers Default:
MIMEPOSTForm: form.Codec MIMEMultipartPOSTForm: form.MultipartCodec MIMEJSON: json.Codec mimeQuery: form.QueryCodec mimeUri: form.UriCodec mimeWildcard: json.Codec
you can manually register your custom Marshaler.
MIMEPROTOBUF: proto.Codec MIMEXML: xml.Codec MIMEXML2: xml.Codec MIMEMSGPACK: msgpack.Codec MIMEMSGPACK2: msgpack.Codec MIMEYAML: yaml.Codec MIMETOML: toml.Codec
func (*Encoding) Bind ¶
Bind checks the Method and Content-Type to select codec.Marshaler automatically, Depending on the "Content-Type" header different bind are used, for example:
"application/json" --> JSON codec.Marshaler "application/xml" --> XML codec.Marshaler
It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer.
func (*Encoding) BindQuery ¶
BindQuery binds the passed struct pointer using the query codec.Marshaler.
func (*Encoding) Delete ¶
Delete remove the MIME type marshaler. MIMEWildcard, MIMEQuery, MIMEURI should be always exist and valid.
func (*Encoding) EncodeQuery ¶
EncodeQuery encode v to the query url.Values.
func (*Encoding) EncodeURL ¶
EncodeURL encode msg to url path. pathTemplate is a template of url path like http://helloworld.dev/{name}/sub/{sub.name},
func (*Encoding) Get ¶
Get returns the marshalers with a case-sensitive MIME type string It checks the MIME type on the Encoding. Otherwise, it follows the above logic for "*" Marshaler.
func (*Encoding) InboundForRequest ¶
InboundForRequest returns the inbound `Content-Type` and marshalers for this request. It checks the registry on the Encoding for the MIME type set by the `Content-Type` header. If it isn't set (or the request `Content-Type` is empty), checks for "*". If there are multiple `Content-Type` headers set, choose the first one that it can exactly match in the registry. Otherwise, it follows the above logic for "*" Marshaler.
func (*Encoding) InboundForResponse ¶ added in v0.0.2
InboundForResponse returns the inbound marshaler for this response. It checks the registry on the Encoding for the MIME type set by the `Content-Type` header. If it isn't set (or the response `Content-Type` is empty), checks for "*". If there are multiple `Content-Type` headers set, choose the first one that it can exactly match in the registry. Otherwise, it follows the above logic for "*" Marshaler.
func (*Encoding) OutboundForRequest ¶
OutboundForRequest returns the marshalers for this request. It checks the registry on the Encoding for the MIME type set by the `Accept` header. If it isn't set (or the request `Accept` is empty), checks for "*". If there are multiple `Accept` headers set, choose the first one that it can exactly match in the registry. Otherwise, it follows the above logic for "*" Marshaler.
func (*Encoding) Register ¶
Register a marshaler for a case-sensitive MIME type string ("*" to match any MIME type). you can override default marshaler with same MIME type
func (*Encoding) Render ¶
Render writes the response headers and calls the outbound marshalers for this request. It checks the registry on the Encoding for the MIME type set by the Accept header. If it isn't set (or the request Accept is empty), checks for "*". for example:
"application/json" --> JSON codec.Marshaler "application/xml" --> XML codec.Marshaler
If there are multiple Accept headers set, choose the first one that it can exactly match in the registry. Otherwise, it follows the above logic for "*" Marshaler.