encoding

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 17 Imported by: 2

README

encoding

http encoding toolkit

GoDoc 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

func FromRequestUri deprecated

func FromRequestUri(req *http.Request) url.Values

FromRequestUri returns the route variables for the current request, if any.

Deprecated: Because BindUri is deprecated.

func RequestWithUri deprecated

func RequestWithUri(req *http.Request, uri url.Values) *http.Request

RequestWithUri sets the URL variables for the given request, Arguments are not modified, a shallow copy is returned. URL variables can be set by making a route that captures the required variables, starting a server and sending the request to that server.

Deprecated: Because BindUri is deprecated.

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

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 added in v0.1.0

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

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

func (*Encoding) BindUri deprecated

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

BindUri binds the passed struct pointer using the uri codec.Marshaler. NOTE: before use this, you should set uri params in the request context with RequestWithUri.

Deprecated: Use BindURI instead.

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.

type HTTPBodyCodec

type HTTPBodyCodec struct {
	codec.Marshaler
}

HTTPBodyCodec is a Marshaler which supports marshaling of a google.api.HttpBody message as the full response body if it is the actual message used as the response. If not, then this will simply fallback to the Marshaler specified as its default Marshaler.

func (*HTTPBodyCodec) ContentType

func (h *HTTPBodyCodec) ContentType(v any) string

ContentType returns its specified content type in case v is a google.api.HttpBody message, otherwise it will fall back to the default Marshalers content type.

func (*HTTPBodyCodec) Marshal

func (h *HTTPBodyCodec) Marshal(v any) ([]byte, error)

Marshal marshals "v" by returning the body bytes if v is a google.api.HttpBody message, otherwise it falls back to the default Marshaler.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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