Documentation
¶
Index ¶
- Constants
- Variables
- func Call(r Receptor, f StructField) error
- func Errorf(format string, v ...any) error
- func IsSupportedKind(k reflect.Kind) bool
- func JoinErrors(err ...error) error
- func ParseFloat[T constraints.Float](s string) (T, error)
- func ParseInt[T constraints.Signed](s string) (T, error)
- func ParseUint[T constraints.Unsigned](s string) (T, error)
- func Switch(r Receptor, kind reflect.Kind) func(StructField) error
- func TryParse[S, T any](conv func(S) (T, error), callback func(S, T) error) func(S) error
- type AnyReceptor
- type BoolReceptor
- type ConvFunc
- type Converter
- type DefaultConverter
- func (c DefaultConverter) Bool(s string) (bool, error)
- func (c DefaultConverter) Float32(s string) (float32, error)
- func (c DefaultConverter) Float64(s string) (float64, error)
- func (c DefaultConverter) Int(s string) (int, error)
- func (c DefaultConverter) Int16(s string) (int16, error)
- func (c DefaultConverter) Int32(s string) (int32, error)
- func (c DefaultConverter) Int64(s string) (int64, error)
- func (c DefaultConverter) Int8(s string) (int8, error)
- func (c DefaultConverter) String(s string) (string, error)
- func (c DefaultConverter) Uint(s string) (uint, error)
- func (c DefaultConverter) Uint16(s string) (uint16, error)
- func (c DefaultConverter) Uint32(s string) (uint32, error)
- func (c DefaultConverter) Uint64(s string) (uint64, error)
- func (c DefaultConverter) Uint8(s string) (uint8, error)
- type DefaultTypedReceptor
- func FlagSetTypedReceptor(boolFunc FlagSetFunc[bool], intFunc FlagSetFunc[int], ...) *DefaultTypedReceptor
- func PFlagSetTypeReceptor(fs *pflag.FlagSet) *DefaultTypedReceptor
- func SetTypedReceptor(ptr any, anyCallback func(StructField, string, func() reflect.Value) error) (*DefaultTypedReceptor, error)
- func (r DefaultTypedReceptor) Any(s StructField, v string) error
- func (r DefaultTypedReceptor) Bool(s StructField, v bool) error
- func (r DefaultTypedReceptor) Float32(s StructField, v float32) error
- func (r DefaultTypedReceptor) Float64(s StructField, v float64) error
- func (r DefaultTypedReceptor) Int(s StructField, v int) error
- func (r DefaultTypedReceptor) Int16(s StructField, v int16) error
- func (r DefaultTypedReceptor) Int32(s StructField, v int32) error
- func (r DefaultTypedReceptor) Int64(s StructField, v int64) error
- func (r DefaultTypedReceptor) Int8(s StructField, v int8) error
- func (r DefaultTypedReceptor) String(s StructField, v string) error
- func (r DefaultTypedReceptor) Uint(s StructField, v uint) error
- func (r DefaultTypedReceptor) Uint16(s StructField, v uint16) error
- func (r DefaultTypedReceptor) Uint32(s StructField, v uint32) error
- func (r DefaultTypedReceptor) Uint64(s StructField, v uint64) error
- func (r DefaultTypedReceptor) Uint8(s StructField, v uint8) error
- type EnvVar
- type FlagSetFunc
- type FloatReceptor
- type IntReceptor
- type Merger
- type PairsReceptor
- func DefaultReceptor(ptr any, anyCallback func(StructField, string, func() reflect.Value) error) (*PairsReceptor, error)
- func EnvReceptor(ptr any, anyCallback func(StructField, string, func() reflect.Value) error) (*PairsReceptor, error)
- func FlagSetReceptor(typedReceptor TypedReceptor) *PairsReceptor
- func PFlagGetReceptor(ptr any, fs *pflag.FlagSet, ...) (*PairsReceptor, error)
- func PFlagSetReceptor(fs *pflag.FlagSet) *PairsReceptor
- func PairsSynthReceptor(get func(StructField) (string, error), converter Converter, ...) *PairsReceptor
- func SetReceptor(ptr any, get func(StructField) (string, error), converter Converter, ...) (*PairsReceptor, error)
- func (r PairsReceptor) Any(f StructField) error
- func (r PairsReceptor) Bool(f StructField) error
- func (r PairsReceptor) Float32(f StructField) error
- func (r PairsReceptor) Float64(f StructField) error
- func (r PairsReceptor) Int(f StructField) error
- func (r PairsReceptor) Int16(f StructField) error
- func (r PairsReceptor) Int32(f StructField) error
- func (r PairsReceptor) Int64(f StructField) error
- func (r PairsReceptor) Int8(f StructField) error
- func (r PairsReceptor) String(f StructField) error
- func (r PairsReceptor) Uint(f StructField) error
- func (r PairsReceptor) Uint16(f StructField) error
- func (r PairsReceptor) Uint32(f StructField) error
- func (r PairsReceptor) Uint64(f StructField) error
- func (r PairsReceptor) Uint8(f StructField) error
- type ParsePair
- type Receptor
- type StringReceptor
- type StructField
- type Supported
- type Tag
- type Type
- type TypedReceptor
- type TypedReceptorFunc
- type UintReceptor
- type Unsigned
Constants ¶
const ( TagName = "name" TagUsage = "usage" TagDefault = "default" TagShort = "short" TagNameIgnored = "-" )
Variables ¶
var ( ErrStructConfig = errors.New("StructConfig") ErrNotStruct = errors.New("NotStruct") ErrNotStructPointer = errors.New("NotStructPointer") )
var ( // Result of conv as default value in ParsePair. ErrParseAsDefault = errors.New("ParseAsDefault") )
var ( // Skip conv and callback but no error in TryParse. ErrSkipParse = errors.New("SkipParse") )
Functions ¶
func Call ¶
func Call(r Receptor, f StructField) error
Call calls the appropriate method of r for the kind of f.
func IsSupportedKind ¶
func JoinErrors ¶
func ParseFloat ¶
func ParseFloat[T constraints.Float](s string) (T, error)
Types ¶
type AnyReceptor ¶
type AnyReceptor interface {
Any(StructField) error
}
type BoolReceptor ¶
type BoolReceptor interface {
Bool(StructField) error
}
type Converter ¶
type Converter interface {
Bool(s string) (bool, error)
Int(s string) (int, error)
Int8(s string) (int8, error)
Int16(s string) (int16, error)
Int32(s string) (int32, error)
Int64(s string) (int64, error)
Uint(s string) (uint, error)
Uint8(s string) (uint8, error)
Uint16(s string) (uint16, error)
Uint32(s string) (uint32, error)
Uint64(s string) (uint64, error)
Float32(s string) (float32, error)
Float64(s string) (float64, error)
String(s string) (string, error)
}
Converter is a set of string conversions.
type DefaultConverter ¶
type DefaultConverter struct {
BoolFunc ConvFunc[bool]
IntFunc ConvFunc[int]
Int8Func ConvFunc[int8]
Int16Func ConvFunc[int16]
Int32Func ConvFunc[int32]
Int64Func ConvFunc[int64]
UintFunc ConvFunc[uint]
Uint8Func ConvFunc[uint8]
Uint16Func ConvFunc[uint16]
Uint32Func ConvFunc[uint32]
Uint64Func ConvFunc[uint64]
Float32Func ConvFunc[float32]
Float64Func ConvFunc[float64]
StringFunc ConvFunc[string]
}
DefaultConverter implements Converter.
func NewConv ¶
func NewConv() *DefaultConverter
func PFlagGetConverter ¶
func PFlagGetConverter(fs *pflag.FlagSet) *DefaultConverter
type DefaultTypedReceptor ¶
type DefaultTypedReceptor struct {
BoolFunc TypedReceptorFunc[bool]
IntFunc TypedReceptorFunc[int]
Int8Func TypedReceptorFunc[int8]
Int16Func TypedReceptorFunc[int16]
Int32Func TypedReceptorFunc[int32]
Int64Func TypedReceptorFunc[int64]
UintFunc TypedReceptorFunc[uint]
Uint8Func TypedReceptorFunc[uint8]
Uint16Func TypedReceptorFunc[uint16]
Uint32Func TypedReceptorFunc[uint32]
Uint64Func TypedReceptorFunc[uint64]
Float32Func TypedReceptorFunc[float32]
Float64Func TypedReceptorFunc[float64]
StringFunc TypedReceptorFunc[string]
AnyFunc TypedReceptorFunc[string]
}
DefaultTypedReceptor implements TypedReceptor.
func FlagSetTypedReceptor ¶
func FlagSetTypedReceptor( boolFunc FlagSetFunc[bool], intFunc FlagSetFunc[int], int8Func FlagSetFunc[int8], int16Func FlagSetFunc[int16], int32Func FlagSetFunc[int32], int64Func FlagSetFunc[int64], uintFunc FlagSetFunc[uint], uint8Func FlagSetFunc[uint8], uint16Func FlagSetFunc[uint16], uint32Func FlagSetFunc[uint32], uint64Func FlagSetFunc[uint64], float32Func FlagSetFunc[float32], float64Func FlagSetFunc[float64], stringFunc FlagSetFunc[string], anyFunc FlagSetFunc[string], ) *DefaultTypedReceptor
func PFlagSetTypeReceptor ¶
func PFlagSetTypeReceptor(fs *pflag.FlagSet) *DefaultTypedReceptor
func SetTypedReceptor ¶
func SetTypedReceptor( ptr any, anyCallback func(StructField, string, func() reflect.Value) error, ) (*DefaultTypedReceptor, error)
func (DefaultTypedReceptor) Any ¶
func (r DefaultTypedReceptor) Any(s StructField, v string) error
func (DefaultTypedReceptor) Bool ¶
func (r DefaultTypedReceptor) Bool(s StructField, v bool) error
func (DefaultTypedReceptor) Float32 ¶
func (r DefaultTypedReceptor) Float32(s StructField, v float32) error
func (DefaultTypedReceptor) Float64 ¶
func (r DefaultTypedReceptor) Float64(s StructField, v float64) error
func (DefaultTypedReceptor) Int ¶
func (r DefaultTypedReceptor) Int(s StructField, v int) error
func (DefaultTypedReceptor) Int16 ¶
func (r DefaultTypedReceptor) Int16(s StructField, v int16) error
func (DefaultTypedReceptor) Int32 ¶
func (r DefaultTypedReceptor) Int32(s StructField, v int32) error
func (DefaultTypedReceptor) Int64 ¶
func (r DefaultTypedReceptor) Int64(s StructField, v int64) error
func (DefaultTypedReceptor) Int8 ¶
func (r DefaultTypedReceptor) Int8(s StructField, v int8) error
func (DefaultTypedReceptor) String ¶
func (r DefaultTypedReceptor) String(s StructField, v string) error
func (DefaultTypedReceptor) Uint ¶
func (r DefaultTypedReceptor) Uint(s StructField, v uint) error
func (DefaultTypedReceptor) Uint16 ¶
func (r DefaultTypedReceptor) Uint16(s StructField, v uint16) error
func (DefaultTypedReceptor) Uint32 ¶
func (r DefaultTypedReceptor) Uint32(s StructField, v uint32) error
func (DefaultTypedReceptor) Uint64 ¶
func (r DefaultTypedReceptor) Uint64(s StructField, v uint64) error
func (DefaultTypedReceptor) Uint8 ¶
func (r DefaultTypedReceptor) Uint8(s StructField, v uint8) error
type EnvVar ¶
type EnvVar string
EnvVar is a environment variable.
type FlagSetFunc ¶
FlagSetFunc defines a command-line flag.
func (FlagSetFunc[T]) SetFlag ¶
func (f FlagSetFunc[T]) SetFlag(s StructField, v T) error
SetFlag defines a flag.
- name: name tag value
- default value: v
- usage: usage tag value
type FloatReceptor ¶
type FloatReceptor interface {
Float32(StructField) error
Float64(StructField) error
}
type IntReceptor ¶
type IntReceptor interface {
Int(StructField) error
Int8(StructField) error
Int16(StructField) error
Int32(StructField) error
Int64(StructField) error
}
type Merger ¶
type Merger[T any] struct { // contains filtered or unexported fields }
func NewMerger ¶
func NewMerger[T any]( anyCallback func(StructField, string, func() reflect.Value) error, anyEqual func(left, right any) (bool, error), prefix string, ) *Merger[T]
NewMerger returns a new Merger.
anyCallback parses "default" tag value and set it. anyEqual reports true if left equals right when kind of arguments are not supported. prefix adds a prefix to "default" tag name.
type PairsReceptor ¶
type PairsReceptor struct {
BoolPair *ParsePair[bool]
IntPair *ParsePair[int]
Int8Pair *ParsePair[int8]
Int16Pair *ParsePair[int16]
Int32Pair *ParsePair[int32]
Int64Pair *ParsePair[int64]
UintPair *ParsePair[uint]
Uint8Pair *ParsePair[uint8]
Uint16Pair *ParsePair[uint16]
Uint32Pair *ParsePair[uint32]
Uint64Pair *ParsePair[uint64]
Float32Pair *ParsePair[float32]
Float64Pair *ParsePair[float64]
StringPair *ParsePair[string]
AnyPair *ParsePair[string]
}
PairsReceptor is a set of ParsePair, implements Receptor.
func DefaultReceptor ¶
func DefaultReceptor( ptr any, anyCallback func(StructField, string, func() reflect.Value) error, ) (*PairsReceptor, error)
DefaultReceptor sets default tag value to the struct field.
ptr should be a pointer of struct.
func EnvReceptor ¶
func EnvReceptor( ptr any, anyCallback func(StructField, string, func() reflect.Value) error, ) (*PairsReceptor, error)
EnvReceptor sets environment variable value to the struct field.
ptr should be a pointer of struct.
func FlagSetReceptor ¶
func FlagSetReceptor(typedReceptor TypedReceptor) *PairsReceptor
func PFlagGetReceptor ¶
func PFlagGetReceptor( ptr any, fs *pflag.FlagSet, anyCallback func(StructField, string, func() reflect.Value) error, ) (*PairsReceptor, error)
PFlagGetReceptor returns a Receptor that can retrieve values from the parsed command-line flags.
func PFlagSetReceptor ¶
func PFlagSetReceptor(fs *pflag.FlagSet) *PairsReceptor
PFlagSetReceptor returns a Receptor that can define the command-line flags.
func PairsSynthReceptor ¶
func PairsSynthReceptor( get func(StructField) (string, error), converter Converter, typedReceptor TypedReceptor, ) *PairsReceptor
PairsSynthReceptor synthesizes Converter and TypedReceptor. get extracts the value from StructField, converter converts it and typedReceptor accepts it.
func SetReceptor ¶
func SetReceptor( ptr any, get func(StructField) (string, error), converter Converter, anyCallback func(StructField, string, func() reflect.Value) error, ) (*PairsReceptor, error)
SetReceptor returns a new PairsReceptor to set value to ptr.
ptr should be a pointer of struct. get should return a value to be set; false means not found. converter should convert result of get().
You can customize how unsupported field values are set by anyCallback, if nil, disable this feature. Example of anyCallback:
func(field StructField, str string, fieldPtr func() reflect.Value) error {
var xs []int
if err := json.Unmarshal([]byte(str), &xs); err != nil {
return err
}
fieldPtr().Set(reflect.ValueOf(xs))
return nil
}
anyCallback should parse str and set the value to fieldPtr.
func (PairsReceptor) Any ¶
func (r PairsReceptor) Any(f StructField) error
func (PairsReceptor) Bool ¶
func (r PairsReceptor) Bool(f StructField) error
func (PairsReceptor) Float32 ¶
func (r PairsReceptor) Float32(f StructField) error
func (PairsReceptor) Float64 ¶
func (r PairsReceptor) Float64(f StructField) error
func (PairsReceptor) Int ¶
func (r PairsReceptor) Int(f StructField) error
func (PairsReceptor) Int16 ¶
func (r PairsReceptor) Int16(f StructField) error
func (PairsReceptor) Int32 ¶
func (r PairsReceptor) Int32(f StructField) error
func (PairsReceptor) Int64 ¶
func (r PairsReceptor) Int64(f StructField) error
func (PairsReceptor) Int8 ¶
func (r PairsReceptor) Int8(f StructField) error
func (PairsReceptor) String ¶
func (r PairsReceptor) String(f StructField) error
func (PairsReceptor) Uint ¶
func (r PairsReceptor) Uint(f StructField) error
func (PairsReceptor) Uint16 ¶
func (r PairsReceptor) Uint16(f StructField) error
func (PairsReceptor) Uint32 ¶
func (r PairsReceptor) Uint32(f StructField) error
func (PairsReceptor) Uint64 ¶
func (r PairsReceptor) Uint64(f StructField) error
func (PairsReceptor) Uint8 ¶
func (r PairsReceptor) Uint8(f StructField) error
type ParsePair ¶
type ParsePair[T any] struct { // contains filtered or unexported fields }
ParsePair defines a conversion of StructField that can fail.
func NewPairSynth ¶
func NewPairSynth[T any]( get func(StructField) (string, error), conv func(string) (T, error), callback func(StructField, T) error, ) *ParsePair[T]
NewPairSynth constructs a pair of functions to process StructField.
- get: extract string from StructField
- conv: convert the value
- callback: accept the converted value
get and conv can return ErrParseAsDefault or ErrSkipParse.
func NewParsePair ¶
func NewParsePair[T any]( conv func(StructField) (T, error), callback func(StructField, T) error, ) *ParsePair[T]
func (*ParsePair[T]) Try ¶
func (p *ParsePair[T]) Try(s StructField) error
type Receptor ¶
type Receptor interface {
BoolReceptor
IntReceptor
UintReceptor
FloatReceptor
StringReceptor
AnyReceptor
}
Receptor accepts StructField.
type StringReceptor ¶
type StringReceptor interface {
String(StructField) error
}
type StructField ¶
func NewStructField ¶
func NewStructField( name string, kind reflect.Kind, tag *Tag, ) StructField
type Tag ¶
type Tag struct {
// contains filtered or unexported fields
}
Tag is a tag of struct.
type Type ¶
type Type struct {
// contains filtered or unexported fields
}
Type is the metadata of struct.
func (Type) Fields ¶
func (t Type) Fields() []StructField
Fields returns the metadata of all fields of the struct.
type TypedReceptor ¶
type TypedReceptor interface {
Bool(StructField, bool) error
Int(StructField, int) error
Int8(StructField, int8) error
Int16(StructField, int16) error
Int32(StructField, int32) error
Int64(StructField, int64) error
Uint(StructField, uint) error
Uint8(StructField, uint8) error
Uint16(StructField, uint16) error
Uint32(StructField, uint32) error
Uint64(StructField, uint64) error
Float32(StructField, float32) error
Float64(StructField, float64) error
String(StructField, string) error
Any(StructField, string) error
}
TypedReceptor accepts a pair of StructField and some value.
type TypedReceptorFunc ¶
type TypedReceptorFunc[T any] func(StructField, T) error
TypedReceptorFunc accepts a pair of StructField and some value.
func (TypedReceptorFunc[T]) Call ¶
func (f TypedReceptorFunc[T]) Call(s StructField, v T) error
Call calls self if not nil.
type UintReceptor ¶
type UintReceptor interface {
Uint(StructField) error
Uint8(StructField) error
Uint16(StructField) error
Uint32(StructField) error
Uint64(StructField) error
}