From b93b37bc5f5322e320812d7c51e01fa7b17521a7 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Tue, 23 Mar 2021 11:40:25 +0100 Subject: [PATCH] add define --- main.go | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 77503a1..f27b2c8 100644 --- a/main.go +++ b/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 + } +}