You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
909 B
Go
57 lines
909 B
Go
10 years ago
|
package moon
|
||
10 years ago
|
|
||
|
import (
|
||
10 years ago
|
"encoding/json"
|
||
10 years ago
|
"fmt"
|
||
|
)
|
||
|
|
||
|
type Config struct {
|
||
|
items map[string]interface{}
|
||
|
}
|
||
|
|
||
|
func (c *Config) hasKey(key string) bool {
|
||
|
if c.items == nil {
|
||
|
return false
|
||
|
}
|
||
|
_, ok := c.items[key]
|
||
|
return ok
|
||
|
}
|
||
|
|
||
|
func (c *Config) setUnique(key string, value interface{}) error {
|
||
|
if c.hasKey(key) {
|
||
|
return fmt.Errorf("the name %s is already defined in this scope", key)
|
||
|
}
|
||
|
c.set(key, value)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (c *Config) set(key string, value interface{}) {
|
||
|
if c.items == nil {
|
||
|
c.items = make(map[string]interface{}, 12)
|
||
|
}
|
||
|
c.items[key] = value
|
||
|
}
|
||
10 years ago
|
|
||
|
func (c *Config) Get(key string) interface{} {
|
||
|
if c.items == nil {
|
||
|
return nil
|
||
|
}
|
||
|
return c.items[key]
|
||
|
}
|
||
|
|
||
|
func (c *Config) GetString(key string) string {
|
||
|
v := c.Get(key)
|
||
|
if v == nil {
|
||
|
return ""
|
||
|
}
|
||
|
s, ok := v.(string)
|
||
|
if ok {
|
||
|
return s
|
||
|
}
|
||
|
return ""
|
||
|
}
|
||
10 years ago
|
|
||
|
func (c *Config) MarshalJSON() ([]byte, error) {
|
||
|
return json.Marshal(c.items)
|
||
|
}
|