loader

package
v4.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidOutValue is returned when a load function is passed an invalid out parameter.
	ErrInvalidOutValue = errors.New("invalid out value")
	// ErrLoadFailed is returned when a file failed to be loaded.
	ErrLoadFailed = errors.New("failed to load file")
)

Functions

func LoadDirFiles

func LoadDirFiles(loader Loader, path string, out interface{}) []error

LoadDirFiles loads files contained in a directory into a target slice. Only a single directory level is loaded. Subdirectories are ignored. Failure to read a file will eventually be returned as an error, but will not stop the function from loading the rest of files in the target directory. `path` must be a directory path. `out` must be a map. The map must have string keys and pointer to interface type the files will be placed in as values (e.g. map[string]*Target). The keys of the map will contain filenames of loaded files, and the values will contain the loaded files.

The return value will contain all errors found when attempting to load files. Returned error types can be checked using `errors.Is`. Errors of type ErrLoadFailed indicate that the file was not accessible. It is up to the caller to consider this a critical error. All other errors are critical.

func LoadFile

func LoadFile(loader Loader, path string, out interface{}) error

LoadFile loads a single file into a target struct.

Types

type Loader

type Loader interface {
	// Load loads a file from a specific path into the passed output value.
	//	 Loaders that do not use files can ignore the `path` parameter.
	Load(path string, out interface{}) error
	// TrimExt returns the filename without the file extension.
	//	 Loaders should remove the extension of the file they expect as config.
	//	 - A YAML Loader will remove the `.yaml` extension.
	//	 - A JSON Loader will remove the `.json` extension.
	TrimExt(filename string) string
	// Filter returns the list of files that match the extension this Loader can process.
	Filter(list []string) []string
}

Loader reads and parses files into Go structs

func NewYAMLLoader

func NewYAMLLoader(logger gz.Logger) Loader

NewYAMLLoader returns a YAML loader.

Jump to

Keyboard shortcuts

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