fixes custom types / implement type for file sizes
This commit is contained in:
parent
70fb2dcb8d
commit
cab87868f4
63
cfg.go
63
cfg.go
|
@ -9,6 +9,7 @@ import (
|
|||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
@ -26,6 +27,50 @@ type CustomType interface {
|
|||
Unmarshal(value string) error
|
||||
}
|
||||
|
||||
type FileSize uint64
|
||||
|
||||
func (fs *FileSize) Unmarshal(value string) error {
|
||||
size := FileSize(0)
|
||||
|
||||
if len(value) == 0 {
|
||||
fs = &size
|
||||
return nil
|
||||
}
|
||||
|
||||
value = strings.ToLower(value)
|
||||
last := len(value) - 1
|
||||
|
||||
mp := uint64(1)
|
||||
|
||||
if value[last] == 'b' {
|
||||
switch value[last-1] {
|
||||
case 't':
|
||||
mp = mp << 40
|
||||
value = strings.TrimSpace(value[:last-1])
|
||||
case 'g':
|
||||
mp = mp << 30
|
||||
value = strings.TrimSpace(value[:last-1])
|
||||
case 'm':
|
||||
mp = mp << 20
|
||||
value = strings.TrimSpace(value[:last-1])
|
||||
case 'k':
|
||||
mp = mp << 10
|
||||
value = strings.TrimSpace(value[:last-1])
|
||||
default:
|
||||
value = strings.TrimSpace(value[:last])
|
||||
}
|
||||
}
|
||||
|
||||
ps, err := strconv.ParseUint(value, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*fs = FileSize(uint64(ps) * mp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) AddConfig(path, name string) {
|
||||
f := File{
|
||||
Path: path,
|
||||
|
@ -55,6 +100,24 @@ func (c Config) MergeConfigsInto(dest interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func LoadConfig(file string, dest interface{}) error {
|
||||
f, err := os.Open(file)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
err = parse(f, dest)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parse(file *os.File, dest interface{}) error {
|
||||
reader := bufio.NewReader(file)
|
||||
kvmap := make(map[string]string)
|
||||
|
|
45
cfg_test.go
45
cfg_test.go
|
@ -118,7 +118,7 @@ func (lm *loginMethod) Unmarshal(value string) error {
|
|||
if value == "mail" {
|
||||
m = loginMethod(mail)
|
||||
}
|
||||
lm = &m
|
||||
*lm = m
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -145,11 +145,50 @@ func TestCustomType(t *testing.T) {
|
|||
t.Error(err)
|
||||
}
|
||||
|
||||
if s.Custom != mail {
|
||||
t.Errorf("s.Custom expected to be mail but was %s", s.Custom)
|
||||
if s.Custom != username {
|
||||
t.Errorf("s.Custom expected to be username but was %s", s.Custom)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileSizes(t *testing.T) {
|
||||
type settings struct {
|
||||
Byte FileSize `cfg:"file_size_byte"`
|
||||
Kilobyte FileSize `cfg:"file_size_kilobyte"`
|
||||
Megabyte FileSize `cfg:"file_size_megabyte"`
|
||||
Gigabyte FileSize `cfg:"file_size_gigabyte"`
|
||||
Terabyte FileSize `cfg:"file_size_terabyte"`
|
||||
Empty FileSize `cfg:"file_size_empty"`
|
||||
}
|
||||
|
||||
c := addConfig("./testcfg", "config.conf")
|
||||
|
||||
s := new(settings)
|
||||
|
||||
err := c.MergeConfigsInto(s)
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if s.Byte != 1<<10 {
|
||||
t.Errorf("s.Byte expected to be %d bytes but was %d", 1<<10, s.Byte)
|
||||
}
|
||||
if s.Kilobyte != 1<<10 {
|
||||
t.Errorf("s.Kilobyte expected to be %d bytes but was %d", 1<<10, s.Kilobyte)
|
||||
}
|
||||
if s.Megabyte != 1<<20 {
|
||||
t.Errorf("s.Megabyte expected to be %d but was %d", 1<<20, s.Megabyte)
|
||||
}
|
||||
if s.Gigabyte != 1<<30 {
|
||||
t.Errorf("s.Gigabyte expected to be %d but was %d", 1<<30, s.Gigabyte)
|
||||
}
|
||||
if s.Terabyte != 1<<40 {
|
||||
t.Errorf("s.Terabyte expected to be %d but was %d", 1<<40, s.Terabyte)
|
||||
}
|
||||
if s.Empty != 0 {
|
||||
t.Errorf("s.Empty expected to be 0 but was %d", s.Empty)
|
||||
}
|
||||
}
|
||||
func addConfig(path, filename string) Config {
|
||||
cfg := Config{
|
||||
Files: make([]File, 0, 1),
|
||||
|
|
|
@ -11,4 +11,14 @@ server_port = 42
|
|||
#Array
|
||||
group_list = Admin,User,Moderator
|
||||
|
||||
login_method = mail
|
||||
login_method = username
|
||||
|
||||
|
||||
#FileSizes
|
||||
file_size_byte = 1024B
|
||||
file_size_kilobyte = 1 KB
|
||||
file_size_megabyte = 1 MB
|
||||
file_size_gigabyte = 1 GB
|
||||
file_size_terabyte = 1 TB
|
||||
file_size_empty =
|
||||
file_size_error = NOT_A_FILE_SIZE
|
||||
|
|
Loading…
Reference in New Issue