form

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeCommaString22Slice added in v1.1.0

func DecodeCommaString22Slice(t reflect.Type, values []string) (any, error)

DecodeCommaString22Slice decode a comma-separated string to a slice. NOTE: slice element only support `constraints.Integer | constraints.Float | ~string | ~bool`

func DecodeCommaString2Slice added in v1.1.0

func DecodeCommaString2Slice[T constraints.Integer | constraints.Float | ~string | ~bool]() func(values []string) (any, error)

DecodeCommaString2Slice decode a comma-separated string to a slice.

func DecodeValues

func DecodeValues(msg proto.Message, values url.Values) error

DecodeValues decode url value into proto message.

func EncodeField

func EncodeField(fieldDescriptor protoreflect.FieldDescriptor, value protoreflect.Value, useEnumNumbers bool) (string, error)

EncodeField encode proto message filed

func EncodeFieldMask

func EncodeFieldMask(m protoreflect.Message, useProtoNames bool) (query string)

EncodeFieldMask return field mask name=paths

func EncodeSlice2CommaString added in v1.1.0

func EncodeSlice2CommaString[T constraints.Integer | constraints.Float | ~string | ~bool]() func(x any) ([]string, error)

EncodeSlice2CommaString encode a slice to a comma-separated string.

func EncodeSliceToCommaString added in v1.1.0

func EncodeSliceToCommaString(t reflect.Type, x any) ([]string, error)

EncodeSliceToCommaString encode a slice to a comma-separated string. NOTE: slice element only support `constraints.Integer | constraints.Float | ~string | ~bool`

func EncodeValues

func EncodeValues(msg proto.Message, useProtoNames, useEnumNumbers bool) (url.Values, error)

EncodeValues encode a message into url values.

func RegisterBuiltinTypeDecoderCommaStringToSlice added in v1.1.0

func RegisterBuiltinTypeDecoderCommaStringToSlice(dec *form.Decoder)

RegisterBuiltinTypeDecoderCommaStringToSlice register to form.Decoder. decode a comma-separated string to slice. NOTE: slice element type only support

`bool` `int`, `int8`, `int16`, `int32`, `int64` `uint`, `uint8`, `uint16`, `uint32`, `uint64` `float32`, `float64` `string`, `uintptr`

func RegisterBuiltinTypeEncoderSliceToCommaString added in v1.1.0

func RegisterBuiltinTypeEncoderSliceToCommaString(enc *form.Encoder)

RegisterBuiltinTypeEncoderSliceToCommaString register to form.Encoder. encode a slice to a comma-separated string. NOTE: slice element type only support

`bool` `int`, `int8`, `int16`, `int32`, `int64` `uint`, `uint8`, `uint16`, `uint32`, `uint64` `float32`, `float64` `string`, `uintptr`

Types

type Codec

type Codec struct {
	Encoder *form.Encoder
	Decoder *form.Decoder
	TagName string
	// UseProtoNames uses proto field name instead of
	// lowerCamelCase name in JSON field names.
	UseProtoNames bool
	// UseEnumNumbers emits enum values as numbers.
	UseEnumNumbers bool
}

func New

func New(tagName string) *Codec

New returns a new Codec,

UseProtoNames: true
UseEnumNumbers: true

func (*Codec) ContentType

func (*Codec) ContentType(_ any) string

ContentType always Returns "application/x-www-form-urlencoded; charset=utf-8"

func (*Codec) Decode

func (c *Codec) Decode(vs url.Values, v any) error

func (*Codec) DisableUseEnumNumbers added in v0.0.5

func (c *Codec) DisableUseEnumNumbers() *Codec

DisableUseProtoNames disable emits enum values as numbers.

func (*Codec) DisableUseProtoNames added in v0.0.4

func (c *Codec) DisableUseProtoNames() *Codec

DisableUseProtoNames disable proto field name, use lowerCamelCase name in JSON field names.

func (*Codec) Encode

func (c *Codec) Encode(v any) (url.Values, error)

func (*Codec) EncodeFieldMask added in v0.0.8

func (c *Codec) EncodeFieldMask(m protoreflect.Message) string

EncodeFieldMask return field mask name=paths

func (*Codec) EncodeURL

func (c *Codec) EncodeURL(pathTemplate string, v 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 (*Codec) Marshal

func (c *Codec) Marshal(v any) ([]byte, error)

func (*Codec) NewDecoder

func (c *Codec) NewDecoder(r io.Reader) codec.Decoder

func (*Codec) NewEncoder

func (c *Codec) NewEncoder(w io.Writer) codec.Encoder

func (*Codec) RegisterBuiltinTypeDecoderCommaStringToSlice added in v1.1.0

func (c *Codec) RegisterBuiltinTypeDecoderCommaStringToSlice() *Codec

RegisterBuiltinTypeDecoderCommaStringToSlice register to form.Decoder. decode a comma-separated string to slice. NOTE: only support form.Encoder NOTE: slice element type only support

`bool` `int`, `int8`, `int16`, `int32`, `int64` `uint`, `uint8`, `uint16`, `uint32`, `uint64` `float32`, `float64` `string`, `uintptr`

func (*Codec) RegisterBuiltinTypeEncoderSliceToCommaString added in v1.1.0

func (c *Codec) RegisterBuiltinTypeEncoderSliceToCommaString() *Codec

RegisterBuiltinTypeEncoderSliceToCommaString register to form.Encoder. encode a slice to a comma-separated string. NOTE: only support form.Encoder NOTE: slice element type only support

`bool` `int`, `int8`, `int16`, `int32`, `int64` `uint`, `uint8`, `uint16`, `uint32`, `uint64` `float32`, `float64` `string`, `uintptr`

func (*Codec) RegisterDecoderCustomTypeFunc added in v1.1.0

func (c *Codec) RegisterDecoderCustomTypeFunc(fn func([]string) (any, error), types ...any) *Codec

RegisterEncoderCustomTypeFunc register to form.Encoder. NOTE: only support form.Decoder NOTE: if not register, the type will cause an error.

func (*Codec) RegisterEncoderCustomTypeFunc added in v1.1.0

func (c *Codec) RegisterEncoderCustomTypeFunc(fn func(x any) ([]string, error), types ...any) *Codec

RegisterEncoderCustomTypeFunc register to form.Encoder. NOTE: only support form.Encoder NOTE: if not register, the type will use default behavior.

func (*Codec) Unmarshal

func (c *Codec) Unmarshal(data []byte, v any) error

type MultipartCodec

type MultipartCodec struct {
	*Codec
}

func (*MultipartCodec) ContentType

func (*MultipartCodec) ContentType(_ any) string

type QueryCodec

type QueryCodec struct {
	*Codec
}

func (*QueryCodec) ContentType

func (*QueryCodec) ContentType(_ any) string

type UriCodec

type UriCodec struct {
	*Codec
}

func (*UriCodec) ContentType

func (*UriCodec) ContentType(_ any) string

Jump to

Keyboard shortcuts

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