package
0.0.4
Repository: https://github.com/kt-micro/go-os-proto.git
Documentation: pkg.go.dev

# README

Config GoDoc

Provides a high level pluggable abstraction for dynamic configuration.

Interface

There's a need for dynamic configuration with namespacing, deltas for rollback, watches for changes and an audit log. At a low level we may care about server addresses changing, routing information, etc. At a high level there may be a need to control business level logic; External API Urls, Pricing information, etc.

// Config is the top level config which aggregates a
// number of sources and provides a single merged
// interface.
type Config interface {
	// Config values
	Values
	// Config options
	Options() Options
	// Render unusable
	Close() error
	// String name of config
	String() string
}

// Values loaded within the config
type Values interface {
	// The path could be a nested structure so
	// make it a composable.
	// Returns internal cached value
	Get(path ...string) Value
	// Sets internal cached value
	Set(val interface{}, path ...string)
	// Deletes internal cached value
	Del(path ...string)
	// Returns vals as bytes
	Bytes() []byte
}

// Represent a value retrieved from the values loaded
type Value interface {
	Bool(def bool) bool
	Int(def int) int
	String(def string) string
	Float64(def float64) float64
	Duration(def time.Duration) time.Duration
	StringSlice(def []string) []string
	StringMap(def map[string]string) map[string]string
	Scan(val interface{}) error
	Bytes() []byte
}

func NewConfig(opts ...Option) Config {
	return newOS(opts...)
}

Supported Backends

Usage

Config provides a way to use configuration that's dynamically loaded from a variety of backends and subscribe to changes. It also allows the ability to set default values where config might be missing.

	// Create a config instance
	config := config.NewConfig(
		// Poll every minute for changes
		config.PollInterval(time.Minute),
		// Use file as a config source
		// Multiple sources can be specified
		config.WithSource(file.NewSource(config.SourceName(configFile))),
	)

	defer config.Close()

	// Get config at path as string
	// Sets value 'default' if it does not exist
	val := config.Get("path", "to", "key").String("default")


	// Scan into some type
	var ival map[string]interface{}
	if err := config.Get("path", "to", "key").Scan(&ival); err != nil {
		fmt.Println(err)
		return
	}

	// Watch for changes. Optionally specify path to watch.
	w, err := c.Watch("path", "to", "key")
	if err != nil {
		fmt.Println(err)
		return
	}

	for {
		// Block waiting for changes from watcher.
		v, err := w.Next()
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("Received value for key:", v.String("default"))
	}

Config Format

The config format expected for backend sources by default is JSON. This is handled by the Reader interface. Multiple sources will be read and merged down based on the order they were configured in options.

{
	"path": {
		"to": {
			"key": ["foo", "bar"]
		}
	}
}

# Packages

Package go_micro_os_config is a generated protocol buffer package.

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
New json reader.
No description provided by the author
PollInterval is the time interval at which the sources are polled to retrieve config.
No description provided by the author
No description provided by the author
SourceName is an option to provide name of a file, a url, key within etcd, consul, zookeeper, etc.
No description provided by the author
WithReader is the reader used by config to parse ChangeSets, merge them and provide values.
WithSource appends a source to our list of sources.

# Variables

No description provided by the author
No description provided by the author

# Structs

ChangeSet represents a set an actual source.
No description provided by the author
No description provided by the author

# Interfaces

Config is the top level config which aggregates a number of sources and provides a single merged interface.
Reader takes a ChangeSet from a source and returns a single merged ChangeSet e.g reads ChangeSet as JSON and can merge down.
Source is the source from which config is loaded.
SourceWatcher allows you to watch a source for changes Next is a blocking call which returns the next ChangeSet update.
Represent a value retrieved from the values loaded.
Values loaded within the config.
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author