1
0
Fork 0

Fields list should be an array first

This commit is contained in:
Gregory Eremin 2015-08-29 17:32:39 +03:00
parent d12ace7f37
commit e64a903c27
3 changed files with 28 additions and 14 deletions

View File

@ -6,12 +6,14 @@ import (
) )
type field struct { type field struct {
Path string `json:"path"`
Name string `json:"name"`
Kind string `json:"kind"` Kind string `json:"kind"`
Val interface{} `json:"val"` Val interface{} `json:"val"`
} }
func extractFields(st interface{}, path string) map[string]field { func extractFields(st interface{}, path string) []field {
res := make(map[string]field) var res []field
val := reflect.ValueOf(st) val := reflect.ValueOf(st)
if val.Kind() == reflect.Ptr { if val.Kind() == reflect.Ptr {
@ -26,9 +28,7 @@ func extractFields(st interface{}, path string) map[string]field {
switch kind := fval.Kind(); kind { switch kind := fval.Kind(); kind {
case reflect.Struct: case reflect.Struct:
sub := extractFields(fval.Interface(), ftyp.Name+".") sub := extractFields(fval.Interface(), ftyp.Name+".")
for k, v := range sub { res = append(res, sub...)
res[k] = v
}
case reflect.Bool, case reflect.Bool,
reflect.Int, reflect.Int,
reflect.Int8, reflect.Int8,
@ -43,10 +43,12 @@ func extractFields(st interface{}, path string) map[string]field {
reflect.Float32, reflect.Float32,
reflect.Float64, reflect.Float64,
reflect.String: reflect.String:
res[path+ftyp.Name] = field{ res = append(res, field{
Path: path + ftyp.Name,
Name: ftyp.Name,
Kind: kind.String(), Kind: kind.String(),
Val: fval.Interface(), Val: fval.Interface(),
} })
default: default:
log.Printf("Field type %q not supported for field %q\n", kind, path+ftyp.Name) log.Printf("Field type %q not supported for field %q\n", kind, path+ftyp.Name)
} }
@ -56,8 +58,8 @@ func extractFields(st interface{}, path string) map[string]field {
} }
func diff(a, b interface{}) map[string][]interface{} { func diff(a, b interface{}) map[string][]interface{} {
af := extractFields(a, "") af := indexFields(extractFields(a, ""))
bf := extractFields(b, "") bf := indexFields(extractFields(b, ""))
res := make(map[string][]interface{}) res := make(map[string][]interface{})
for name, f := range af { for name, f := range af {
@ -68,3 +70,12 @@ func diff(a, b interface{}) map[string][]interface{} {
return res return res
} }
func indexFields(fields []field) map[string]field {
res := make(map[string]field)
for _, f := range fields {
res[f.Path] = f
}
return res
}

View File

@ -15,7 +15,7 @@ func TestExtractFields(t *testing.T) {
}, },
} }
fields := extractFields(c, "") fields := indexFields(extractFields(c, ""))
testField := func(fname, kind string, val interface{}) { testField := func(fname, kind string, val interface{}) {
if f, ok := fields[fname]; ok { if f, ok := fields[fname]; ok {
if f.Kind != kind { if f.Kind != kind {

View File

@ -24,6 +24,10 @@ var (
// SetupFlags sets up Confection configuration flags. // SetupFlags sets up Confection configuration flags.
func SetupFlags() { func SetupFlags() {
if flag.Parsed() {
log.Fatalln("secondly.SetupFlags() must be called before flag.Parse()")
}
flag.StringVar(&configFile, "config", "config.json", "Path to config file") flag.StringVar(&configFile, "config", "config.json", "Path to config file")
} }
@ -99,14 +103,13 @@ func OnChange(field string, fun func(oldVal, newVal interface{})) {
func bootstrap() { func bootstrap() {
if configFile == "" { if configFile == "" {
panic("path to config file is not set") log.Fatalln("path to config file is not set")
} }
if fileExist(configFile) { if fileExist(configFile) {
log.Println("Loading config file") log.Println("Loading config file")
readConfig() readConfig()
} else { } else {
log.Println("Config file not found, saving an empty one") log.Fatalln("Config file not found")
writeConfig()
} }
} }
@ -131,7 +134,7 @@ func writeConfig() {
func updateConfig(body []byte) { func updateConfig(body []byte) {
dupe := duplicate(config) dupe := duplicate(config)
if err := json.Unmarshal(body, dupe); err != nil { if err := json.Unmarshal(body, dupe); err != nil {
log.Println("Failed to update config") panic("Failed to update config")
return return
} }