config

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2022 License: MIT Imports: 15 Imported by: 2

Documentation

Overview

Package config provides features to abstract the specification of a configuration value, from the consumption of a configuration value, using environment variables.

Index

Examples

Constants

View Source
const (
	// MinInt is the minimum value that can be expressed using the int type.
	MinInt = -MaxInt - 1 // -1 << 31 or -1 << 63

	// MaxInt is the maximum value that can be expressed using the int type.
	MaxInt = 1<<(uintBitSize-1) - 1 // 1<<31 - 1 or 1<<63 - 1
)
View Source
const (
	// MaxUint is the maximum value that can be expressed using the uint type.
	MaxUint = 1<<uintBitSize - 1 // 1<<32 - 1 or 1<<64 - 1

)

Variables

This section is empty.

Functions

func AsBool added in v1.0.0

func AsBool(b Bucket, k string) bool

AsBool returns the boolean representation of the value associated with k or panics if unable to do so.

It returns true if the value "true", "yes" or "on", or false if the value is "false", "no" or "off".

func AsBoolDefault added in v1.0.0

func AsBoolDefault(b Bucket, k string, v bool) bool

AsBoolDefault returns the boolean representation of the value associated with k, or the default value v if k is undefined.

It returns true if the value "true", "yes" or "on", or false if the value is "false", "no" or "off".

func AsBoolF added in v1.0.0

func AsBoolF(b Bucket, k string) bool

AsBoolF returns the boolean representation of the value associated with k, or true if k is undefined.

It returns true if the value "true", "yes" or "on", or false if the value is "false", "no" or "off".

func AsBoolT added in v1.0.0

func AsBoolT(b Bucket, k string) bool

AsBoolT returns the boolean representation of the value associated with k, or true if k is undefined.

It returns true if the value "true", "yes" or "on", or false if the value is "false", "no" or "off".

func AsBytes added in v1.0.0

func AsBytes(b Bucket, k string) []byte

AsBytes returns the byte-slice representation of the value associated with k or panics if unable to do so.

func AsBytesDefault added in v1.0.0

func AsBytesDefault(b Bucket, k string, v []byte) []byte

AsBytesDefault returns the byte-slice representation of the value associated with k, or the default value v if k is undefined.

func AsDuration added in v1.0.0

func AsDuration(b Bucket, k string) time.Duration

AsDuration returns the time.Duration representation of the value associated with k or panics if unable to do so.

Durations are specified using the syntax supported by time.ParseDuration.

func AsDurationBetween added in v1.0.0

func AsDurationBetween(b Bucket, k string, min, max time.Duration) time.Duration

AsDurationBetween returns the time.Duration representation of the value associated with k or panics if unable to do so.

Durations are specified using the syntax supported by time.ParseDuration.

It panics if the value is not between min and max (inclusive).

func AsDurationDefault added in v1.0.0

func AsDurationDefault(b Bucket, k string, v time.Duration) time.Duration

AsDurationDefault returns the time.Duration representation of the value associated with k, or the default value v if k is undefined.

Durations are specified using the syntax supported by time.ParseDuration.

func AsDurationDefaultBetween added in v1.0.0

func AsDurationDefaultBetween(b Bucket, k string, v, min, max time.Duration) time.Duration

AsDurationDefaultBetween returns the time.Duration representation of the value associated with k, or the default value v if k is undefined.

Durations are specified using the syntax supported by time.ParseDuration.

It panics if the value is not between min and max (inclusive).

func AsFloat32 added in v1.0.0

func AsFloat32(b Bucket, k string) float32

AsFloat32 returns the float32 representation of the value associated with k or panics if unable to do so.

func AsFloat32Between added in v1.0.0

func AsFloat32Between(b Bucket, k string, min, max float32) float32

AsFloat32Between returns the float32 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsFloat32Default added in v1.0.0

func AsFloat32Default(b Bucket, k string, v float32) float32

AsFloat32Default returns the float32 representation of the value associated with k, or the default value v if k is undefined.

func AsFloat32DefaultBetween added in v1.0.0

func AsFloat32DefaultBetween(b Bucket, k string, v, min, max float32) float32

