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. Mime_Query = "__MIME__/QUERY" // Mime_Uri is special form uri. Mime_Uri = "__MIME__/URI" // Mime_Wildcard is the fallback special MIME type used for requests which do not match // a registered MIME type. Mime_Wildcard = "*" Mime_JSON = "application/json" Mime_HTML = "text/html" Mime_XML = "application/xml" Mime_XML2 = "text/xml" Mime_Plain = "text/plain" Mime_PostForm = "application/x-www-form-urlencoded" Mime_MultipartPostForm = "multipart/form-data" Mime_PROTOBUF = "application/x-protobuf" Mime_MSGPACK = "application/x-msgpack" Mime_MSGPACK2 = "application/msgpack" Mime_YAML = "application/x-yaml" Mime_TOML = "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:
Mime_PostForm: form.Codec Mime_MultipartPostForm: form.MultipartCodec Mime_JSON: json.Codec mime_Query: form.QueryCodec mime_Uri: form.UriCodec mime_Wildcard: json.Codec
you can manually register your custom Marshaler.
Mime_PROTOBUF: proto.Codec Mime_XML: xml.Codec Mime_XML2: xml.Codec Mime_MSGPACK: msgpack.Codec Mime_MSGPACK2: msgpack.Codec Mime_YAML: yaml.Codec Mime_TOML: 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 ¶
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.