Documentation ¶
Index ¶
- Constants
- Variables
- func AddCustomTypeSetter(typ interface{}, setter TypeSetter)
- func GetEnvWithFallback(name string, fallback string) string
- func GetReflectElem(val interface{}) (reflect.Value, error)
- func IsLocalNetworkError(err error) bool
- func IsStruct(typ reflect.Type) bool
- func IsTagOmitted(tagVals []string) bool
- func IsTagSkip(tagVals []string) bool
- func JoinFieldNames(outer, inner, separator string) string
- func PlainFieldNameWithPath(outer string, currentField reflect.StructField) string
- func SetReflectValueString(fieldName, v string, val reflect.Value) error
- func SetStructFieldValue(fieldName, v string, strct reflect.Value) error
- func ShouldSkipField(field reflect.Value, fieldName string, noUpdate bool) bool
- func TagValue(field reflect.StructField, key string) []string
- func TagValueByKeys(tag reflect.StructTag, keys ...string) []string
- func TypeImplementsInterface(val reflect.Value, interfaceType reflect.Type) reflect.Value
- type FieldNameFunc
- type IteratorFunc
- type StructIterator
- type TypeSetter
Constants ¶
const DefaultConfigTag = "cfg"
const SkipFieldTagValue = "-"
Variables ¶
var ErrInputIsNotPointerOrStruct = errors.New("input value is not struct or not pointer")
var ErrNoIterValue = errors.New("no value to iterate")
var SliceSeparator = ","
var TimeType = reflect.TypeOf(time.Time{})
Functions ¶
func AddCustomTypeSetter ¶
func AddCustomTypeSetter(typ interface{}, setter TypeSetter)
AddCustomTypeSetter will add custom setter for specified type. It is rare to add setter for pointer type: only to satisfy 'flags' loader. Warning: pointer types are not guaranteed to work!
This types are also used for flag setters.
To disable custom setter - call this method with setter == nil. This is also the method to remove pointer type setter provided by this function.
For example:
// This will add custom setter for type 'int64' AddCustomTypeSetter(int64(0), someSetter) // To disable any custom setter - provide nil as setter for required type: AddCustomTypeSetter(int64(0), nil) // To add pointer-only type use this: AddCustomTypeSetter((*time.Duration)(nil), someDurationSetter) // This will add custom setter for type '*time.Duration' only.
func GetEnvWithFallback ¶ added in v0.1.2
GetEnvWithFallback tries to load a value from an env variable by its name and gives back a given fallback value in case the requested variable is empty.
func GetReflectElem ¶
func IsLocalNetworkError ¶
IsLocalNetworkError will check if provided error is local connection error.
It will return true if error is connection failed to host "127.0.0.1".
func IsStruct ¶
IsStruct returns true if specified type is struct.
Note: time.Time is not a struct by definition of this function!
func IsTagOmitted ¶
IsTagOmitted returns true if tag was not found.
func JoinFieldNames ¶
JoinFieldNames will return concatenated string with 'separator' based on the value of 'outer'.
If 'inner' == "-" - then "-" will be returned. If 'outer' == "" - then 'inner' will be returned as is. If 'inner' == "" - then outer + separator will be returned.
Separator can be empty.
func PlainFieldNameWithPath ¶
func PlainFieldNameWithPath(outer string, currentField reflect.StructField) string
func SetReflectValueString ¶
SetReflectValueString sets a value in the config struct.
'val' is actual variable that should be set.
Custom types can be added for value setting with AddCustomTypeSetter.
If type that is being set implements encoding.TextUnmarshaler - it will be used instead of direct field set. If custom setter function is present AND type implements encoding.TextUnmarshaler - custom setter function will take priority.
func ShouldSkipField ¶
ShouldSkipField returns true if field should be skipped.
Field will be skipped if it is not valid / cannot be set, or if fieldName is equal to SkipFieldTagValue.
func TagValue ¶
func TagValue(field reflect.StructField, key string) []string
TagValue returns splitted by comma value of tag for specified key.
If no tags are provided - lowercased field name is returned.
Note: If no such key in tag is present - DefaultConfigTag will be used to get value:
TagValue(field, "key") == TagValueByKeys(field, "key", DefaultConfigTag)
See TagValueByKeys for usage examples.
func TagValueByKeys ¶
TagValueByKeys returns comma-separated list of values based on keys and bool value representing if tag was found.
Keys are sorted, which means that if first key has valid value - that value will be returned and no further keys will be checked.
This function is useful to get default tag value if no requested tags have any values:
tagVal := TagValueByKeys(field, "highestPriority", "lowerPriority", "lowestPriority", "defaultTagKey")
It is valid to call this function with zero or one key.
If no tag found - nil is returned.
func TypeImplementsInterface ¶
TypeImplementsInterface checks if provided value's type, or pointer(if possible to make pointer) to it's type implements specified interface type.
Argument interfaceType can be created as reflect.TypeOf((*interface{})(nil)).Elem().
Types ¶
type FieldNameFunc ¶
type FieldNameFunc func(outerName string, currentField reflect.StructField) string
func FieldNameWithSeparator ¶
func FieldNameWithSeparator(tag, separator string, opts ...func(string) string) FieldNameFunc
FieldNameWithSeparator will return FieldNameFunc that will use provided struct tag to get field name.
Outer and inner field names will be concatenated with provided separator.
Optionally result modifying functions can be specified with opts argument.
type StructIterator ¶
type StructIterator struct { Value interface{} ReflectValue reflect.Value BaseName string // NoUpdate will specify that IteratorFunc cannot be run on fields with non-zero values. NoUpdate bool FieldNameFunc FieldNameFunc IteratorFunc IteratorFunc }
StructIterator allows to traverse structures and call predefined function on each field.
All loaders that use internal.StructIterator will not be able to use multiple names of fields!
func (StructIterator) Iterate ¶
func (it StructIterator) Iterate() error
Iterate will iterate over every field and will execute FieldNameFunc and IteratorFunc on them.
It will dive into all inner structs and will also iterate fields there.
Pointer fields that are equal to nil will be initialized.
Fields(but not structs) can implement encoding.TextUnmarshaler to be able to set values with custom logic.
func (*StructIterator) ValidateData ¶
func (it *StructIterator) ValidateData() error
ValidateData makes sure that input data in iterator will be able to use it.
type TypeSetter ¶
func GetCustomSetter ¶
func GetCustomSetter(typ reflect.Type) TypeSetter