AsFloat32DefaultBetween returns the float32 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsFloat64 added in v1.0.0

func AsFloat64(b Bucket, k string) float64

AsFloat64 returns the float64 representation of the value associated with k or panics if unable to do so.

func AsFloat64Between added in v1.0.0

func AsFloat64Between(b Bucket, k string, min, max float64) float64

AsFloat64Between returns the float64 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsFloat64Default added in v1.0.0

func AsFloat64Default(b Bucket, k string, v float64) float64

AsFloat64Default returns the float64 representation of the value associated with k, or the default value v if k is undefined.

func AsFloat64DefaultBetween added in v1.0.0

func AsFloat64DefaultBetween(b Bucket, k string, v, min, max float64) float64

AsFloat64DefaultBetween returns the float64 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsInt added in v1.0.0

func AsInt(b Bucket, k string) int

AsInt returns the int representation of the value associated with k or panics if unable to do so.

func AsInt16 added in v1.0.0

func AsInt16(b Bucket, k string) int16

AsInt16 returns the int16 representation of the value associated with k or panics if unable to do so.

func AsInt16Between added in v1.0.0

func AsInt16Between(b Bucket, k string, min, max int16) int16

AsInt16Between returns the int16 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsInt16Default added in v1.0.0

func AsInt16Default(b Bucket, k string, v int16) int16

AsInt16Default returns the int16 representation of the value associated with k, or the default value v if k is undefined.

func AsInt16DefaultBetween added in v1.0.0

func AsInt16DefaultBetween(b Bucket, k string, v, min, max int16) int16

AsInt16DefaultBetween returns the int16 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsInt32 added in v1.0.0

func AsInt32(b Bucket, k string) int32

AsInt32 returns the int32 representation of the value associated with k or panics if unable to do so.

func AsInt32Between added in v1.0.0

func AsInt32Between(b Bucket, k string, min, max int32) int32

AsInt32Between returns the int32 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsInt32Default added in v1.0.0

func AsInt32Default(b Bucket, k string, v int32) int32

AsInt32Default returns the int32 representation of the value associated with k, or the default value v if k is undefined.

func AsInt32DefaultBetween added in v1.0.0

func AsInt32DefaultBetween(b Bucket, k string, v, min, max int32) int32

AsInt32DefaultBetween returns the int32 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsInt64 added in v1.0.0

func AsInt64(b Bucket, k string) int64

AsInt64 returns the int64 representation of the value associated with k or panics if unable to do so.

func AsInt64Between added in v1.0.0

func AsInt64Between(b Bucket, k string, min, max int64) int64

AsInt64Between returns the int64 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsInt64Default added in v1.0.0

func AsInt64Default(b Bucket, k string, v int64) int64

AsInt64Default returns the int64 representation of the value associated with k, or the default value v if k is undefined.

func AsInt64DefaultBetween added in v1.0.0

func AsInt64DefaultBetween(b Bucket, k string, v, min, max int64) int64

AsInt64DefaultBetween returns the int64 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsInt8 added in v1.0.0

func AsInt8(b Bucket, k string) int8

AsInt8 returns the int8 representation of the value associated with k or panics if unable to do so.

func AsInt8Between added in v1.0.0

func AsInt8Between(b Bucket, k string, min, max int8) int8

AsInt8Between returns the int8 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsInt8Default added in v1.0.0

func AsInt8Default(b Bucket, k string, v int8) int8

AsInt8Default returns the int8 representation of the value associated with k, or the default value v if k is undefined.

func AsInt8DefaultBetween added in v1.0.0

func AsInt8DefaultBetween(b Bucket, k string, v, min, max int8) int8

AsInt8DefaultBetween returns the int8 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsIntBetween added in v1.0.0

func AsIntBetween(b Bucket, k string, min, max int) int

AsIntBetween returns the int representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsIntDefault added in v1.0.0

func AsIntDefault(b Bucket, k string, v int) int

AsIntDefault returns the int representation of the value associated with k, or the default value v if k is undefined.

func AsIntDefaultBetween added in v1.0.0

func AsIntDefaultBetween(b Bucket, k string, v, min, max int) int

