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 ¶
- Constants
- func AsBool(b Bucket, k string) bool
- func AsBoolDefault(b Bucket, k string, v bool) bool
- func AsBoolF(b Bucket, k string) bool
- func AsBoolT(b Bucket, k string) bool
- func AsBytes(b Bucket, k string) []byte
- func AsBytesDefault(b Bucket, k string, v []byte) []byte
- func AsDuration(b Bucket, k string) time.Duration
- func AsDurationBetween(b Bucket, k string, min, max time.Duration) time.Duration
- func AsDurationDefault(b Bucket, k string, v time.Duration) time.Duration
- func AsDurationDefaultBetween(b Bucket, k string, v, min, max time.Duration) time.Duration
- func AsFloat32(b Bucket, k string) float32
- func AsFloat32Between(b Bucket, k string, min, max float32) float32
- func AsFloat32Default(b Bucket, k string, v float32) float32
- func AsFloat32DefaultBetween(b Bucket, k string, v, min, max float32) float32
- func AsFloat64(b Bucket, k string) float64
- func AsFloat64Between(b Bucket, k string, min, max float64) float64
- func AsFloat64Default(b Bucket, k string, v float64) float64
- func AsFloat64DefaultBetween(b Bucket, k string, v, min, max float64) float64
- func AsInt(b Bucket, k string) int
- func AsInt16(b Bucket, k string) int16
- func AsInt16Between(b Bucket, k string, min, max int16) int16
- func AsInt16Default(b Bucket, k string, v int16) int16
- func AsInt16DefaultBetween(b Bucket, k string, v, min, max int16) int16
- func AsInt32(b Bucket, k string) int32
- func AsInt32Between(b Bucket, k string, min, max int32) int32
- func AsInt32Default(b Bucket, k string, v int32) int32
- func AsInt32DefaultBetween(b Bucket, k string, v, min, max int32) int32
- func AsInt64(b Bucket, k string) int64
- func AsInt64Between(b Bucket, k string, min, max int64) int64
- func AsInt64Default(b Bucket, k string, v int64) int64
- func AsInt64DefaultBetween(b Bucket, k string, v, min, max int64) int64
- func AsInt8(b Bucket, k string) int8
- func AsInt8Between(b Bucket, k string, min, max int8) int8
- func AsInt8Default(b Bucket, k string, v int8) int8
- func AsInt8DefaultBetween(b Bucket, k string, v, min, max int8) int8
- func AsIntBetween(b Bucket, k string, min, max int) int
- func AsIntDefault(b Bucket, k string, v int) int
- func AsIntDefaultBetween(b Bucket, k string, v, min, max int) int
- func AsString(b Bucket, k string) string
- func AsStringDefault(b Bucket, k string, v string) string
- func AsURL(b Bucket, k string) *url.URL
- func AsURLDefault(b Bucket, k, v string) *url.URL
- func AsUint(b Bucket, k string) uint
- func AsUint16(b Bucket, k string) uint16
- func AsUint16Between(b Bucket, k string, min, max int16) uint16
- func AsUint16Default(b Bucket, k string, v uint16) uint16
- func AsUint16DefaultBetween(b Bucket, k string, v, min, max int16) uint16
- func AsUint32(b Bucket, k string) uint32
- func AsUint32Between(b Bucket, k string, min, max int32) uint32
- func AsUint32Default(b Bucket, k string, v uint32) uint32
- func AsUint32DefaultBetween(b Bucket, k string, v, min, max int32) uint32
- func AsUint64(b Bucket, k string) uint64
- func AsUint64Between(b Bucket, k string, min, max uint64) uint64
- func AsUint64Default(b Bucket, k string, v uint64) uint64
- func AsUint64DefaultBetween(b Bucket, k string, v, min, max uint64) uint64
- func AsUint8(b Bucket, k string) uint8
- func AsUint8Between(b Bucket, k string, min, max int8) uint8
- func AsUint8Default(b Bucket, k string, v uint8) uint8
- func AsUint8DefaultBetween(b Bucket, k string, v, min, max int8) uint8
- func AsUintBetween(b Bucket, k string, min, max int) uint
- func AsUintDefault(b Bucket, k string, v uint) uint
- func AsUintDefaultBetween(b Bucket, k string, v, min, max int) uint
- func GetEnv(k string) string
- type Bucket
- type EachFunc
- type InvalidDefaultValue
- type InvalidValue
- type KeyError
- type Map
- type NotDefined
- type Value
Examples ¶
- Environment (Each)
- Environment (Get)
- Environment (GetDefault)
- Environment (GetDefaultWithUndefinedVariable)
- Environment (GetWithUndefinedVariable)
- GetEnv
- Value.AsBytes (SpecifiedAsBase64)
- Value.AsBytes (SpecifiedAsFile)
- Value.AsBytes (SpecifiedAsHex)
- Value.AsBytes (SpecifiedAsString)
- Value.AsPath (SpecifiedAsBase64)
- Value.AsPath (SpecifiedAsFile)
- Value.AsPath (SpecifiedAsHex)
- Value.AsPath (SpecifiedAsString)
- Value.AsReader (SpecifiedAsBase64)
- Value.AsReader (SpecifiedAsFile)
- Value.AsReader (SpecifiedAsHex)
- Value.AsReader (SpecifiedAsString)
- Value.AsString (SpecifiedAsBase64)
- Value.AsString (SpecifiedAsFile)
- Value.AsString (SpecifiedAsHex)
- Value.AsString (SpecifiedAsString)
Constants ¶
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 )
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 ¶
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 ¶
EachFunc is a function used to visit the key/value pairs in a bucket using Bucket.Each().
type InvalidDefaultValue ¶ added in v1.2.0
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
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
Map is an in-memory implementation of Bucket.
func (Map) Each ¶ added in v1.0.0
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.
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 (Value) AsBytes ¶
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 ¶
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 ¶
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
Bytes returns the value as a byte-slice, or panics if unable to do so.