Documentation ¶
Overview ¶
Package yamagiconf provides an opinionated configuration file parser using a subset of YAML and Go struct type restrictions to allow for consistent configuration files, easy validation and good error reporting. Supports primitive `env` struct tags used to overwrite fields from env vars. Also supports github.com/go-playground/validator struct tags.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrConfigNil = errors.New("cannot load into nil config") ErrValidation = errors.New("validation") ErrValidateTagViolation = errors.New("violates validation rule") ErrYAMLEmptyFile = errors.New("empty file") ErrYAMLMalformed = errors.New("malformed YAML") ErrYAMLInlineNonAnon = errors.New("inline yaml on non-embedded field") ErrYAMLInlineOpt = errors.New("use `yaml:\",inline\"` for embedded fields") ErrYAMLTagOnUnexported = errors.New("yaml tag on unexported field") ErrYAMLTagRedefined = errors.New("a yaml tag must be unique") ErrYAMLAnchorRedefined = errors.New("yaml anchors must be unique throughout " + "the whole document") ErrYAMLAnchorUnused = errors.New("yaml anchors must be referenced at least once") ErrYAMLAnchorNoValue = errors.New("don't use anchors with implicit null value") ErrYAMLMissingConfig = errors.New("missing field in config file") ErrYAMLBadBoolLiteral = errors.New("must be either false or true, " + "other variants of boolean literals of YAML are not supported") ErrYAMLTagUsed = errors.New("avoid using YAML tags") ErrYAMLNullOnNonPointer = errors.New("cannot assign null to non-pointer type") ErrYAMLBadNullLiteral = errors.New("must be null, " + "any other variants of null are not supported") ErrTypeRecursive = errors.New("recursive type") ErrTypeIllegalRoot = errors.New("root type must be a struct type and must not " + "implement encoding.TextUnmarshaler and yaml.Unmarshaler") ErrTypeMissingYAMLTag = errors.New("missing yaml struct tag") ErrTypeEnvTagOnUnexported = errors.New("env tag on unexported field") ErrTypeTagOnInterfaceImpl = errors.New("implementations of interfaces " + "yaml.Unmarshaler or encoding.TextUnmarshaler must not " + "contain yaml and env tags") ErrTypeEnvOnYAMLUnmarsh = errors.New("env var on yaml.Unmarshaler implementation") ErrTypeNoExportedFields = errors.New("no exported fields") ErrTypeInvalidEnvTag = fmt.Errorf("invalid env struct tag: "+ "must match the POSIX env var regexp: %s", regexEnvVarPOSIXPattern) ErrTypeEnvVarOnUnsupportedType = errors.New("env var on unsupported type") ErrTypeUnsupported = errors.New("unsupported type") ErrTypeUnsupportedPtrType = errors.New("unsupported pointer type") ErrEnvInvalidVar = errors.New("invalid env var") )
All possible errors.
Functions ¶
func Load ¶
Load reads and validates the configuration of type T from yamlSource. Load behaves similar to LoadFile.
func LoadFile ¶ added in v0.2.0
LoadFile reads and validates the configuration of type T from a YAML file. Will return an error if:
- ValidateType returns an error for T.
- the yaml file is empty or not found.
- the yaml file doesn't contain a field specified by T.
- the yaml file is missing a field specified by T.
- the yaml file contains values that don't pass validation.
- the yaml file contains boolean literals other than `true` and `false`.
- the yaml file contains null values other than `null` (`~`, etc.).
- the yaml file assigns `null` to a non-pointer Go type.
- the yaml file contains any YAML tags (https://yaml.org/spec/1.2.2/#3212-tags).
- the yaml file contains any redeclared anchors.
- the yaml file contains any unused anchors.
- the yaml file contains any anchors with implicit null value (no value).
func Validate ¶ added in v0.7.0
Validate behaves similar to Load and LoadFile just without parsing YAML and instead performing the same type and value checks on t. Validate will obviously not report line:column error location. Validate first validates type T, then validates t according to go-playground/validator struct tags, then recursively invokes all Validate methods returning an error if any.
func ValidateType ¶ added in v0.2.0
ValidateType returns an error if...
- T contains any struct field without a "yaml" struct tag.
- T contains any struct field with an invalid "env" struct tag.
- T is recursive.
- T contains any unsupported types (signed and unsigned integers with unspecified width, interface (including `any`), function, channel, unsafe.Pointer, pointer to pointer, pointer to slice, pointer to map).
- T is not a struct or implements yaml.Unmarshaler or encoding.TextUnmarshaler.
- T contains any structs with no exported fields.
- T contains any structs with yaml and/or env tags assigned to unexported fields.
- T contains any struct implementing either yaml.Unmarshaler or encoding.TextUnmarshaler that contains fields with yaml or env struct tags.
- T contains any fields with env tag on a type that implements yaml.Unmarshaler.
- T contains any struct containing multiple fields with the same yaml tag.