1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
Package config is a simple tool to handle key-value config, like Redis's configuration:
# commet
key = value
*/
package config
import (
"errors"
"fmt"
"strconv"
"strings"
)
var (
ErrNil = errors.New("nil value")
)
type Config struct {
Values map[string]string
}
func NewConfig() *Config {
c := &Config{}
c.Values = make(map[string]string)
return c
}
/*
bool: true, false, 0, 1, or ""
*/
func (c *Config) GetBool(key string) (bool, error) {
v, err := c.GetString(key)
if err != nil {
return false, err
}
v = strings.ToLower(v)
switch v {
case "true", "1":
return true, nil
case "false", "0", "":
return false, nil
default:
return false, fmt.Errorf("invalid bool format %s", v)
}
}
/*
int may be pure number or below format:
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
*/
func (c *Config) GetInt64(key string) (int64, error) {
v, err := c.GetString(key)
if err != nil {
return 0, err
}
if len(v) == 0 {
return 0, ErrNil
}
var scale int64 = 1
v = strings.ToLower(v)
var b bool = false
if v[len(v)-1] == 'b' {
v = v[0 : len(v)-1]
b = true
}
if len(v) == 0 {
return 0, fmt.Errorf("invalid number format %s", v)
}
switch v[len(v)-1] {
case 'k':
v = v[0 : len(v)-1]
if b {
scale = 1024
} else {
scale = 1000
}
break
case 'm':
v = v[0 : len(v)-1]
if b {
scale = 1024 * 1024
} else {
scale = 1000 * 1000
}
case 'g':
v = v[0 : len(v)-1]
if b {
scale = 1024 * 1024 * 1024
} else {
scale = 1000 * 1000 * 1000
}
}
var n int64
n, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return 0, err
}
return n * scale, nil
}
func (c *Config) GetUint64(key string) (uint64, error) {
v, err := c.GetInt64(key)
if v < 0 {
return 0, fmt.Errorf("negative number %d", v)
}
return uint64(v), err
}
func (c *Config) GetInt(key string) (int, error) {
v, err := c.GetInt64(key)
return int(v), err
}
func (c *Config) GetString(key string) (string, error) {
v, ok := c.Values[key]
if !ok {
return "", ErrNil
} else {
return v, nil
}
}
func (c *Config) SetString(key string, value string) {
c.Values[key] = value
}
func (c *Config) SetInt64(key string, n int64) {
c.Values[key] = strconv.FormatInt(n, 10)
}
func (c *Config) SetUint64(key string, n uint64) {
c.Values[key] = strconv.FormatUint(n, 10)
}
func (c *Config) SetInt(key string, n int) {
c.SetInt64(key, int64(n))
}
func (c *Config) SetBool(key string, v bool) {
if v {
c.Values[key] = "true"
} else {
c.Values[key] = "false"
}
}
|