config

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 9 Imported by: 6

README

Darvaza Config

Go Reference Go Report Card

darvaza.org/x/config provides helpers for dealing with config files.

AppDir

appdir contains helpers to determine the location of application specific files.

Default values

Wrappers for github.com/amery/defaults:

  • SetDefaults()
  • Set()
  • CanUpdate()

Environment

Expand shell-style variables:

  • FromString()
  • FromBytes()
  • FromReader()
  • FromFile()

Loader

Attempts to decode an object from one of a list of filenames.

Validations

Wrappers for github.com/go-playground/validator/v10:

  • Validate()
  • AsValidationError()
  • and Prepare(). calling SetDefaults() and Validate().

See also

Documentation

Overview

Package config provides helpers for handling config structs

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsValidationErrors

func AsValidationErrors(err error) (validator.ValidationErrors, bool)

AsValidationErrors gives access to a slice of validator.FieldError

func CanUpdate

func CanUpdate(v any) bool

CanUpdate returns true when the given value is an initial value of its type

func Join added in v0.3.0

func Join(directories []string, base string, extensions []string) ([]string, error)

Join combines a list of directories with a name and an optional list of extensions. This are `/` separated, absolute, but without the initial `/`. Following fs.ValidPath rules. Final result is cleaned.

func NewPathError added in v0.3.0

func NewPathError(filename, op string, err error) *os.PathError

NewPathError is a shortcut to generate os.PathError.

func Prepare

func Prepare(v any) error

Prepare runs SetDefaults and Validate

func Set

func Set(v any) error

Set applies `default` struct-tags and SetDefaults() recursively. If the given object has a `SetDefaults() error` method, it will be ignored. Any `SetDefaults() error` deeper in the struct will be called.

func SetDefaults

func SetDefaults(v any) error

SetDefaults applies `default` struct-tags and SetDefaults() recursively. If the given object has a `SetDefaults() error` method, it will be invoked instead.

func Validate

func Validate(v any) error

Validate validates exposed fields including nested structs

Types

type Decoder added in v0.3.0

type Decoder[T any] interface {
	Decode(name string, data []byte) (*T, error)
}

A Decoder attempts to convert the contents of a file into a [T] type.

type DecoderFunc added in v0.3.0

type DecoderFunc[T any] func(string, []byte) (*T, error)

A DecoderFunc represents a function that can act as a full Decoder.

func (DecoderFunc[T]) Decode added in v0.3.0

func (df DecoderFunc[T]) Decode(name string, data []byte) (*T, error)

Decode converts the contents of a file into an entity of a given [T] type.

type Loader added in v0.3.0

type Loader[T any] struct {

	// NewDecoder returns a [Decoder] based on the filename
	NewDecoder func(string) (Decoder[T], error)

	// IsSkip checks if the error returned by the [Decoder]
	// indicates we should try the next option instead of
	// failing. [os.IsNotExist] is always tested first.
	IsSkip func(error) bool

	// Options are applied to objects after decoding and
	// before Load() returns.
	Options []Option[T]
	// contains filtered or unexported fields
}

Loader tries to load an object from the first success on a list of options.

func (*Loader[T]) Last added in v0.3.0

func (l *Loader[T]) Last() (fs.FS, string)

Last returns the filename last used. empty if it was the Fallback.

func (*Loader[T]) New added in v0.3.0

func (l *Loader[T]) New() (*T, error)

New creates a new object

func (*Loader[T]) NewFromFile added in v0.3.0

func (l *Loader[T]) NewFromFile(fSys fs.FS, names ...string) (*T, error)

NewFromFile returns the first successfully decoded option.

func (*Loader[T]) NewFromFileOS added in v0.3.0

func (l *Loader[T]) NewFromFileOS(names ...string) (*T, error)

NewFromFileOS returns the first successfully decoded option.

type Option added in v0.3.0

type Option[T any] func(*T) error

An Option if a function used to configure an object after decoding.

Directories

Path Synopsis
Package appdir provides helpers to compose filenames to be used by applications.
Package appdir provides helpers to compose filenames to be used by applications.
Package expand implements helpers to expand shell-style variables within config files before they are parsed
Package expand implements helpers to expand shell-style variables within config files before they are parsed

Jump to

Keyboard shortcuts

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