internal

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultConfigTag = "cfg"
View Source
const SkipFieldTagValue = "-"

Variables

View Source
var ErrInputIsNotPointerOrStruct = errors.New("input value is not struct or not pointer")
View Source
var ErrNoIterValue = errors.New("no value to iterate")
View Source
var SliceSeparator = ","
View Source
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

func GetEnvWithFallback(name string, fallback string) string

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 GetReflectElem(val interface{}) (reflect.Value, error)

func IsLocalNetworkError

func IsLocalNetworkError(err error) bool

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

func IsStruct(typ reflect.Type) bool

IsStruct returns true if specified type is struct.

Note: time.Time is not a struct by definition of this function!

func IsTagOmitted

func IsTagOmitted(tagVals []string) bool

IsTagOmitted returns true if tag was not found.

func IsTagSkip

func IsTagSkip(tagVals []string) bool

IsTagSkip returns true if tag value was exactly "-".

func JoinFieldNames

func JoinFieldNames(outer, inner, separator string) string

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

func SetReflectValueString(fieldName, v string, val reflect.Value) error

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 SetStructFieldValue

func SetStructFieldValue(fieldName, v string, strct reflect.Value) error

func ShouldSkipField

func ShouldSkipField(field reflect.Value, fieldName string, noUpdate bool) bool

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

func TagValueByKeys(tag reflect.StructTag, keys ...string) []string

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

func TypeImplementsInterface(val reflect.Value, interfaceType reflect.Type) reflect.Value

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 IteratorFunc

type IteratorFunc func(fieldName string, field reflect.Value) error

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

type TypeSetter func(input string, val reflect.Value) error

func GetCustomSetter

func GetCustomSetter(typ reflect.Type) TypeSetter

Jump to

Keyboard shortcuts

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