Documentation
¶
Index ¶
- Constants
- func FieldNameById(obj reflect.Type, idx int) string
- func GetFileEnvOrPath(envKey string, flagKey string) string
- func ToReflectType(input any) reflect.Type
- type KindParser
- type NoFieldError
- type NoValueError
- type Parser
- type PatchPanel
- func (pc *PatchPanel) AddKindParser(kind reflect.Kind, parser KindParser)
- func (pc *PatchPanel) AddParser(typ reflect.Type, parser Parser)
- func (pc *PatchPanel) GetDefault(fieldName string, t reflect.Type, parserHints []string) (any, error)
- func (pc *PatchPanel) GetFieldTag(fieldName string, tagName string, t reflect.Type, parserHints []string) (reflect.StructField, any, error)
- type UnhandledParserTypeError
Constants ¶
const ENV_CONFIG_FILE = "CONFIG_FILE"
const FLAG_CONFIG_FILE = "config_file"
const KeyValueSeparator = `:`
KeyValueSeparator is used to split key/value combinations on a given tag e.g. for `entries:"a:b·c:d"`, KeyValueSeparator is used to create:
{
"a": "b",
"c": "d"
}
with TokenSeparator set to · The default is a colon due to common usage.
Reminder that multiple hints can be provided for parsing, such as `ports:"http:80·https:443" allowed_ciphers:"1.2,1.3"`
Use of key/value is optional and provided for custom parsers.
const TokenSeparator = `·`
TokenSeparator separates entries inside a given tag, typically used in conjunction with KeyValueSeparator The default is a middot due to low likelihood of collision.
Variables ¶
This section is empty.
Functions ¶
func FieldNameById ¶
FieldNameById is a convenience func to get named field off of a struct. This is useful for being able to loop over a struct object. Field() lookups on structs use zero-based counting
func GetFileEnvOrPath ¶
func ToReflectType ¶
ToReflectType is a shallow wrapper around reflect.TypeOf, placed in this library for reasons of code-flow This library operates on types that are understood by the `reflect` library
Types ¶
type KindParser ¶ added in v1.0.2
KindParser is like Parser but also receives the target reflect.Type, enabling it to handle all types matching a given reflect.Kind (e.g. all struct types). This is necessary because a single KindParser must be able to construct a zero value of any type of that kind.
type NoFieldError ¶
type NoFieldError struct {
Msg string
}
NoFieldError allows for differentiating no named field vs parsing errors
func (NoFieldError) Error ¶
func (nfe NoFieldError) Error() string
type NoValueError ¶
type NoValueError struct {
Msg string
}
func (NoValueError) Error ¶
func (nve NoValueError) Error() string
type Parser ¶
Parser handles parsing a string input based on an arbitrary function body. parserHints are provided at call time for interpretation within a given function (see time.Time's parser in NewPatchPanel for an example implementation)
type PatchPanel ¶
func NewPatchPanel ¶
func NewPatchPanel(tokenSeparator string, keyValueSeparator string) *PatchPanel
NewPatchPanel instantiates a PatchPanel
func (*PatchPanel) AddKindParser ¶ added in v1.0.2
func (pc *PatchPanel) AddKindParser(kind reflect.Kind, parser KindParser)
AddKindParser adds a KindParser that is used as a fallback for all types matching the given reflect.Kind when no exact reflect.Type parser is registered. The ability to overwrite is intentional.
func (*PatchPanel) AddParser ¶
func (pc *PatchPanel) AddParser(typ reflect.Type, parser Parser)
AddParser adds a parser configuration. The ability to overwrite is intentional.
func (*PatchPanel) GetDefault ¶
func (pc *PatchPanel) GetDefault(fieldName string, t reflect.Type, parserHints []string) (any, error)
GetDefault retrieves the field tag called 'default' and extracts the value
func (*PatchPanel) GetFieldTag ¶
func (pc *PatchPanel) GetFieldTag(fieldName string, tagName string, t reflect.Type, parserHints []string) (reflect.StructField, any, error)
GetFieldTag loads a tag off of a given field in a struct. In an example struct of { A int `x:"y"` }, the fieldName is A, the tagName is x.
The struct field, the tag value, and any error is returned.
type UnhandledParserTypeError ¶
type UnhandledParserTypeError struct {
Msg string
}
UnhandledParserTypeError allows for a user to handle his/her type or just use as is (e.g. if plumbing an any)
func (UnhandledParserTypeError) Error ¶
func (u UnhandledParserTypeError) Error() string