internal

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TagName    = "name"
	TagUsage   = "usage"
	TagDefault = "default"
	TagShort   = "short"

	TagNameIgnored = "-"
)

Variables

View Source
var (
	ErrStructConfig     = errors.New("StructConfig")
	ErrNotStruct        = errors.New("NotStruct")
	ErrNotStructPointer = errors.New("NotStructPointer")
)
View Source
var (
	// Result of conv as default value in ParsePair.
	ErrParseAsDefault = errors.New("ParseAsDefault")
)
View Source
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 Errorf

func Errorf(format string, v ...any) error

func IsSupportedKind

func IsSupportedKind(k reflect.Kind) bool

func JoinErrors

func JoinErrors(err ...error) error

func ParseFloat

func ParseFloat[T constraints.Float](s string) (T, error)

func ParseInt

func ParseInt[T constraints.Signed](s string) (T, error)

func ParseUint

func ParseUint[T constraints.Unsigned](s string) (T, error)

func Switch

func Switch(r Receptor, kind reflect.Kind) func(StructField) error

Switch chooses a method of r that corresponds to kind.

func TryParse

func TryParse[S, T any](
	conv func(S) (T, error),
	callback func(S, T) error,
) func(S) error

Types

type AnyReceptor

type AnyReceptor interface {
	Any(StructField) error
}

type BoolReceptor

type BoolReceptor interface {
	Bool(StructField) error
}

type ConvFunc

type ConvFunc[T any] func(string) (T, error)

ConvFunc is a function that converts string into T.

func (ConvFunc[T]) Call

func (f ConvFunc[T]) Call(s string) (T, error)

Call calls self if not nil, otherwise returns the default value.

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

func (DefaultConverter) Bool

func (c DefaultConverter) Bool(s string) (bool, error)

func (DefaultConverter) Float32

func (c DefaultConverter) Float32(s string) (float32, error)

func (DefaultConverter) Float64

func (c DefaultConverter) Float64(s string) (float64, error)

func (DefaultConverter) Int

func (c DefaultConverter) Int(s string) (int, error)

func (DefaultConverter) Int16

func (c DefaultConverter) Int16(s string) (int16, error)

func (DefaultConverter) Int32

func (c DefaultConverter) Int32(s string) (int32, error)

func (DefaultConverter) Int64

func (c DefaultConverter) Int64(s string) (int64, error)

func (DefaultConverter) Int8

func (c DefaultConverter) Int8(s string) (int8, error)

func (DefaultConverter) String

func (c DefaultConverter) String(s string) (string, error)

func (DefaultConverter) Uint

func (c DefaultConverter) Uint(s string) (uint, error)

func (DefaultConverter) Uint16

func (c DefaultConverter) Uint16(s string) (uint16, error)

func (DefaultConverter) Uint32

func (c DefaultConverter) Uint32(s string) (uint32, error)

func (DefaultConverter) Uint64

func (c DefaultConverter) Uint64(s string) (uint64, error)

func (DefaultConverter) Uint8

func (c DefaultConverter) Uint8(s string) (uint8, error)

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 (DefaultTypedReceptor) Bool

func (DefaultTypedReceptor) Float32

func (DefaultTypedReceptor) Float64

func (DefaultTypedReceptor) Int

func (DefaultTypedReceptor) Int16

func (DefaultTypedReceptor) Int32

func (DefaultTypedReceptor) Int64

func (DefaultTypedReceptor) Int8

func (DefaultTypedReceptor) String

func (DefaultTypedReceptor) Uint

func (DefaultTypedReceptor) Uint16

func (DefaultTypedReceptor) Uint32

func (DefaultTypedReceptor) Uint64

func (DefaultTypedReceptor) Uint8

type EnvVar

type EnvVar string

EnvVar is a environment variable.

func NewEnvVar

func NewEnvVar(name string) EnvVar

func (EnvVar) Get

func (v EnvVar) Get() (string, bool)

Get retrieves the value of the environment variable.

func (EnvVar) String

func (v EnvVar) String() string

String returns the name of the environment variable.

type FlagSetFunc

type FlagSetFunc[T any] func(name string, defaultValue T, usage string) error

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.

func (Merger[T]) Merge

func (m Merger[T]) Merge(left, right T) (T, error)

Merge values based on the 'default' tag values. For each field, if the right value is not the default, use it; if not, use the left value. If that is also the default, set the default value. Return this instance.

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 StringReceptor

type StringReceptor interface {
	String(StructField) error
}

type StructField

type StructField interface {
	Name() string
	Kind() reflect.Kind
	Tag() *Tag
}

func NewStructField

func NewStructField(
	name string,
	kind reflect.Kind,
	tag *Tag,
) StructField

type Supported

type Supported interface {
	~bool | constraints.Signed | Unsigned | ~string
}

type Tag

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

Tag is a tag of struct.

func NewTag

func NewTag(t reflect.StructTag, prefix string) *Tag

NewTag returns a new Tag. Tag retrieves values of all TagName, TagUsage, TagDefault. prefix adds a prefix to the tag names.

func (Tag) Default

func (t Tag) Default() (string, bool)

func (Tag) Name

func (t Tag) Name() (string, bool)

func (Tag) Short added in v0.2.0

func (t Tag) Short() (string, bool)

func (Tag) String

func (t Tag) String() string

func (Tag) Usage

func (t Tag) Usage() string

type Type

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

Type is the metadata of struct.

func NewType

func NewType(v any, prefix string) (*Type, error)

NewType constructs Type from struct value. prefix is for Tag.

func (Type) Accept

func (t Type) Accept(r Receptor) error

Accept Call r on each of Type.Fields.

func (Type) Fields

func (t Type) Fields() []StructField

Fields returns the metadata of all fields of the struct.

func (Type) Name

func (t Type) Name() string

Name returns the name of the struct.

type TypedReceptor

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
}

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Jump to

Keyboard shortcuts

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