transformer

package
v1.6.5-rc22 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const HTMLTsfName = "text/html"

Variables

View Source
var (
	ErrEncodeDataNotProtobuf   = errors.New("encode data must be `proto.Message`")
	ErrDecodeTargetNotProtobuf = errors.New("decode target must be `proto.Message`")
)
View Source
var DefaultFactory = &factory{}

Functions

func ContextWithFactory

func ContextWithFactory(ctx context.Context, f Factory) context.Context

func EachParameter

func EachParameter(_ context.Context, t typesx.Type, each func(p *Param) bool)

func EachReqParam

func EachReqParam(ctx context.Context, tpe typesx.Type, each func(rp *ReqParam)) error

func GroupedParamSetFromContext

func GroupedParamSetFromContext(ctx context.Context) *sync.Map

func MIMEHeader

func MIMEHeader(headers ...textproto.MIMEHeader) textproto.MIMEHeader

func MustNewFileHeader

func MustNewFileHeader(fieldName string, filename string, r io.Reader) *multipart.FileHeader

func NewFactory

func NewFactory() *factory

func NewFileHeader

func NewFileHeader(fieldName string, filename string, r io.Reader) (*multipart.FileHeader, error)

func NewValidator

func NewValidator(
	ctx context.Context,
	ft typesx.Type,
	tags map[string]reflectx.StructTag,
	omitempty bool,
	tsfm Transformer,
) (validator.Validator, error)

func ParamsFromMap

func ParamsFromMap(m map[string]string) httprouter.Params

func ToPathParts

func ToPathParts(p string) []string

func Transformers

func Transformers() (ret []string)

Transformers returns all tsfms registered to DefaultFactory

func WithGroupedParamSet

func WithGroupedParamSet(ctx context.Context, m *sync.Map) context.Context

func WrapLocationDecoderError

func WrapLocationDecoderError(dec *json.Decoder, err error) error

Types

type CanInterface

type CanInterface interface {
	Interface() interface{}
}

type CanNextReader

type CanNextReader interface {
	NextReader() io.Reader
}

type CanNextWriter

type CanNextWriter interface {
	NextWriter() io.Writer
}

type CanString

type CanString interface {
	String() string
}

type CommonOption

type CommonOption struct {
	Omitempty bool // Omitempty should be ignored when value is empty
	Explode   bool // Explode for raw uint8/byte slice/array
}

type Factory

type Factory interface {
	NewTransformer(context.Context, typesx.Type, Option) (Transformer, error)
}

func FactoryFromContext

func FactoryFromContext(ctx context.Context) Factory

type FileHeaderReader

type FileHeaderReader struct {
	// contains filtered or unexported fields
}

func (*FileHeaderReader) Interface

func (f *FileHeaderReader) Interface() interface{}

func (*FileHeaderReader) Read

func (f *FileHeaderReader) Read(p []byte) (int, error)

type FlattenParams

type FlattenParams struct {
	Params []ReqParam
}

func (*FlattenParams) CollectParams

func (ps *FlattenParams) CollectParams(ctx context.Context, typ typesx.Type) error

func (FlattenParams) NewValidator

func (FlattenParams) NewValidator(ctx context.Context, typ typesx.Type) (validator.Validator, error)

func (FlattenParams) String

func (FlattenParams) String() string

func (*FlattenParams) Validate

func (ps *FlattenParams) Validate(v interface{}) error

type FormPartWriter

type FormPartWriter struct {
	// contains filtered or unexported fields
}

func NewFormPartWriter

func NewFormPartWriter(creator PartWriterCreator) *FormPartWriter

func (*FormPartWriter) Header

func (w *FormPartWriter) Header() http.Header

func (*FormPartWriter) NextWriter

func (w *FormPartWriter) NextWriter() io.Writer

func (*FormPartWriter) Write

func (w *FormPartWriter) Write(p []byte) (n int, err error)

type GroupedParams

type GroupedParams = map[string][]Param

func CollectGroupedParam

func CollectGroupedParam(ctx context.Context, tpe typesx.Type) GroupedParams

type HTMLText

type HTMLText struct{}

func (HTMLText) DecodeFrom

