Documentation
¶
Index ¶
- func IsDuplicateKeyError(err error) bool
- func IsOverflowError(err error) bool
- func IsSyntaxError(err error) bool
- func IsTypeError(err error) bool
- func IsUnknownFieldError(err error) bool
- func Parse[T any](path string, opts ...Option) (*T, error)
- func ParseFS[T any](fs embed.FS, path string, opts ...Option) (*T, error)
- func ParseWithConfig[T any](cfg *T, path string, opts ...Option) (*T, error)
- func Position(err error) (line, column int, ok bool)
- func UnmarshalConfig[T any](cfg *T, data []byte, opts ...Option) error
- type DuplicateKeyError
- type Error
- type Option
- func AllowDuplicateKeys() Option
- func AllowFieldPrefixes(prefixes ...string) Option
- func AllowUnknownFields() Option
- func UseOrderedMap() Option
- func WithEmbeddedReferences(fsys embed.FS, paths ...string) Option
- func WithRecursiveDirs(dirs ...string) Option
- func WithReferenceDirs(dirs ...string) Option
- func WithReferenceFiles(paths ...string) Option
- func WithReferenceReaders(readers ...io.Reader) Option
- func WithValidator(v StructValidator) Option
- type OverflowError
- type StructValidator
- type SyntaxError
- type TypeError
- type UnknownFieldError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsDuplicateKeyError ¶ added in v0.8.0
IsDuplicateKeyError reports whether err was caused by a duplicate mapping key (only possible without AllowDuplicateKeys). goccy currently reports duplicate keys as a SyntaxError whose message says the key is "already defined", so this check also matches that form.
func IsOverflowError ¶ added in v0.8.0
IsOverflowError reports whether err was caused by a number that overflows the destination Go type.
func IsSyntaxError ¶ added in v0.8.0
IsSyntaxError reports whether err was caused by malformed YAML.
func IsTypeError ¶ added in v0.8.0
IsTypeError reports whether err was caused by a YAML value that cannot be converted to the destination Go type.
func IsUnknownFieldError ¶ added in v0.8.0
IsUnknownFieldError reports whether err was caused by a YAML key that does not match any struct field (only possible without AllowUnknownFields).
func Parse ¶
Parse takes the given path and attempts to read and unmarshal the config The given config will also be validated if it has a Validate function on it
Example ¶
package main
import (
"log/slog"
"github.com/aranw/yamlcfg"
)
type Config struct {
LogLevel string `yaml:"log_level"`
}
func main() {
cfg, err := yamlcfg.Parse[Config]("config.yaml")
if err != nil {
slog.Error("loading yaml config", "err", err)
return
}
_ = cfg.LogLevel
}
Output:
func ParseFS ¶
ParseFS attempts to load the given path and config from a embed.FS
Example ¶
package main
import (
"embed"
"log/slog"
"github.com/aranw/yamlcfg"
)
type Config struct {
LogLevel string `yaml:"log_level"`
}
//go:embed testdata
var testdata embed.FS
func main() {
cfg, err := yamlcfg.ParseFS[Config](testdata, "config.yaml")
if err != nil {
slog.Error("loading yaml config", "err", err)
return
}
_ = cfg.LogLevel
}
Output:
func ParseWithConfig ¶ added in v0.2.0
ParseWithConfig takes the given config and path and attempts to read and unmarshal the config The given config can be populated with default values If the given config implements a `Validate() error` function this will be called
Example ¶
package main
import (
"log/slog"
"github.com/aranw/yamlcfg"
)
type Config struct {
LogLevel string `yaml:"log_level"`
}
func main() {
defaults := &Config{
LogLevel: "info",
}
cfg, err := yamlcfg.ParseWithConfig(defaults, "config.yaml")
if err != nil {
slog.Error("loading yaml config", "err", err)
return
}
_ = cfg.LogLevel
}
Output:
func Position ¶ added in v0.8.0
Position returns the line and column (1-based) of the YAML token that caused err, if that information is available.
func UnmarshalConfig ¶
UnmarshalConfig takes the provided yaml data and unmarshals it into the provided config struct. It will return an error if the decoding fails or if the yaml data is not in the expected format. It will also expand any environment variables in the yaml data
Example ¶
package main
import (
"fmt"
"log/slog"
"github.com/aranw/yamlcfg"
)
type Config struct {
LogLevel string `yaml:"log_level"`
}
func main() {
data := []byte(`log_level: "debug"`)
var cfg Config
if err := yamlcfg.UnmarshalConfig(&cfg, data); err != nil {
slog.Error("unmarshalling config", "err", err)
return
}
fmt.Println(cfg.LogLevel)
}
Output: debug
Types ¶
type DuplicateKeyError ¶ added in v0.8.0
type DuplicateKeyError = yaml.DuplicateKeyError
DuplicateKeyError reports a duplicate mapping key.
type Error ¶ added in v0.8.0
Error is implemented by every typed error below. It provides the offending token, a plain message, and pretty source-formatted output.
type Option ¶ added in v0.8.0
type Option func(*decodeConfig)
Option customises how a YAML config is decoded. Options are accepted by Parse, ParseWithConfig, ParseFS and UnmarshalConfig.
func AllowDuplicateKeys ¶ added in v0.8.0
func AllowDuplicateKeys() Option
AllowDuplicateKeys allows duplicate mapping keys in the YAML input. Without this option duplicate keys are an error. When allowed, the last value wins.
func AllowFieldPrefixes ¶ added in v0.8.0
AllowFieldPrefixes, while strict decoding is enabled, allows YAML keys with the given prefixes to bypass the unknown field check.
func AllowUnknownFields ¶ added in v0.8.0
func AllowUnknownFields() Option
AllowUnknownFields disables strict decoding so that YAML keys that do not match any struct field are ignored instead of rejected.
func UseOrderedMap ¶ added in v0.8.0
func UseOrderedMap() Option
UseOrderedMap preserves mapping key order by decoding untyped values (interface{} destinations) as a *yaml.MapSlice instead of a map.
func WithEmbeddedReferences ¶ added in v0.8.0
WithEmbeddedReferences allows a config parsed from an embed.FS to reference anchors and aliases defined in other YAML files from the same embed.FS. Files are opened lazily, so a missing file surfaces as a decode error.
func WithRecursiveDirs ¶ added in v0.8.0
WithRecursiveDirs works like WithReferenceDirs but searches the given directories recursively.
func WithReferenceDirs ¶ added in v0.8.0
WithReferenceDirs allows the config to reference anchors and aliases defined in YAML files under the given directories (searched non-recursively).
func WithReferenceFiles ¶ added in v0.8.0
WithReferenceFiles allows the config to reference anchors and aliases defined in the given YAML files. Note that environment variable expansion only applies to the main config data, not to referenced files.
func WithReferenceReaders ¶ added in v0.8.0
WithReferenceReaders allows the config to reference anchors and aliases defined in YAML documents read from the given readers.
func WithValidator ¶ added in v0.8.0
func WithValidator(v StructValidator) Option
WithValidator registers a StructValidator that runs against the decoded value before decoding is considered successful.
type OverflowError ¶ added in v0.8.0
type OverflowError = yaml.OverflowError
OverflowError reports a number that overflows the destination type.
type StructValidator ¶ added in v0.8.0
type StructValidator = yaml.StructValidator
StructValidator validates a decoded value. It matches the interface implemented by validation libraries such as go-playground/validator (see validator.Validate.Struct), which can be plugged in directly.
type SyntaxError ¶ added in v0.8.0
type SyntaxError = yaml.SyntaxError
SyntaxError reports malformed YAML, including duplicate mapping keys.
type TypeError ¶ added in v0.8.0
TypeError reports a YAML value that cannot be converted to the destination Go type.
type UnknownFieldError ¶ added in v0.8.0
type UnknownFieldError = yaml.UnknownFieldError
UnknownFieldError reports a YAML key with no matching struct field.