AsIntDefaultBetween returns the int representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsString added in v1.0.0

func AsString(b Bucket, k string) string

AsString returns the string representation of the value associated with k or panics if unable to do so.

func AsStringDefault added in v1.0.0

func AsStringDefault(b Bucket, k string, v string) string

AsStringDefault returns the string representation of the value associated with k, or the default value v if k is undefined.

func AsURL added in v1.1.0

func AsURL(b Bucket, k string) *url.URL

AsURL returns the url.URL representation of the value associated with k or panics if unable to do so.

func AsURLDefault added in v1.1.0

func AsURLDefault(b Bucket, k, v string) *url.URL

AsURLDefault returns the url.URL representation of the value associated with k, or the default value v if k is undefined.

func AsUint added in v1.0.0

func AsUint(b Bucket, k string) uint

AsUint returns the uint representation of the value associated with k or panics if unable to do so.

func AsUint16 added in v1.0.0

func AsUint16(b Bucket, k string) uint16

AsUint16 returns the uint16 representation of the value associated with k or panics if unable to do so.

func AsUint16Between added in v1.0.0

func AsUint16Between(b Bucket, k string, min, max int16) uint16

AsUint16Between returns the uint16 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsUint16Default added in v1.0.0

func AsUint16Default(b Bucket, k string, v uint16) uint16

AsUint16Default returns the uint16 representation of the value associated with k, or the default value v if k is undefined.

func AsUint16DefaultBetween added in v1.0.0

func AsUint16DefaultBetween(b Bucket, k string, v, min, max int16) uint16

AsUint16DefaultBetween returns the uint16 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsUint32 added in v1.0.0

func AsUint32(b Bucket, k string) uint32

AsUint32 returns the uint32 representation of the value associated with k or panics if unable to do so.

func AsUint32Between added in v1.0.0

func AsUint32Between(b Bucket, k string, min, max int32) uint32

AsUint32Between returns the uint32 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsUint32Default added in v1.0.0

func AsUint32Default(b Bucket, k string, v uint32) uint32

AsUint32Default returns the uint32 representation of the value associated with k, or the default value v if k is undefined.

func AsUint32DefaultBetween added in v1.0.0

func AsUint32DefaultBetween(b Bucket, k string, v, min, max int32) uint32

AsUint32DefaultBetween returns the uint32 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsUint64 added in v1.0.0

func AsUint64(b Bucket, k string) uint64

AsUint64 returns the uint64 representation of the value associated with k or panics if unable to do so.

func AsUint64Between added in v1.0.0

func AsUint64Between(b Bucket, k string, min, max uint64) uint64

AsUint64Between returns the uint64 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsUint64Default added in v1.0.0

func AsUint64Default(b Bucket, k string, v uint64) uint64

AsUint64Default returns the uint64 representation of the value associated with k, or the default value v if k is undefined.

func AsUint64DefaultBetween added in v1.0.0

func AsUint64DefaultBetween(b Bucket, k string, v, min, max uint64) uint64

AsUint64DefaultBetween returns the uint64 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsUint8 added in v1.0.0

func AsUint8(b Bucket, k string) uint8

AsUint8 returns the uint8 representation of the value associated with k or panics if unable to do so.

func AsUint8Between added in v1.0.0

func AsUint8Between(b Bucket, k string, min, max int8) uint8

AsUint8Between returns the uint8 representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsUint8Default added in v1.0.0

func AsUint8Default(b Bucket, k string, v uint8) uint8

AsUint8Default returns the uint8 representation of the value associated with k, or the default value v if k is undefined.

func AsUint8DefaultBetween added in v1.0.0

func AsUint8DefaultBetween(b Bucket, k string, v, min, max int8) uint8

AsUint8DefaultBetween returns the uint8 representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func AsUintBetween added in v1.0.0

func AsUintBetween(b Bucket, k string, min, max int) uint

AsUintBetween returns the uint representation of the value associated with k or panics if unable to do so.

It panics if the value is not between min and max (inclusive).

func AsUintDefault added in v1.0.0

func AsUintDefault(b Bucket, k string, v uint) uint

AsUintDefault returns the uint representation of the value associated with k, or the default value v if k is undefined.

