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"
|
||||
"unicode"
|
||||
"fmt"
|
||||
"os")
|
||||
|
||||
const TypeNone = (1 << 1)
|
||||
const TypeInt = (1 << 2)
|
||||
const TypeDuration = (1 << 3)
|
||||
const TypeString = (1 << 4)
|
||||
type DataType int
|
||||
|
||||
const (
|
||||
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 {
|
||||
env map[string]map[string]int
|
||||
env map[string]cEntry
|
||||
}
|
||||
|
||||
func getFirstRune(str string)(rune) {
|
||||
for _,v := range str {
|
||||
fmt.Printf("symbol (%c) (%d)\n", v, v)
|
||||
return v
|
||||
}
|
||||
return rune(0)
|
||||
@ -24,19 +32,29 @@ func getFirstRune(str string)(rune) {
|
||||
|
||||
func NewConfig()(*Config) {
|
||||
config := new(Config)
|
||||
config.env = make(map[string]map[string]int)
|
||||
config.env = make(map[string]cEntry)
|
||||
for _,v := range os.Environ() {
|
||||
splitted := strings.SplitN(v, "=", 2)
|
||||
if len(splitted) == 2 {
|
||||
key := strings.TrimSpace(strings.ToLower(splitted[0]))
|
||||
if unicode.IsLetter(getFirstRune(key)) {
|
||||
strval := splitted[1]
|
||||
val := make(map[string]int)
|
||||
val[strval] = TypeNone
|
||||
config.env[key] = val
|
||||
var entry cEntry
|
||||
entry.value = splitted[1]
|
||||
entry.dtype = TypeNone
|
||||
config.env[key] = entry
|
||||
}
|
||||
}
|
||||
}
|
||||
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