add define
This commit is contained in:
parent
8b7af226fe
commit
b93b37bc5f
42
main.go
42
main.go
@ -2,21 +2,29 @@ package envconf
|
|||||||
|
|
||||||
import ("strings"
|
import ("strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
"fmt"
|
|
||||||
"os")
|
"os")
|
||||||
|
|
||||||
const TypeNone = (1 << 1)
|
type DataType int
|
||||||
const TypeInt = (1 << 2)
|
|
||||||
const TypeDuration = (1 << 3)
|
const (
|
||||||
const TypeString = (1 << 4)
|
TypeNone DataType = iota
|
||||||
|
TypeUnset DataType = iota
|
||||||
|
TypeInt DataType = iota
|
||||||
|
TypeDuration DataType = iota
|
||||||
|
TypeString DataType = iota
|
||||||
|
)
|
||||||
|
|
||||||
|
type cEntry struct {
|
||||||
|
value string
|
||||||
|
dtype DataType
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
env map[string]map[string]int
|
env map[string]cEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFirstRune(str string)(rune) {
|
func getFirstRune(str string)(rune) {
|
||||||
for _,v := range str {
|
for _,v := range str {
|
||||||
fmt.Printf("symbol (%c) (%d)\n", v, v)
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return rune(0)
|
return rune(0)
|
||||||
@ -24,19 +32,29 @@ func getFirstRune(str string)(rune) {
|
|||||||
|
|
||||||
func NewConfig()(*Config) {
|
func NewConfig()(*Config) {
|
||||||
config := new(Config)
|
config := new(Config)
|
||||||
config.env = make(map[string]map[string]int)
|
config.env = make(map[string]cEntry)
|
||||||
for _,v := range os.Environ() {
|
for _,v := range os.Environ() {
|
||||||
splitted := strings.SplitN(v, "=", 2)
|
splitted := strings.SplitN(v, "=", 2)
|
||||||
if len(splitted) == 2 {
|
if len(splitted) == 2 {
|
||||||
key := strings.TrimSpace(strings.ToLower(splitted[0]))
|
key := strings.TrimSpace(strings.ToLower(splitted[0]))
|
||||||
if unicode.IsLetter(getFirstRune(key)) {
|
if unicode.IsLetter(getFirstRune(key)) {
|
||||||
strval := splitted[1]
|
var entry cEntry
|
||||||
val := make(map[string]int)
|
entry.value = splitted[1]
|
||||||
val[strval] = TypeNone
|
entry.dtype = TypeNone
|
||||||
config.env[key] = val
|
config.env[key] = entry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) Define(key string, dtype DataType) {
|
||||||
|
entry, ok := c.env[key]
|
||||||
|
if ok {
|
||||||
|
entry.dtype = dtype
|
||||||
|
} else {
|
||||||
|
var entry cEntry
|
||||||
|
entry.dtype = TypeUnset
|
||||||
|
c.env[key] = entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user