func AsUintDefaultBetween added in v1.0.0

func AsUintDefaultBetween(b Bucket, k string, v, min, max int) uint

AsUintDefaultBetween returns the uint representation of the value associated with k, or the default value v if k is undefined.

It panics if the value is not between min and max (inclusive).

func GetEnv

func GetEnv(k string) string

GetEnv returns the value associated with the environment variable named k.

This function is intended as a "drop-in" replacement for os.Getenv() in legacy codebases.

In new codebases it is preferable to accept a Bucket as a dependency. The bucket returned by Environment() can be used to satisfy this dependency.

Example
package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the path to
	// a configuration file, and the EXAMPLE__DATASOURCE specifies the source
	// type as "file".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", "./testdata/example.json")
	os.Setenv("EXAMPLE__DATASOURCE", "file")

	fmt.Println(
		config.GetEnv("EXAMPLE"),
	)

}
Output:

{"example_config": true}

Types

type Bucket

type Bucket interface {
	// Get returns the value associated with the given key.
	//
	// If they key is not defined, it returns a zero-value.
	Get(k string) Value

	// GetDefault returns the value associated with the given key.
	//
	// If the key is not defined, it returns a value with the content of v.
	GetDefault(k string, v string) Value

	// Each calls fn for each key/value pair in the bucket.
	//
	// If fn returns false, iteration is stopped.
	//
	// Each returns true if iteration completes fully, or false if fn()
	// returns false.
	Each(fn EachFunc) bool
}

Bucket is a container for configuration key/value pairs.

func Environment

func Environment() Bucket

Environment returns a Bucket that produces configuration values from the operating system's environment variables.

For any given environment variable K, the environment variable K__DATASOURCE indicates how the content of K should be interpreted.

If K__DATASOURCE is:

● empty, undefined or the value "string:plain", then K is a regular variable

● the value "string:hex", then K contains a binary value with hexadecimal encoding

● the value "string:base64", then K contains a binary value with base-64 encoding

● the value "file", then K contains a path to a file containing the value

Example (Each)
package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	os.Setenv("FOO", "<foo>")
	os.Setenv("BAR", "<bar>")

	config.Environment().Each(
		func(k string, v config.Value) bool {
			if k == "FOO" || k == "BAR" {
				fmt.Printf("%s=%s\n", k, v)
			}

			return true
		},
	)

}
Output:

FOO=<foo>
BAR=<bar>
Example (Get)
package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	os.Setenv("FOO", "<foo>")

	v := config.Environment().Get("FOO")

	fmt.Println(v)

}
Output:

<foo>
Example (GetDefault)
package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	os.Setenv("FOO", "<foo>")

	v := config.Environment().GetDefault("FOO", "<default>")

	fmt.Println(v)

}
Output:

<foo>
Example (GetDefaultWithUndefinedVariable)
package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	os.Setenv("FOO", "")

	v := config.Environment().GetDefault("FOO", "<default>")

	fmt.Println(v)

}
Output:

<default>
Example (GetWithUndefinedVariable)
package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	os.Setenv("FOO", "")

	v := config.Environment().Get("FOO")

	fmt.Println(v.IsZero())

}
Output:

true

type EachFunc

type EachFunc func(k string, v Value) bool

EachFunc is a function used to visit the key/value pairs in a bucket using Bucket.Each().

type InvalidDefaultValue added in v1.2.0

type InvalidDefaultValue struct {
	Key          string
	DefaultValue string
	Explanation  string
}

InvalidDefaultValue is an error used as a panic value when the default value associated with a key is not well-formed or is otherwise invalid.

func (InvalidDefaultValue) ConfigKey added in v1.2.0

func (e InvalidDefaultValue) ConfigKey() string

ConfigKey returns the config key that the error relates to.

func (InvalidDefaultValue) Error added in v1.2.0

func (e InvalidDefaultValue) Error() string

type InvalidValue added in v1.2.0

type InvalidValue struct {
	Key         string
	Value       string
	Explanation string
}

InvalidValue is an error used as a panic value when the value associated with a key is not well-formed or is otherwise invalid.

