rewrite cleanKey
This commit is contained in:
parent
712f3b65cf
commit
f27c08ef84
@ -22,7 +22,6 @@ type cValue struct {
|
|||||||
durval time.Duration
|
durval time.Duration
|
||||||
boolval bool
|
boolval bool
|
||||||
strval string
|
strval string
|
||||||
mapval map[string]cValue
|
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
63
envconf.go
63
envconf.go
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
@ -38,26 +39,22 @@ func NewConfig() *Config {
|
|||||||
splitted := strings.SplitN(v, "=", 2)
|
splitted := strings.SplitN(v, "=", 2)
|
||||||
if len(splitted) == 2 {
|
if len(splitted) == 2 {
|
||||||
key := cleanKey(splitted[0])
|
key := cleanKey(splitted[0])
|
||||||
keysplit := strings.Split(key, "_")
|
left, right, mappable := keySplit(key)
|
||||||
left := ""
|
|
||||||
right := ""
|
|
||||||
|
|
||||||
if len(keysplit) > 1 {
|
|
||||||
left = strings.Join(keysplit[:len(keysplit)-1], "_")
|
|
||||||
right = keysplit[len(keysplit)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
if unicode.IsLetter(getFirstRune(key)) {
|
if unicode.IsLetter(getFirstRune(key)) {
|
||||||
if len(config.mapEnv[left]) == 0 {
|
|
||||||
config.mapEnv[left] = make(map[string]cEntry)
|
|
||||||
}
|
|
||||||
var entry cEntry
|
var entry cEntry
|
||||||
entry.value = splitted[1]
|
entry.value = splitted[1]
|
||||||
entry.dtype = TypeNone
|
entry.dtype = TypeNone
|
||||||
entry.unset = false
|
entry.unset = false
|
||||||
entry.empty = false
|
entry.empty = false
|
||||||
config.env[key] = entry
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,17 +64,17 @@ func NewConfig() *Config {
|
|||||||
// Define the type of an environment variable.
|
// Define the type of an environment variable.
|
||||||
// Variables without a defined type will be ignored by Parse.
|
// Variables without a defined type will be ignored by Parse.
|
||||||
func (c *Config) Define(key string, dtype DataType) {
|
func (c *Config) Define(key string, dtype DataType) {
|
||||||
upper := strings.ToUpper(key)
|
key = cleanKey(key)
|
||||||
entry, ok := c.env[upper]
|
entry, ok := c.env[key]
|
||||||
if ok {
|
if ok {
|
||||||
entry.dtype = dtype
|
entry.dtype = dtype
|
||||||
c.env[upper] = entry
|
c.env[key] = entry
|
||||||
} else {
|
} else {
|
||||||
var entry cEntry
|
var entry cEntry
|
||||||
entry.dtype = dtype
|
entry.dtype = dtype
|
||||||
entry.unset = true
|
entry.unset = true
|
||||||
entry.empty = true
|
entry.empty = true
|
||||||
c.env[upper] = entry
|
c.env[key] = entry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,8 +94,8 @@ func (c *Config) DefineMap(key string, dtype DataType) {
|
|||||||
// Define the type and default value of an environment variable.
|
// Define the type and default value of an environment variable.
|
||||||
// Variables without a defined type will be ignored by Parse.
|
// Variables without a defined type will be ignored by Parse.
|
||||||
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
||||||
upper := strings.ToUpper(key)
|
key = cleanKey(key)
|
||||||
entry, ok := c.env[upper]
|
entry, ok := c.env[key]
|
||||||
if ok {
|
if ok {
|
||||||
if entry.unset {
|
if entry.unset {
|
||||||
entry.value = val
|
entry.value = val
|
||||||
@ -107,7 +104,7 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
|||||||
entry.empty = false
|
entry.empty = false
|
||||||
entry.defval = val
|
entry.defval = val
|
||||||
entry.hasdef = true
|
entry.hasdef = true
|
||||||
c.env[upper] = entry
|
c.env[key] = entry
|
||||||
} else {
|
} else {
|
||||||
var entry cEntry
|
var entry cEntry
|
||||||
entry.dtype = dtype
|
entry.dtype = dtype
|
||||||
@ -116,7 +113,7 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
|||||||
entry.value = val
|
entry.value = val
|
||||||
entry.defval = val
|
entry.defval = val
|
||||||
entry.hasdef = true
|
entry.hasdef = true
|
||||||
c.env[upper] = entry
|
c.env[key] = entry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +258,8 @@ func (c *Config) Status() (ok bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanKey(str string) string {
|
func cleanKey(key string) string {
|
||||||
|
expr := regexp.MustCompile("_+")
|
||||||
fn := func(r rune) rune {
|
fn := func(r rune) rune {
|
||||||
if (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || r == '_' {
|
if (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || r == '_' {
|
||||||
return r
|
return r
|
||||||
@ -269,24 +267,25 @@ func cleanKey(str string) string {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
str = strings.Map(fn, strings.ToUpper(str))
|
key = strings.Map(fn, strings.ToUpper(key))
|
||||||
|
return expr.ReplaceAllString(key, "_")
|
||||||
|
}
|
||||||
|
|
||||||
oldlen := len(str) + 1
|
func keySplit(key string) (left string, right string, ok bool) {
|
||||||
newlen := len(str)
|
key = cleanKey(key)
|
||||||
for newlen < oldlen {
|
pos := strings.LastIndex(key, "_")
|
||||||
oldlen = newlen
|
ok = false
|
||||||
str = strings.ReplaceAll(str, "__", "_")
|
if (pos+1 < len(key)) && (pos > 1) {
|
||||||
newlen = len(str)
|
return cleanKey(key[:pos]), cleanKey(key[pos:]), true
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return str
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
|
func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
|
||||||
val.dtype = TypeNone
|
val.dtype = TypeNone
|
||||||
if c.parsed {
|
if c.parsed {
|
||||||
upper := strings.ToUpper(key)
|
key = cleanKey(key)
|
||||||
entry, ok := c.env[upper]
|
entry, ok := c.env[key]
|
||||||
if ok && (entry.dtype == dtype) {
|
if ok && (entry.dtype == dtype) {
|
||||||
return entry.parsed
|
return entry.parsed
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user