cfgconv

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 8 Imported by: 0

README

cfgconv

GoDoc

The cfgconv package provides functions to convert from an interface{} to a particular type.

The cfgconv package is similar to the standard strconv package, but converts from interface{} instead of string.

The conversions performed by cfgconv are as permissive as possible, given the data types involved, to allow for mapping from sources that may not directly support the requested type.

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

Constants

This section is empty.

Variables

View Source
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

func Bool(v interface{}) (bool, error)

Bool converts a generic object into a bool, if possible. Returns false and an error if conversion is not possible.

func Convert

func Convert(v interface{}, rt reflect.Type) (interface{}, error)

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

func Duration(v interface{}) (time.Duration, error)

Duration converts a string to a duration, if possible. Returns 0 and an error if conversion is not possible.

func Float

func Float(v interface{}) (float64, error)

Float converts a generic object into a float64, if possible. Returns 0 and an error if conversion is not possible.

func Int

func Int(v interface{}) (int64, error)

Int converts a generic object into an int64, if possible. Returns 0 and an error if conversion is not possible.

func IntSlice

func IntSlice(v interface{}) (retval []int64, rerr error)

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

func String(v interface{}) (string, error)

String converts a generic object into a string, if possible. Returns 0 and an error if conversion is not possible.

func StringSlice

func StringSlice(v interface{}) (retval []string, rerr error)

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

func Time(v interface{}) (time.Time, error)

Time converts a string to a Time, if possible. Returns 0 and an error if conversion is not possible.

func Uint

func Uint(v interface{}) (uint64, error)

Uint converts a generic object into a uint64, if possible. Returns 0 and an error if conversion is not possible.

func UintSlice

func UintSlice(v interface{}) (retval []uint64, rerr error)

UintSlice converts a generic object into a slice of uint64, if possible. Returns nil and an error if not possible.

func UnmarshalStructFromMap

func UnmarshalStructFromMap(m map[string]interface{}, obj interface{}) (rerr error)

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

type OverflowError struct {
	Value interface{}
	Kind  reflect.Kind
}

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

type TypeError

type TypeError struct {
	Value interface{}
	Kind  reflect.Kind
}

TypeError indicates a type conversion was not possible. Identifies the value being converted and the kind it couldn't be converted into.

func (TypeError) Error

func (e TypeError) Error() string

Jump to

Keyboard shortcuts

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