Documentation ¶
Overview ¶
Package cfgconv provides type conversions from incoming configuration types to requested internal types. Essentially a more general version of strconv.
The type conversions are flexible, and include automatic conversion from:
string to numeric string to bool numeric to string numeric to bool bool to numeric bool to string float to int
Performs range checks when converting between types to prevent loss of precision.
Index ¶
- Variables
- func Bool(v interface{}) (bool, error)
- func Convert(v interface{}, rt reflect.Type) (interface{}, error)
- func Duration(v interface{}) (time.Duration, error)
- func Float(v interface{}) (float64, error)
- func Int(v interface{}) (int64, error)
- func IntSlice(v interface{}) (retval []int64, rerr error)
- func Slice(v interface{}) ([]interface{}, error)
- func String(v interface{}) (string, error)
- func StringSlice(v interface{}) (retval []string, rerr error)
- func Struct(v interface{}, obj interface{}) error
- func Time(v interface{}) (time.Time, error)
- func Uint(v interface{}) (uint64, error)
- func UintSlice(v interface{}) (retval []uint64, rerr error)
- func UnmarshalStructFromMap(m map[string]interface{}, obj interface{}) (rerr error)
- type OverflowError
- type TypeError
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidStruct = errors.New("unmarshal: provided obj is not pointer to struct")
ErrInvalidStruct indicates UnMarshal was provided an object to populate which is not a pointer to struct.
Functions ¶
func Bool ¶
Bool converts a generic object into a bool, if possible. Returns false and an error if conversion is not possible.
func Convert ¶
Convert converts the value v to the requested type rt, if possible. If not possible then returns a zeroed instance and an error. Returned errors are typically TypeErrors or OverflowErrors, but can also be errors from underlying type converters.
func Duration ¶
Duration converts a string to a duration, if possible. Returns 0 and an error if conversion is not possible.
func Float ¶
Float converts a generic object into a float64, if possible. Returns 0 and an error if conversion is not possible.
func Int ¶
Int converts a generic object into an int64, if possible. Returns 0 and an error if conversion is not possible.
func IntSlice ¶
IntSlice converts a generic object into a slice of int64s, if possible. Returns nil and an error if not possible.
func Slice ¶
func Slice(v interface{}) ([]interface{}, error)
Slice converts a slice of something into a []interface{}
Also interprets strings as a single element slice, to allow for the case where a Getter cannot distinguish between a single entry slice and a literal, e.g. the env Getter.
func String ¶
String converts a generic object into a string, if possible. Returns 0 and an error if conversion is not possible.
func StringSlice ¶
StringSlice converts a generic object to a slice of string, if possible. Returns nil and an error if not possible.
func Struct ¶
func Struct(v interface{}, obj interface{}) error
Struct converts a generic object to a struct.
The obj is a struct with fields corresponding to config values. The config values will be converted to the type defined in the corresponding struct fields. Overflow checks are performed during conversion to ensure the value returned by the getter can fit within the designated field.
By default the map keys are drawn from the struct field names, converted to LowerCamelCase (as per typical JSON naming conventions). This can be overridden using `config:"<name>"` tags.
Struct fields which do not have corresponding map keys are ignored, as are map keys which have no corresponding struct field, and non-exported struct fields.
The error identifies the first type conversion error, if any.
Currently only support conversion from map[string]interface{}, but may support struct to struct conversions at a later date, hence the wrapper around UnmarshalStructFromMap.
func Time ¶
Time converts a string to a Time, if possible. Returns 0 and an error if conversion is not possible.
func Uint ¶
Uint converts a generic object into a uint64, if possible. Returns 0 and an error if conversion is not possible.
func UintSlice ¶
UintSlice converts a generic object into a slice of uint64, if possible. Returns nil and an error if not possible.
func UnmarshalStructFromMap ¶
UnmarshalStructFromMap populates a struct with the values from a map.
The obj is pointer to a struct with fields corresponding to config values. The config values will be converted to the type defined in the corresponding struct fields. Overflow checks are performed during conversion to ensure the value returned by the getter can fit within the designated field.
By default the map keys are drawn from the struct field names, converted to LowerCamelCase (as per typical JSON naming conventions). This can be overridden using `config:"<name>"` tags.
Struct fields which do not have corresponding map keys are ignored, as are map keys which have no corresponding struct field, and non-exported struct fields.
The error identifies the first type conversion error, if any.
Types ¶
type OverflowError ¶
OverflowError indicates type conversion would lose precision. Identifies the value being converted and the kind it couldn't be converted into without loss of precision.
func (OverflowError) Error ¶
func (e OverflowError) Error() string