Documentation
¶
Overview ¶
Package defaults 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, t reflect.Type) (reflect.Value, error)
- func ParseBool(str string, t reflect.Type) (reflect.Value, error)
- func ParseComplex(str string, t reflect.Type) (reflect.Value, error)
- func ParseDuration(str string, t reflect.Type) (reflect.Value, error)
- func ParseFloat(str string, t reflect.Type) (reflect.Value, error)
- func ParseInt(str string, t reflect.Type) (reflect.Value, error)
- func ParseMap(str string, t reflect.Type) (reflect.Value, error)
- func ParseSlice(str string, t reflect.Type) (reflect.Value, error)
- func ParseString(str string, t reflect.Type) (reflect.Value, error)
- func ParseUint(str string, t reflect.Type) (reflect.Value, error)
- func SetDefaultTag(tag string)
- type ParserFunc
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedType = errors.New("unsupported type")
ErrUnsupportedType is an error returned when attempting to parse an unsupported type.
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, time.Duration, 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.
func ParseArray ¶
ParseArray parses a JSON-like string to an array.
func ParseComplex ¶
ParseComplex parses a string to a complex type (complex64, complex128).
func ParseDuration ¶
ParseDuration parses a string to a time.Duration.
func ParseFloat ¶
ParseFloat parses a string to a float type (float32, float64).
func ParseSlice ¶
ParseSlice parses a JSON-like string to a slice.
func ParseString ¶
ParseString parses a string to a string.