func (InvalidValue) ConfigKey added in v1.2.0

func (e InvalidValue) ConfigKey() string

ConfigKey returns the config key that the error relates to.

func (InvalidValue) Error added in v1.2.0

func (e InvalidValue) Error() string

type KeyError added in v1.2.0

type KeyError interface {
	error

	// ConfigKey returns the config key that the error relates to.
	ConfigKey() string
}

KeyError is an error that indicates a problem with the value associated with a specific key.

type Map added in v1.0.0

type Map map[string]Value

Map is an in-memory implementation of Bucket.

func (Map) Each added in v1.0.0

func (m Map) Each(fn EachFunc) bool

Each calls fn for each key/value pair in the bucket.

If fn returns false, iteration is stopped.

Each returns true if iteration completes fully, or false if fn() returns false.

func (Map) Get added in v1.0.0

func (m Map) Get(k string) Value

Get returns the value associated with the given key.

If they key is not defined, it returns a zero-value.

func (Map) GetDefault added in v1.0.0

func (m Map) GetDefault(k string, v string) Value

GetDefault returns the value associated with the given key.

If the key is not defined, it returns a value with the content of v.

type NotDefined added in v1.2.0

type NotDefined struct {
	Key string
}

NotDefined is an error used as a panic value when a requested key is not defined.

func (NotDefined) ConfigKey added in v1.2.0

func (e NotDefined) ConfigKey() string

ConfigKey returns the config key that the error relates to.

func (NotDefined) Error added in v1.2.0

func (e NotDefined) Error() string

type Value

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

Value is a configuration value.

func Bytes

func Bytes(v []byte) Value

Bytes returns a configuration value that is specified as a byte-slice.

func File

func File(p string) Value

File returns a configuration value that is specified as a path to a file.

func String

func String(v string) Value

String returns a configuration value that is specified as a string.

func (Value) AsBytes

func (v Value) AsBytes() ([]byte, error)

AsBytes returns the configuration value as a byte-slice.

If v is the zero-value, it returns a nil slice.

Example (SpecifiedAsBase64)

This example demonstrates how to specify a configuration value as a base-64 encoded string and consume it as a byte-slice.

package main

import (
	"encoding/base64"
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the
	// configuration value as a hexadecimal string, and the EXAMPLE__DATASOURCE
	// specifies the source type as "string:base64".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", base64.StdEncoding.EncodeToString([]byte("<the configuration value>")))
	os.Setenv("EXAMPLE__DATASOURCE", "string:base64")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Get the configuration value as a byte-slice.
	buf, err := value.AsBytes()
	if err != nil {
		panic(err)
	}

	// Print the value as a string.
	fmt.Println(string(buf))

}
Output:

<the configuration value>
Example (SpecifiedAsFile)

This example demonstrates how to specify a configuration value as a file and consume it as a byte-slice.

package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the path to
	// a configuration file, and the EXAMPLE__DATASOURCE specifies the source
	// type as "file".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", "./testdata/example.json")
	os.Setenv("EXAMPLE__DATASOURCE", "file")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Get the configuration value as a byte-slice.
	buf, err := value.AsBytes()
	if err != nil {
		panic(err)
	}

	// Print the value as a string.
	fmt.Println(string(buf))

}
Output:

{"example_config": true}
Example (SpecifiedAsHex)

This example demonstrates how to specify a configuration value as a hexadecimal string and consume it as a byte-slice.

package main

import (
	"encoding/hex"
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the
	// configuration value as a hexadecimal string, and the EXAMPLE__DATASOURCE
	// specifies the source type as "string:hex".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", hex.EncodeToString([]byte("<the configuration value>")))
	os.Setenv("EXAMPLE__DATASOURCE", "string:hex")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Get the configuration value as a byte-slice.
	buf, err := value.AsBytes()
	if err != nil {
		panic(err)
	}

	// Print the value as a string.
	fmt.Println(string(buf))

}
Output:

<the configuration value>
Example (SpecifiedAsString)

This example demonstrates how to specify a configuration value as a string and consume it as a byte-slice.