func (HTMLText) DecodeFrom(_ context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error

func (*HTMLText) EncodeTo

func (t *HTMLText) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

func (HTMLText) NamedByTag

func (HTMLText) NamedByTag() string

func (HTMLText) Names

func (HTMLText) Names() []string

func (HTMLText) New

func (*HTMLText) String

func (t *HTMLText) String() string

type HeaderWriter

type HeaderWriter interface {
	httpx.WithHeader
	io.Writer
}

func WriterWithHeader

func WriterWithHeader(w io.Writer, header http.Header) HeaderWriter

type JSON

type JSON struct{}

func (JSON) DecodeFrom

func (JSON) DecodeFrom(ctx context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error

func (*JSON) EncodeTo

func (t *JSON) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

func (JSON) NamedByTag

func (JSON) NamedByTag() string

func (JSON) Names

func (JSON) Names() []string

func (JSON) New

func (*JSON) String

func (t *JSON) String() string

type MayValidator

type MayValidator interface {
	NewValidator(context.Context, typesx.Type) (validator.Validator, error)
}

type Multipart

type Multipart struct{ *FlattenParams }

Multipart for multipart/form-data

func (*Multipart) DecodeFrom

func (t *Multipart) DecodeFrom(ctx context.Context, r io.Reader, v interface{}, headers ...textproto.MIMEHeader) error

func (*Multipart) EncodeTo

func (t *Multipart) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

func (Multipart) NamedByTag

func (Multipart) NamedByTag() string

func (Multipart) Names

func (Multipart) Names() []string

func (Multipart) New

func (*Multipart) String

func (t *Multipart) String() string

type OctetStream

type OctetStream struct{}

func (OctetStream) DecodeFrom

func (OctetStream) DecodeFrom(_ context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error

func (*OctetStream) EncodeTo

func (t *OctetStream) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

func (OctetStream) Names

func (OctetStream) Names() []string

func (OctetStream) New

func (*OctetStream) String

func (t *OctetStream) String() string

type Option

type Option struct {
	Name string
	MIME string
	CommonOption
}

func (Option) String

func (o Option) String() string

type Param

type Param struct {
	In    string
	Name  string
	Field typesx.StructField
	Type  typesx.Type
	Tags  map[string]reflectx.StructTag
	Loc   []int
}

func (*Param) FieldValue

func (p *Param) FieldValue(rv reflect.Value) reflect.Value

type ParamAndValue

type ParamAndValue struct {
	Param
	Value reflect.Value
}

type PartWriterCreator

type PartWriterCreator func(header textproto.MIMEHeader) (io.Writer, error)

type PathWalker

type PathWalker struct {
	// contains filtered or unexported fields
}

func (*PathWalker) Enter

func (p *PathWalker) Enter(i interface{})

func (*PathWalker) Exit

func (p *PathWalker) Exit()

func (*PathWalker) Paths

func (p *PathWalker) Paths() []interface{}

func (*PathWalker) String

func (p *PathWalker) String() string

type PathnamePattern

type PathnamePattern struct {
	// contains filtered or unexported fields
}

func NewPathnamePattern

func NewPathnamePattern(pattern string) *PathnamePattern

func (*PathnamePattern) Parse

func (pp *PathnamePattern) Parse(pathname string) (params httprouter.Params, err error)

func (*PathnamePattern) String

func (pp *PathnamePattern) String() string

func (*PathnamePattern) Stringify

func (pp *PathnamePattern) Stringify(params httprouter.Params) string

type PlainText

type PlainText struct{}

func (*PlainText) DecodeFrom

func (t *PlainText) DecodeFrom(_ context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error

func (*PlainText) EncodeTo

func (t *PlainText) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

func (PlainText) Names

func (PlainText) Names() []string

func (PlainText) New

func (*PlainText) String

func (t *PlainText) String() string

type Protobuf added in v1.0.0

type Protobuf struct{}

func (*Protobuf) DecodeFrom added in v1.0.0

func (t *Protobuf) DecodeFrom(ctx context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error

func (*Protobuf) EncodeTo added in v1.0.0

func (t *Protobuf) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

func (Protobuf) NamedByTag added in v1.0.0

func (Protobuf) NamedByTag() string

func (Protobuf) Names added in v1.0.0

func (Protobuf) Names() []string

func (Protobuf) New added in v1.0.0

func (*Protobuf) NewValidator added in v1.0.0

func (t *Protobuf) NewValidator(_ context.Context, _ typesx.Type) (validator.Validator, error)

NewValidator returns empty validator to implements interface `MayValidate` to skip protobuf struct validation

func (*Protobuf) String added in v1.0.0

func (t *Protobuf) String() string

type ReqParam

type ReqParam struct {
	Param
	Option    Option
	Tsf       Transformer
	Validator validator.Validator
}

type StringBuilders

type StringBuilders struct {
	// contains filtered or unexported fields
}

func NewStringBuilders

func NewStringBuilders() *StringBuilders

func (*StringBuilders) NextWriter

func (v *StringBuilders) NextWriter() io.Writer

func (*StringBuilders) SetN

func (v *StringBuilders) SetN(n int)

func (*StringBuilders) StringSlice

func (v *StringBuilders) StringSlice() []string

func (*StringBuilders) Write

func (v *StringBuilders) Write(p []byte) (n int, err error)

type StringReader

type StringReader struct {
	// contains filtered or unexported fields
}

func NewStringReader

func NewStringReader(v string) *StringReader

func (*StringReader) Interface

func (r *StringReader) Interface() interface{}

func (*StringReader) Read

func (r *StringReader) Read(p []byte) (n int, err error)

func (*StringReader) String

func (r *StringReader) String() string

type StringReaders

type StringReaders struct {
	// contains filtered or unexported fields
}

func NewFileHeaderReaders

func NewFileHeaderReaders(headers []*multipart.FileHeader) *StringReaders

func NewStringReaders

func NewStringReaders(values []string) *StringReaders

func (*StringReaders) Interface

func (v *StringReaders) Interface() interface{}

func (*StringReaders) Len

func (v *StringReaders) Len() int

func (*StringReaders) NextReader

func (v *StringReaders) NextReader() io.Reader

func (*StringReaders) Read

func (v *StringReaders) Read(p []byte) (n int, err error)

type Super

type Super struct {
	CommonOption
	// contains filtered or unexported fields
}

func NewSuper

func NewSuper(tsfm Transformer, opt *CommonOption) *Super

func (*Super) DecodeFrom

func (t *Super) DecodeFrom(ctx context.Context, r io.Reader, v interface{}) error

func (*Super) EncodeTo

func (t *Super) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

type Transformer

type Transformer interface {
	Names() []string
	New(context.Context, typesx.Type) (Transformer, error)
	EncodeTo(context.Context, io.Writer, any) error
	DecodeFrom(context.Context, io.Reader, any, ...textproto.MIMEHeader) error
}

func NewTransformer

func NewTransformer(ctx context.Context, t typesx.Type, opt Option) (Transformer, error)

type TsfmAndOption

type TsfmAndOption struct {
	Transformer
	Option Option
}

type URLEncoded

type URLEncoded struct{ *FlattenParams }

URLEncoded for application/x-www-form-urlencoded

var s = struct {
	Username string `name:"username"`
	Nickname string `name:"username,omitempty"`
	Tags []string   `name:"tag"`
}{
	Username: "name",
	Tags: []string{"1","2"},
}

will transform to

username=name&tag=1&tag=2

func (*URLEncoded) DecodeFrom

func (t *URLEncoded) DecodeFrom(ctx context.Context, r io.Reader, v interface{}, headers ...textproto.MIMEHeader) error

func (*URLEncoded) EncodeTo

func (t *URLEncoded) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

func (URLEncoded) NamedByTag

func (URLEncoded) NamedByTag() string

func (URLEncoded) Names

func (URLEncoded) Names() []string

func (URLEncoded) New

func (URLEncoded) String

func (t URLEncoded) String() string

type WithNamedByTag

type WithNamedByTag interface {
	NamedByTag() string
}

type XML

type XML struct{}

func (XML) DecodeFrom

func (XML) DecodeFrom(_ context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error

func (*XML) EncodeTo

func (t *XML) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error

func (XML) NamedByTag

func (XML) NamedByTag() string

func (XML) Names

func (XML) Names() []string

func (XML) New

func (*XML) String

func (t *XML) String() string

Jump to

Keyboard shortcuts

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