config

package
v0.0.0-...-66343a0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: BSD-3-Clause, BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Simple abstractions to read and write configuration parameters by name.

At the heart of the config module there is the `Store` interface, which allows to load (Unmarshal) or store (Marshal) a configuration through a unique name.

For example, once you have a Store object, you can store or load a configuration with:

config := Config{
    Server: "127.0.0.1",
    Port: 53,
}

if err := store.Marshal("server-config", config); err != nil {
   ...
}

... load it later ...

if _, err := store.Unmarshal("server-config", &config); err != nil {
   ...
}

The "server-config" string is ... just a string. A key by which the configuration is known by. Different Store implementations will use it differently: they may turn it into a file name, into the key of a database, ...

Internally, a `Store` does two things:

  1. It converts your config object into some binary blob (marshal, unmarshal).
  2. It reads and writes this blob somewhere.

Some databases and config stores use their own marshalling mechanism, while others have no built in marshalling, and rely on a standard mechanism like a json, yaml, or gob encoder.

If you need to store configs on a file system or database that does not have a native marshalling/unmarshalling scheme, you can implement the `Loader` interface, and then use NewSimple() or NewMulti() to turn a Loader into a Store.

NewSimple and NewMulti wrap a store around an object capable of using one of the standard encoders/decoders provided by go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Descriptor

type Descriptor interface{}

Represents a file that was Unmarshalled.

Use descriptors to guarantee that a file is saved in the same location it was read from.

type Loader

type Loader interface {
	List() ([]string, error)
	Read(name string) ([]byte, error)
	Write(name string, data []byte) error
	Delete(name string) error
}

Implement the Loader interface to prvoide mechanisms to read and write configuration files.

If you have an object implementing the Loader interface, you can then use NewSimple() or NewMulti() to turn it into a Store.

type MultiFormat

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

func NewMulti

func NewMulti(loader Loader, marshaller ...marshal.FileMarshaller) *MultiFormat

func (*MultiFormat) Delete

func (ss *MultiFormat) Delete(desc Descriptor) error

func (*MultiFormat) List

func (ss *MultiFormat) List() ([]string, error)

List returns the list of configs the loader knows about.

If a config exists in multiple formats, list will return all known formats. The names returned are usable to be passed directly to Unmarshal, but may contain an extension that was not added to begin with.

For example:

mf.Marshal("config", Config{})
mf.Marshal("config.json", Config{})

will results in a "config.toml" file (default preferred format) and "config.json" file being created.

List() will return "config.toml" and "config.json" both.

Unmarshal() can be called with Unmarshal("config"), which will result in the "config.toml" file being parsed, with Unmarsahl("config.toml"), or with Unmarshal("config.json"), as desired.

In general, the value returned by List is guaranteed to be usable with Unmarshal, but may not match the value that was passed to Marshal before.

func (*MultiFormat) Marshal

func (ss *MultiFormat) Marshal(desc Descriptor, value interface{}) error

func (*MultiFormat) Unmarshal

func (ss *MultiFormat) Unmarshal(name string, value interface{}) (Descriptor, error)

type Opener

type Opener func(name string, namespace ...string) (Store, error)

Opener is any function that is capable of opening a store.

type SimpleStore

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

func NewSimple

func NewSimple(loader Loader, marshaller marshal.Marshaller) *SimpleStore

func (*SimpleStore) Delete

func (ss *SimpleStore) Delete(desc Descriptor) error

func (*SimpleStore) List

func (ss *SimpleStore) List() ([]string, error)

func (*SimpleStore) Marshal

func (ss *SimpleStore) Marshal(desc Descriptor, value interface{}) error

func (*SimpleStore) Unmarshal

func (ss *SimpleStore) Unmarshal(name string, value interface{}) (Descriptor, error)

type Store

type Store interface {
	// List the object names available for unmarshalling.
	List() ([]string, error)

	// Marshal saves an object, specified by value, under the name specified in descriptor.
	//
	// descriptor is either a string, indicating the desired unique name to store the
	// object as, or an object returned by Unmarshal.
	//
	// Using a descriptor returned by Unmarshal guarantees that the object is written
	// in exactly the same location where it was retrieved. This is useful for object
	// stores that allow writing in multiple locations at once.
	Marshal(descriptor Descriptor, value interface{}) error

	// Unmarshal will read an object from the config store, and parse it into the value supplied,
	// which should generally be a pointer.
	//
	// Unmarshal returns a descriptor that can be passed back to Marshal to store data into this object.
	//
	// In case the config file cannot be found, os.IsNotExist(error) will return true.
	Unmarshal(name string, value interface{}) (Descriptor, error)

	// Deletes an object.
	//
	// descriptor is either a string, indicating the desired unique name of the object
	// to delete, or an object returned by Unmarshal.
	//
	// When specifying a string, Delete guarantees that all copies of the object known
	// by that string are deleted.
	//
	// When specifying a Descriptor, Delete will only delete that one instance of the object.
	Delete(descriptor Descriptor) error
}

Store is the interface normally used from this library.

It allows to load config files and store them, by using the Marshal and Unmarshal interface.

Directories

Path Synopsis
Package datastore provides a Store capable of storing configs on datastore.
Package datastore provides a Store capable of storing configs on datastore.
Returns the default config store used in the enkit repository.
Returns the default config store used in the enkit repository.
Config loaders to read/write files in directories.
Config loaders to read/write files in directories.
Utility functions to load, process, and store identity tokens in a config store.
Utility functions to load, process, and store identity tokens in a config store.
Generic marshalling and unmarshalling interfaces.
Generic marshalling and unmarshalling interfaces.
A config Store able to load and store configs via simple HTTP requests.
A config Store able to load and store configs via simple HTTP requests.

Jump to

Keyboard shortcuts

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