encoding

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2024 License: MIT Imports: 9 Imported by: 2

README

encoding

http encoding toolkit

Go.Dev reference codecov Tests Go Report Card Licence Tag

Usage

encoding是一个http编解码器.

Installation

Use go get.

    go get github.com/things-go/encoding

Then import the package into your own code.

    import "github.com/things-go/encoding"
Example

References

License

This project is under MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
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

func (r *Encoding) Bind(req *http.Request, v any) error

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

func (r *Encoding) BindQuery(req *http.Request, v any) error

BindQuery binds the passed struct pointer using the query codec.Marshaler.

func (*Encoding) BindUri

func (r *Encoding) BindUri(raws url.Values, v any) error

BindUri binds the passed struct pointer using the uri codec.Marshaler.

func (*Encoding) Delete

func (r *Encoding) Delete(mime string) error

Delete remove the MIME type marshaler. MIMEWildcard, MIMEQuery, MIMEURI should be always exist and valid.

func (*Encoding) Encode

func (r *Encoding) Encode(contentType string, v any) ([]byte, error)

Encode encode v use contentType

func (*Encoding) EncodeQuery

func (r *Encoding) EncodeQuery(v any) (url.Values, error)

EncodeQuery encode v to the query url.Values.

func (*Encoding) EncodeURL

func (r *Encoding) EncodeURL(athTemplate string, msg any, needQuery bool) string

EncodeURL encode msg to url path. pathTemplate is a template of url path like http://helloworld.dev/{name}/sub/{sub.name},

func (*Encoding) Get

func (r *Encoding) Get(mime string) codec.Marshaler

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

func (r *Encoding) InboundForRequest(req *http.Request) (string, codec.Marshaler)

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

func (r *Encoding) InboundForResponse(resp *http.Response) codec.Marshaler

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

func (r *Encoding) OutboundForRequest(req *http.Request) codec.Marshaler

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

func (r *Encoding) Register(mime string, marshaler codec.Marshaler) error

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

func (r *Encoding) Render(w http.ResponseWriter, req *http.Request, v any) error

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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