// Setup the environment such that the EXAMPLE variable contains the
// configuration value as a string, and the EXAMPLE__DATASOURCE specifies
// the source type as "string:plain".
//
// This is the default source type. EXAMPLE__DATASOURCE can be left
// undefined and the behavior would be the same.
//
// Outside of an example these environment variables would be set in the
// operating system shell, and the path would refer to a real configuration
// file.
os.Setenv("EXAMPLE", "<the configuration value>")
os.Setenv("EXAMPLE__DATASOURCE", "string:plain")

// Get the configuration value from the Environment bucket.
value := config.Environment().Get("EXAMPLE")

// Get the configuration value as a byte-slice.
buf, err := value.AsBytes()
if err != nil {
	panic(err)
}

// Print the value as a string.
fmt.Println(string(buf))
Output:

<the configuration value>

func (Value) AsPath

func (v Value) AsPath() (string, io.Closer, error)

AsPath returns the path to a real file on disk that contains the configuration value.

If the configuration value was originally specified as a file, this will be the path to the original file. Otherwise, the path may be to a temporary file.

This method should be used when some existing code requires a path to a file. Otherwise, it is preferable to use AsReader(), AsString() or AsBytes().

It returns an io.Closer that must be closed when the file is no longer needed, regardless of how the configuration was specified.

If v is the zero-value, it returns an os.ErrNotExist error.

Example (SpecifiedAsBase64)

This example demonstrates how to specify a configuration value as a base-64 encoded string and consume it as a path to a temporary file containing the string's content.

package main

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the
	// configuration value as a hexadecimal string, and the EXAMPLE__DATASOURCE
	// specifies the source type as "string:base64".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", base64.StdEncoding.EncodeToString([]byte("<the configuration value>")))
	os.Setenv("EXAMPLE__DATASOURCE", "string:base64")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Obtain the path to the configuration file.
	//
	// Because the configuration value is specified as a string, Path() returns
	// the path to a temporary file.
	p, c, err := value.AsPath()
	if err != nil {
		panic(err)
	}
	defer c.Close()

	// Read the temporary file into a buffer.
	buf, err := ioutil.ReadFile(p)
	if err != nil {
		panic(err)
	}

	// Print the value as a string.
	fmt.Println(string(buf))

}
Output:

<the configuration value>
Example (SpecifiedAsFile)

This example demonstrates how to specify a configuration value as a file and consume it as a path to that file.

package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the path to
	// a configuration file, and the EXAMPLE__DATASOURCE specifies the source
	// type as "file".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", "./testdata/example.json")
	os.Setenv("EXAMPLE__DATASOURCE", "file")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Obtain the path to the configuration file.
	//
	// Because the configuration value is specified as a file, Path() returns
	// the path to the original file. If the configuration value were specified
	// by some other means, this would be a path to a temporary file.
	p, c, err := value.AsPath()
	if err != nil {
		panic(err)
	}
	defer c.Close()

	// Print the path.
	fmt.Println(p)

}
Output:

./testdata/example.json
Example (SpecifiedAsHex)

This example demonstrates how to specify a configuration value as a hexadecimal string and consume it as a path to a temporary file containing the string's content.

package main

import (
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the
	// configuration value as a hexadecimal string, and the EXAMPLE__DATASOURCE
	// specifies the source type as "string:hex".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", hex.EncodeToString([]byte("<the configuration value>")))
	os.Setenv("EXAMPLE__DATASOURCE", "string:hex")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Obtain the path to the configuration file.
	//
	// Because the configuration value is specified as a string, Path() returns
	// the path to a temporary file.
	p, c, err := value.AsPath()
	if err != nil {
		panic(err)
	}
	defer c.Close()

	// Read the temporary file into a buffer.
	buf, err := ioutil.ReadFile(p)
	if err != nil {
		panic(err)
	}

	// Print the value as a string.
	fmt.Println(string(buf))

}
Output:

<the configuration value>
Example (SpecifiedAsString)

This example demonstrates how to specify a configuration value as a string and consume it as a path to a temporary file containing the string's content.

