rest

package
v0.0.0-...-18a359a Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxMemory = 32 << 20 // 32mb

DefaultMaxMemory defines the default max memory bytes that will be used when parsing a form request body.

View Source
const MultipartJsonKey string = "@jsonPayload"

MultipartJsonKey is the key for the special multipart/form-data handling allowing reading serialized json payload without normalization.

Variables

This section is empty.

Functions

func BindBody

func BindBody(c echo.Context, i any) error

BindBody binds request body content to i.

This is similar to `echo.BindBody()`, but for JSON requests uses custom json reader that **copies** the request body, allowing multiple reads.

func CopyJsonBody

func CopyJsonBody(r *http.Request, i any) error

CopyJsonBody reads the request body into i by creating a copy of `r.Body` to allow multiple reads.

func FindUploadedFiles

func FindUploadedFiles(r *http.Request, key string) ([]*filesystem.File, error)

FindUploadedFiles extracts all form files of "key" from a http request and returns a slice with filesystem.File instances (if any).

func NormalizeUrl

func NormalizeUrl(originalUrl string) (string, error)

NormalizeUrl removes duplicated slashes from a url path.

func PickFields

func PickFields(data any, rawFields string) (any, error)

PickFields parses the provided fields string expression and returns a new subset of data with only the requested fields.

Fields transformations with modifiers are also supported (see initModifer()).

Example:

data := map[string]any{"a": 1, "b": 2, "c": map[string]any{"c1": 11, "c2": 22}}
PickFields(data, "a,c.c1") // map[string]any{"a": 1, "c": map[string]any{"c1": 11}}

Types

type FieldModifier

type FieldModifier interface {
	// Modify executes the modifier and returns a new modified value.
	Modify(value any) (any, error)
}

type MultiBinder

type MultiBinder struct{}

MultiBinder is similar to echo.DefaultBinder but uses slightly different application/json and multipart/form-data bind methods to accommodate better the PocketBase router needs.

func (*MultiBinder) Bind

func (b *MultiBinder) Bind(c echo.Context, i interface{}) (err error)

Bind implements the [Binder.Bind] method.

Bind is almost identical to echo.DefaultBinder.Bind but uses the rest.BindBody function for binding the request body.

type Serializer

type Serializer struct {
	echo.DefaultJSONSerializer

	FieldsParam string
}

Serializer represents custom REST JSON serializer based on echo.DefaultJSONSerializer, with support for additional generic response data transformation (eg. fields picker).

func (*Serializer) Serialize

func (s *Serializer) Serialize(c echo.Context, i any, indent string) error

Serialize converts an interface into a json and writes it to the response.

It also provides a generic response data fields picker via the FieldsParam query parameter (default to "fields").

Note: for the places where it is safe, the std encoding/json is replaced with goccy due to its slightly better Unmarshal/Marshal performance.

Jump to

Keyboard shortcuts

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