Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
d1bcc8c4cc
|
|||
afa50b4fca
|
|||
09acd79fe8
|
|||
0f1586393b
|
|||
0d49b3e716
|
|||
f27c08ef84
|
|||
712f3b65cf
|
|||
7b77ef35af
|
@ -14,6 +14,7 @@ const (
|
||||
TypeString DataType = iota
|
||||
TypeDirectory DataType = iota
|
||||
TypeBool DataType = iota
|
||||
TypeHex DataType = iota
|
||||
)
|
||||
|
||||
type cValue struct {
|
||||
@ -21,15 +22,18 @@ type cValue struct {
|
||||
intval int64
|
||||
durval time.Duration
|
||||
boolval bool
|
||||
binval []byte
|
||||
strval string
|
||||
mapval map[string]cValue
|
||||
err error
|
||||
}
|
||||
|
||||
func (dtype DataType) parse(key string, str string) (ret cValue) {
|
||||
info, ok := tInfo[dtype]
|
||||
if ok {
|
||||
return info.parser(key, str)
|
||||
ret = info.parser(key, str)
|
||||
if len(ret.binval) == 0 {
|
||||
ret.binval = make([]byte, 0, 0)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
93
envconf.go
93
envconf.go
@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
@ -38,14 +39,7 @@ func NewConfig() *Config {
|
||||
splitted := strings.SplitN(v, "=", 2)
|
||||
if len(splitted) == 2 {
|
||||
key := cleanKey(splitted[0])
|
||||
keysplit := strings.Split(key, "_")
|
||||
left := ""
|
||||
right := ""
|
||||
|
||||
if len(keysplit) > 1 {
|
||||
left = strings.Join(keysplit[:len(keysplit)-1], "_")
|
||||
right = keysplit[len(keysplit)-1]
|
||||
}
|
||||
left, right, mappable := keySplit(key)
|
||||
|
||||
if unicode.IsLetter(getFirstRune(key)) {
|
||||
var entry cEntry
|
||||
@ -54,7 +48,13 @@ func NewConfig() *Config {
|
||||
entry.unset = false
|
||||
entry.empty = false
|
||||
config.env[key] = entry
|
||||
config.mapEnv[left][right] = entry
|
||||
if mappable {
|
||||
if len(config.mapEnv[left]) == 0 {
|
||||
config.mapEnv[left] = make(map[string]cEntry)
|
||||
}
|
||||
|
||||
config.mapEnv[left][right] = entry
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -64,25 +64,25 @@ func NewConfig() *Config {
|
||||
// Define the type of an environment variable.
|
||||
// Variables without a defined type will be ignored by Parse.
|
||||
func (c *Config) Define(key string, dtype DataType) {
|
||||
upper := strings.ToUpper(key)
|
||||
entry, ok := c.env[upper]
|
||||
key = cleanKey(key)
|
||||
entry, ok := c.env[key]
|
||||
if ok {
|
||||
entry.dtype = dtype
|
||||
c.env[upper] = entry
|
||||
c.env[key] = entry
|
||||
} else {
|
||||
var entry cEntry
|
||||
entry.dtype = dtype
|
||||
entry.unset = true
|
||||
entry.empty = true
|
||||
c.env[upper] = entry
|
||||
c.env[key] = entry
|
||||
}
|
||||
}
|
||||
|
||||
// Define the type of an environment variable.
|
||||
// Variables without a defined type will be ignored by Parse.
|
||||
func (c *Config) DefineMap(key string, dtype DataType) {
|
||||
upper := strings.ToUpper(key)
|
||||
entries, ok := c.mapEnv[upper]
|
||||
key = cleanKey(key)
|
||||
entries, ok := c.mapEnv[key]
|
||||
if ok {
|
||||
for mapKey, entry := range entries {
|
||||
entry.dtype = dtype
|
||||
@ -94,8 +94,8 @@ func (c *Config) DefineMap(key string, dtype DataType) {
|
||||
// Define the type and default value of an environment variable.
|
||||
// Variables without a defined type will be ignored by Parse.
|
||||
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
||||
upper := strings.ToUpper(key)
|
||||
entry, ok := c.env[upper]
|
||||
key = cleanKey(key)
|
||||
entry, ok := c.env[key]
|
||||
if ok {
|
||||
if entry.unset {
|
||||
entry.value = val
|
||||
@ -104,7 +104,7 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
||||
entry.empty = false
|
||||
entry.defval = val
|
||||
entry.hasdef = true
|
||||
c.env[upper] = entry
|
||||
c.env[key] = entry
|
||||
} else {
|
||||
var entry cEntry
|
||||
entry.dtype = dtype
|
||||
@ -113,7 +113,7 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
||||
entry.value = val
|
||||
entry.defval = val
|
||||
entry.hasdef = true
|
||||
c.env[upper] = entry
|
||||
c.env[key] = entry
|
||||
}
|
||||
}
|
||||
|
||||
@ -258,7 +258,8 @@ func (c *Config) Status() (ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func cleanKey(str string) string {
|
||||
func cleanKey(key string) string {
|
||||
expr := regexp.MustCompile("__+")
|
||||
fn := func(r rune) rune {
|
||||
if (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || r == '_' {
|
||||
return r
|
||||
@ -266,24 +267,25 @@ func cleanKey(str string) string {
|
||||
return -1
|
||||
}
|
||||
|
||||
str = strings.Map(fn, strings.ToUpper(str))
|
||||
key = strings.Trim(strings.Map(fn, strings.ToUpper(key)), "_")
|
||||
return expr.ReplaceAllString(key, "_")
|
||||
}
|
||||
|
||||
oldlen := len(str) + 1
|
||||
newlen := len(str)
|
||||
for newlen < oldlen {
|
||||
oldlen = newlen
|
||||
str = strings.ReplaceAll(str, "__", "_")
|
||||
newlen = len(str)
|
||||
func keySplit(key string) (left string, right string, ok bool) {
|
||||
key = cleanKey(key)
|
||||
pos := strings.LastIndex(key, "_")
|
||||
ok = false
|
||||
if (pos+1 < len(key)) && (pos > 1) {
|
||||
return cleanKey(key[:pos]), cleanKey(key[pos:]), true
|
||||
}
|
||||
|
||||
return str
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
|
||||
val.dtype = TypeNone
|
||||
if c.parsed {
|
||||
upper := strings.ToUpper(key)
|
||||
entry, ok := c.env[upper]
|
||||
key = cleanKey(key)
|
||||
entry, ok := c.env[key]
|
||||
if ok && (entry.dtype == dtype) {
|
||||
return entry.parsed
|
||||
}
|
||||
@ -300,7 +302,7 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
|
||||
|
||||
if ok {
|
||||
for k, v := range entries {
|
||||
if v.dtype == dtype {
|
||||
if (v.dtype == dtype) && (v.parsed.err == nil) {
|
||||
retval[k] = v.parsed
|
||||
} else {
|
||||
return
|
||||
@ -312,21 +314,28 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
|
||||
return
|
||||
}
|
||||
|
||||
// GetInt returns the value of an environment variable.
|
||||
// Returns the value of an environment variable.
|
||||
// If the variable is not defined as envconf.TypeInt the function will return 0.
|
||||
func (c *Config) GetInt(key string) int64 {
|
||||
val := c.getRaw(key, TypeInt)
|
||||
return val.intval
|
||||
}
|
||||
|
||||
// GetMetric returns the value of an environment variable.
|
||||
// Returns the value of an environment variable.
|
||||
// If the variable is not defined as envconf.TypeHex the function will return []byte{}.
|
||||
func (c *Config) GetHex(key string) []byte {
|
||||
val := c.getRaw(key, TypeHex)
|
||||
return val.binval
|
||||
}
|
||||
|
||||
// Returns the value of an environment variable.
|
||||
// If the variable is not defined as envconf.TypeMetric the function will return 0.
|
||||
func (c *Config) GetMetric(key string) int64 {
|
||||
val := c.getRaw(key, TypeMetric)
|
||||
return val.intval
|
||||
}
|
||||
|
||||
// GetDirectory returns the value of an environment variable.
|
||||
// Returns the value of an environment variable.
|
||||
// If the variable is not defined as envconf.TypeDirectory the
|
||||
// function will return the empty string.
|
||||
func (c *Config) GetDirectory(key string) string {
|
||||
@ -334,7 +343,7 @@ func (c *Config) GetDirectory(key string) string {
|
||||
return val.strval
|
||||
}
|
||||
|
||||
// GetString returns the value of an environment variable.
|
||||
// Returns the value of an environment variable.
|
||||
// If the variable is not defined as envconf.TypeString the
|
||||
// function will return the empty string.
|
||||
func (c *Config) GetString(key string) string {
|
||||
@ -342,7 +351,7 @@ func (c *Config) GetString(key string) string {
|
||||
return val.strval
|
||||
}
|
||||
|
||||
// GetDuration returns the value of an environment variable.
|
||||
// Returns the value of an environment variable.
|
||||
// If the variable is not defined as envconf.TypeDuration the
|
||||
// function will return time.Duration(0).
|
||||
func (c *Config) GetDuration(key string) time.Duration {
|
||||
@ -350,7 +359,7 @@ func (c *Config) GetDuration(key string) time.Duration {
|
||||
return val.durval
|
||||
}
|
||||
|
||||
// GetBool returns the value of an environment variable.
|
||||
// Returns the value of an environment variable.
|
||||
// If the variable is not defined as envconf.TypeBool the
|
||||
// function will return false.
|
||||
func (c *Config) GetBool(key string) bool {
|
||||
@ -390,6 +399,14 @@ func (c *Config) GetMapBool(key string) (retval map[string]bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Config) GetMapHex(key string) (retval map[string][]byte) {
|
||||
retval = make(map[string][]byte)
|
||||
for k, v := range c.getRawMap(key, TypeHex) {
|
||||
retval[k] = v.binval
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getFirstRune(str string) rune {
|
||||
for _, v := range str {
|
||||
return v
|
||||
|
11
parsers.go
11
parsers.go
@ -1,6 +1,7 @@
|
||||
package envconf
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -65,6 +66,16 @@ func parseBool(key string, str string) (ret cValue) {
|
||||
return
|
||||
}
|
||||
|
||||
func parseHex(key string, str string) (ret cValue) {
|
||||
val, err := hex.DecodeString(str)
|
||||
if err == nil {
|
||||
ret.binval = val
|
||||
} else {
|
||||
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func parseDirectory(_ string, str string) (ret cValue) {
|
||||
wd, err := os.Getwd()
|
||||
if err == nil {
|
||||
|
@ -14,6 +14,7 @@ func init() {
|
||||
var durInfo dataTypeInfo
|
||||
var strInfo dataTypeInfo
|
||||
var dirInfo dataTypeInfo
|
||||
var hexInfo dataTypeInfo
|
||||
var boolInfo dataTypeInfo
|
||||
|
||||
intInfo.name = "int"
|
||||
@ -21,6 +22,7 @@ func init() {
|
||||
durInfo.name = "duration"
|
||||
dirInfo.name = "directory"
|
||||
strInfo.name = "string"
|
||||
hexInfo.name = "hex"
|
||||
boolInfo.name = "bool"
|
||||
|
||||
intInfo.parser = parseInt
|
||||
@ -28,6 +30,7 @@ func init() {
|
||||
durInfo.parser = parseDuration
|
||||
dirInfo.parser = parseDirectory
|
||||
strInfo.parser = parseString
|
||||
hexInfo.parser = parseHex
|
||||
boolInfo.parser = parseBool
|
||||
|
||||
tInfo[TypeInt] = intInfo
|
||||
@ -35,5 +38,6 @@ func init() {
|
||||
tInfo[TypeDuration] = durInfo
|
||||
tInfo[TypeString] = strInfo
|
||||
tInfo[TypeDirectory] = dirInfo
|
||||
tInfo[TypeHex] = hexInfo
|
||||
tInfo[TypeBool] = boolInfo
|
||||
}
|
||||
|
Reference in New Issue
Block a user