// Setup the environment such that the EXAMPLE variable contains the
// configuration value as a string, and the EXAMPLE__DATASOURCE specifies
// the source type as "string:plain".
//
// This is the default source type. EXAMPLE__DATASOURCE can be left
// undefined and the behavior would be the same.
//
// Outside of an example these environment variables would be set in the
// operating system shell, and the path would refer to a real configuration
// file.
os.Setenv("EXAMPLE", "<the configuration value>")
os.Setenv("EXAMPLE__DATASOURCE", "string:plain")

// Get the configuration value from the Environment bucket.
value := config.Environment().Get("EXAMPLE")

// Obtain the path to the configuration file.
//
// Because the configuration value is specified as a string, Path() returns
// the path to a temporary file.
p, c, err := value.AsPath()
if err != nil {
	panic(err)
}
defer c.Close()

// Read the temporary file into a buffer.
buf, err := ioutil.ReadFile(p)
if err != nil {
	panic(err)
}

// Print the value as a string.
fmt.Println(string(buf))
Output:

<the configuration value>

func (Value) AsReader

func (v Value) AsReader() (io.ReadCloser, error)

AsReader returns an io.ReadCloser that produces the configuration value.

If v is the zero-value, it returns an os.ErrNotExist error.

Example (SpecifiedAsBase64)

This example demonstrates how to specify a configuration value as a base-64 encoded string and consume it using an io.ReadCloser.

package main

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the
	// configuration value as a hexadecimal string, and the EXAMPLE__DATASOURCE
	// specifies the source type as "string:base64".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", base64.StdEncoding.EncodeToString([]byte("<the configuration value>")))
	os.Setenv("EXAMPLE__DATASOURCE", "string:base64")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Consume the value as an io.ReadCloser.
	r, err := value.AsReader()
	if err != nil {
		panic(err)
	}
	defer r.Close()

	// Read the entire value into a buffer.
	buf, err := ioutil.ReadAll(r)
	if err != nil {
		panic(err)
	}

	// Print the buffer.
	fmt.Println(string(buf))

}
Output:

<the configuration value>
Example (SpecifiedAsFile)

This example demonstrates how to specify a configuration value as a file and consume it using an io.ReadCloser.

package main

import (
	"fmt"
	"io/ioutil"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the path to
	// a configuration file, and the EXAMPLE__DATASOURCE specifies the source
	// type as "file".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", "./testdata/example.json")
	os.Setenv("EXAMPLE__DATASOURCE", "file")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Consume the value as an io.ReadCloser.
	r, err := value.AsReader()
	if err != nil {
		panic(err)
	}
	defer r.Close()

	// Read the entire value into a buffer.
	buf, err := ioutil.ReadAll(r)
	if err != nil {
		panic(err)
	}

	// Print the buffer.
	fmt.Println(string(buf))

}
Output:

{"example_config": true}
Example (SpecifiedAsHex)

This example demonstrates how to specify a configuration value as a hexadecimal string and consume it using an io.ReadCloser.

package main

import (
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the
	// configuration value as a hexadecimal string, and the EXAMPLE__DATASOURCE
	// specifies the source type as "string:hex".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", hex.EncodeToString([]byte("<the configuration value>")))
	os.Setenv("EXAMPLE__DATASOURCE", "string:hex")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Consume the value as an io.ReadCloser.
	r, err := value.AsReader()
	if err != nil {
		panic(err)
	}
	defer r.Close()

	// Read the entire value into a buffer.
	buf, err := ioutil.ReadAll(r)
	if err != nil {
		panic(err)
	}

	// Print the buffer.
	fmt.Println(string(buf))

}
Output:

<the configuration value>
Example (SpecifiedAsString)

This example demonstrates how to specify a configuration value as a string and consume it using an io.ReadCloser.

// Setup the environment such that the EXAMPLE variable contains the
// configuration value as a string, and the EXAMPLE__DATASOURCE specifies
// the source type as "string:plain".
//
// This is the default source type. EXAMPLE__DATASOURCE can be left
// undefined and the behavior would be the same.
//
// Outside of an example these environment variables would be set in the
// operating system shell, and the path would refer to a real configuration
// file.
os.Setenv("EXAMPLE", "<the configuration value>")
os.Setenv("EXAMPLE__DATASOURCE", "string:plain")

