Documentation
¶
Overview ¶
Package pdefault provides functionality to parse and set default values for struct fields based on their "default" tags. The Defaults function processes a non-nil pointer to a struct, setting default values for exported fields that are unset (zero values for non-pointers or nil for pointers) using the tag key specified by the package-level variable Tag (defaulting to "default"). Nested structs are processed recursively. Fields without a "default" tag are skipped unless they are structs or struct pointers.
Supported field types and example default tags:
- int: `default:"123"`
- int8: `default:"127"`
- int16: `default:"32767"`
- int32: `default:"2147483647"`
- int64: `default:"9223372036854775807"`
- uint: `default:"123"`
- uint8: `default:"255"`
- uint16: `default:"65535"`
- uint32: `default:"4294967295"`
- uint64: `default:"18446744073709551615"`
- float32: `default:"3.14"`
- float64: `default:"3.14159265359"`
- complex64: `default:"1+2i"`
- complex128: `default:"1.5+2.5i"`
- string: `default:"hello"`
- bool: `default:"true"`
- map (e.g., map[string]any): `default:"{\"key\":\"value\",\"num\":42}"`
- slice (e.g., []string): `default:"[\"a\",\"b\",\"c\"]"`
- array (e.g., [3]int): `default:"[1,2,3]"`
- struct (triggers recursive default setting for nested struct fields)
- Pointers to the above types (e.g., *int, *string, *map[string]any, *[]string, *[3]int, *struct):
- *int: `default:"123"`
- *string: `default:"hello"`
- *map[string]any: `default:"{\"key\":\"value\"}"`
- *[]string: `default:"[\"a\",\"b\"]"`
- *[3]int: `default:"[1,2,3]"`
- *struct (recursively processes nested struct fields)
Unsupported field types:
- Function types (e.g., func(), *func())
- Channels (e.g., chan int)
- Interfaces
- Unsafe pointers (e.g., unsafe.Pointer)
- Any other types not listed above
Default tag values must be valid for the field's type. Numeric types require valid numeric strings, bool requires "true" or "false", strings can be plain or JSON-escaped, and maps/slices/arrays require JSON-formatted strings. Errors are returned for invalid inputs, unexported fields, empty tags (for non-struct fields), parsing failures (e.g., invalid number formats, JSON syntax errors), out-of-range values, or unsupported types. The Defaults function recursively processes nested structs to apply their default tags.
Index ¶
- Variables
- func Defaults(s any) error
- func ParseArray(str string, targetType reflect.Type) (any, error)
- func ParseBool(str string) (bool, error)
- func ParseComplex64(str string) (complex64, error)
- func ParseComplex128(str string) (complex128, error)
- func ParseDuration(str string) (time.Duration, error)
- func ParseFloat32(str string) (float32, error)
- func ParseFloat64(str string) (float64, error)
- func ParseInt(str string) (int, error)
- func ParseInt8(str string) (int8, error)
- func ParseInt16(str string) (int16, error)
- func ParseInt32(str string) (int32, error)
- func ParseInt64(str string) (int64, error)
- func ParseMap(str string, targetType reflect.Type) (any, error)
- func ParseSlice(str string, targetType reflect.Type) (any, error)
- func ParseString(str string) (string, error)
- func ParseUint(str string) (uint, error)
- func ParseUint8(str string) (uint8, error)
- func ParseUint16(str string) (uint16, error)
- func ParseUint32(str string) (uint32, error)
- func ParseUint64(str string) (uint64, error)
- func SetDefaultTag(tag string)
- func Zero[T any]() T
Constants ¶
This section is empty.
Variables ¶
var Tag = defaultTag
Tag specifies the struct tag key used to parse default values for fields.
Functions ¶
func Defaults ¶
Defaults sets default values for struct fields based on their "default" tags.
It takes a pointer to a struct as input and processes each exported field with a "default" tag. If a field is unset (zero value for non-pointers or nil for pointers), the function parses the tag value and sets it according to the field's type. Supported types include int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128, bool, string, map, slice, array, and nested structs (including pointers to these types). The function recursively processes nested structs. It skips unexported fields, non-zero fields, and fields without a "default" tag unless they are structs or struct pointers.
Errors are returned for invalid inputs, unsupported types, or parsing failures.
The tag key used for parsing is defined by the package-level variable Tag, which defaults to "default".
func ParseArray ¶
ParseArray parses a string to a slice of any values representing an array. It expects a JSON-like string (e.g., [1,2,3]).
func ParseComplex64 ¶
ParseComplex64 parses a string to an complex64.
func ParseComplex128 ¶
func ParseComplex128(str string) (complex128, error)
ParseComplex128 parses a string to an complex128.
func ParseDuration ¶
ParseDuration parse a string to time.Duration
func ParseFloat32 ¶
ParseFloat32 parses a string to an float32.
func ParseFloat64 ¶
ParseFloat64 parses a string to an float64.
func ParseInt16 ¶
ParseInt16 parses a string to an int16.
func ParseInt32 ¶
ParseInt32 parses a string to an int32.
func ParseInt64 ¶
ParseInt64 parses a string to an int64.
func ParseMap ¶
ParseMap parses a string to a map with any keys and values. It expects a JSON-like string (e.g., "{<key>:<value>}"). String value should be \"<value>\".
func ParseSlice ¶
ParseSlice parses a string to a slice of any values. It expects a JSON-like string (e.g., [1,2,3]).
func ParseString ¶
ParseString parse a string to string
func ParseUint8 ¶
ParseUint8 parses a string to an uint8.
func ParseUint16 ¶
ParseUint16 parses a string to an uint16.
func ParseUint32 ¶
ParseUint32 parses a string to an uint32.
func ParseUint64 ¶
ParseUint64 parses a string to an uint64.
Types ¶
This section is empty.