blob

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 6 Imported by: 10

README

blob

A library for loading configuration files into config.

GoDoc

Blobs represent configuration sources containing configuration stored as a block and in a known format, e.g. config files. Blobs are partitioned into two layers, the Loader, which loads the configuration as a []byte blob from the source, and the Decoder, which decodes the blob into a form that can be used by config.

The Loader may support being watched for changes, and if so the Blob containing it will automatically support being watched for changes as well.

Quick Start

Given a loader and decoder, you create a blob and add it to your config:

    c := config.New(blob.New(file.New("config.json"), json.NewDecoder()))

or add it into your config stack:

    c := config.New(pflag.New(), env.New())
    // ...
    cfgFile := blob.New(file.New("config.json"), json.NewDecoder())
    c.Append(cfgFile)

Loaders

Loaders read configuration from some source.

The following loaders are provided:

Loader Configuration Source
bytes []byte
file local file

Decoders

Decoders unmarshal configuration from a particular textual format.

Decoders for the following formats are provided:

Documentation

Overview

Package blob provides a getter that loads and decodes configuration from a source where the configuration is stored in a known format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConfigFile added in v0.2.0

func NewConfigFile(cfg *config.Config, pathfield string,
	defpath string, fdec Decoder, foptions ...file.Option) config.Getter

NewConfigFile is a helper function that creates a File getter. The config file path is defined in either the existing config, in a field indicated by pathfield, or a default path. If the config file is specified in the config then it must exist and load, or the function will panic. If the config file is not specified in the config then the default config file is used, if it exists, or an empty Getter. Any provided foptions are passed to the File constructor.

Types

type Decoder

type Decoder interface {
	Decode(b []byte, v interface{}) error
}

Decoder unmarshals configuration from raw []byte into the provided type, typically a map[string]interface{}.

type ErrorHandler added in v0.2.0

type ErrorHandler func(error)

ErrorHandler handles an error.

type ErrorHandlerOption added in v0.2.0

type ErrorHandlerOption struct {
	// contains filtered or unexported fields
}

ErrorHandlerOption defines the handler for errors returned during construction and the initial load.

func MustLoad added in v0.2.0

func MustLoad() ErrorHandlerOption

MustLoad requires that the Blob successfully loads during construction. Note that this option overrides any earlier ErrorHandlerOptions.

func WithErrorHandler added in v0.2.0

func WithErrorHandler(e ErrorHandler) ErrorHandlerOption

WithErrorHandler is an Option that sets the error handling for an object.

type Getter

type Getter struct {
	config.GetterAsOption
	// contains filtered or unexported fields
}

Getter represents a two stage getter for blobs. The first stage is the Loader which retrieves the configuration as a []byte blob from an underlying source. The second stage is the Decoder, which converts the returned []byte blob into a map[string]interface{}.

func New

func New(l Loader, d Decoder, options ...Option) *Getter

New creates a new Blob using the provided loader and decoder. The configuration is loaded and decoded during construction, else an error is returned.

func (*Getter) Get

func (g *Getter) Get(key string) (interface{}, bool)

Get implements the Getter API.

func (*Getter) NewWatcher

func (g *Getter) NewWatcher(done <-chan struct{}) config.GetterWatcher

NewWatcher creates a watcher for the getter. Returns nil if the getter does not support being watched.

type Loader

type Loader interface {
	Load() ([]byte, error)
}

Loader retrieves raw configuration data, as []byte, from some source. The Loader may also support the watchableLoader interface if it is watchable.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is a construction option for a Blob.

type SeparatorOption

type SeparatorOption struct {
	// contains filtered or unexported fields
}

SeparatorOption defines the string that separates tiers in keys.

func WithSeparator

func WithSeparator(s string) SeparatorOption

WithSeparator is an Option that sets the config namespace separator. This is an option to ensure it can only set at construction time, as changing it at runtime makes no sense.

type WatchableLoader

type WatchableLoader interface {
	NewWatcher(done <-chan struct{}) <-chan error
}

WatchableLoader is the interface supported by Loaders that can be watched for changes.

Directories

Path Synopsis
Package decoder contains decoders that convert blobs of configuration from raw bytes into map[string]interface{}.
Package decoder contains decoders that convert blobs of configuration from raw bytes into map[string]interface{}.
hcl
Package hcl provides a HCL format decoder for config.
Package hcl provides a HCL format decoder for config.
ini
Package ini provides an INI format decoder for config.
Package ini provides an INI format decoder for config.
json
Package json provides a JSON format decoder for config.
Package json provides a JSON format decoder for config.
properties
Package properties provides a Java properties format decoder for config.
Package properties provides a Java properties format decoder for config.
toml
Package toml provides a TOML format decoder for config.
Package toml provides a TOML format decoder for config.
yaml
Package yaml provides a YAML format decoder for config.
Package yaml provides a YAML format decoder for config.
Package loader contains loaders that read blobs of configuration from various sources.
Package loader contains loaders that read blobs of configuration from various sources.
bytes
Package bytes provides a loader from []byte for config.
Package bytes provides a loader from []byte for config.
file
Package file provides a file loader for config.
Package file provides a file loader for config.

Jump to

Keyboard shortcuts

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