// Get the configuration value from the Environment bucket.
value := config.Environment().Get("EXAMPLE")

// Consume the value as an io.ReadCloser.
r, err := value.AsReader()
if err != nil {
	panic(err)
}
defer r.Close()

// Read the entire value into a buffer.
buf, err := ioutil.ReadAll(r)
if err != nil {
	panic(err)
}

// Print the buffer.
fmt.Println(string(buf))
Output:

<the configuration value>

func (Value) AsString

func (v Value) AsString() (string, error)

AsString returns the configuration value as a string.

It returns an error v is the zero-value.

Example (SpecifiedAsBase64)

This example demonstrates how to specify a configuration value as a base-64 encoded string and consume it as string.

package main

import (
	"encoding/base64"
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the
	// configuration value as a hexadecimal string, and the EXAMPLE__DATASOURCE
	// specifies the source type as "string:base64".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", base64.StdEncoding.EncodeToString([]byte("<the configuration value>")))
	os.Setenv("EXAMPLE__DATASOURCE", "string:base64")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Get the configuration value as a string.
	str, err := value.AsString()
	if err != nil {
		panic(err)
	}

	// Print the value.
	fmt.Println(str)

}
Output:

<the configuration value>
Example (SpecifiedAsFile)

This example demonstrates how to specify a configuration value as a file and consume it as string.

package main

import (
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the path to
	// a configuration file, and the EXAMPLE__DATASOURCE specifies the source
	// type as "file".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", "./testdata/example.json")
	os.Setenv("EXAMPLE__DATASOURCE", "file")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Get the configuration value as a string.
	str, err := value.AsString()
	if err != nil {
		panic(err)
	}

	// Print the value.
	fmt.Println(str)

}
Output:

{"example_config": true}
Example (SpecifiedAsHex)

This example demonstrates how to specify a configuration value as a hexadecimal string and consume it as string.

package main

import (
	"encoding/hex"
	"fmt"
	"os"

	"github.com/dogmatiq/dodeca/config"
)

func main() {
	// Setup the environment such that the EXAMPLE variable contains the
	// configuration value as a hexadecimal string, and the EXAMPLE__DATASOURCE
	// specifies the source type as "string:hex".
	//
	// Outside of an example these environment variables would be set in the
	// operating system shell, and the path would refer to a real configuration
	// file.
	os.Setenv("EXAMPLE", hex.EncodeToString([]byte("<the configuration value>")))
	os.Setenv("EXAMPLE__DATASOURCE", "string:hex")

	// Get the configuration value from the Environment bucket.
	value := config.Environment().Get("EXAMPLE")

	// Get the configuration value as a string.
	str, err := value.AsString()
	if err != nil {
		panic(err)
	}

	// Print the value.
	fmt.Println(str)

}
Output:

<the configuration value>
Example (SpecifiedAsString)

This example demonstrates how to specify a configuration value as a string and consume it as string.

// Setup the environment such that the EXAMPLE variable contains the
// configuration value as a string, and the EXAMPLE__DATASOURCE specifies
// the source type as "string:plain".
//
// This is the default source type. EXAMPLE__DATASOURCE can be left
// undefined and the behavior would be the same.
//
// Outside of an example these environment variables would be set in the
// operating system shell, and the path would refer to a real configuration
// file.
os.Setenv("EXAMPLE", "<the configuration value>")
os.Setenv("EXAMPLE__DATASOURCE", "string:plain")

// Get the configuration value from the Environment bucket.
value := config.Environment().Get("EXAMPLE")

// Get the configuration value as a string.
str, err := value.AsString()
if err != nil {
	panic(err)
}

// Print the value.
fmt.Println(str)
Output:

<the configuration value>

func (Value) Bytes added in v0.2.0

func (v Value) Bytes() []byte

Bytes returns the value as a byte-slice, or panics if unable to do so.

func (*Value) IsZero added in v0.2.0

func (v *Value) IsZero() bool

IsZero returns true if this value is the zero-value.

This means that the value has no data-source, and not necessarily that the data source will produce an empty value.

func (Value) String

func (v Value) String() string

String returns the value as a string, or panics if unable to do so.

Jump to

Keyboard shortcuts

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