Fields list should be an array first
This commit is contained in:
parent
d12ace7f37
commit
e64a903c27
29
field.go
29
field.go
|
@ -6,12 +6,14 @@ import (
|
|||
)
|
||||
|
||||
type field struct {
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind"`
|
||||
Val interface{} `json:"val"`
|
||||
}
|
||||
|
||||
func extractFields(st interface{}, path string) map[string]field {
|
||||
res := make(map[string]field)
|
||||
func extractFields(st interface{}, path string) []field {
|
||||
var res []field
|
||||
|
||||
val := reflect.ValueOf(st)
|
||||
if val.Kind() == reflect.Ptr {
|
||||
|
@ -26,9 +28,7 @@ func extractFields(st interface{}, path string) map[string]field {
|
|||
switch kind := fval.Kind(); kind {
|
||||
case reflect.Struct:
|
||||
sub := extractFields(fval.Interface(), ftyp.Name+".")
|
||||
for k, v := range sub {
|
||||
res[k] = v
|
||||
}
|
||||
res = append(res, sub...)
|
||||
case reflect.Bool,
|
||||
reflect.Int,
|
||||
reflect.Int8,
|
||||
|
@ -43,10 +43,12 @@ func extractFields(st interface{}, path string) map[string]field {
|
|||
reflect.Float32,
|
||||
reflect.Float64,
|
||||
reflect.String:
|
||||
res[path+ftyp.Name] = field{
|
||||
res = append(res, field{
|
||||
Path: path + ftyp.Name,
|
||||
Name: ftyp.Name,
|
||||
Kind: kind.String(),
|
||||
Val: fval.Interface(),
|
||||
}
|
||||
})
|
||||
default:
|
||||
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{} {
|
||||
af := extractFields(a, "")
|
||||
bf := extractFields(b, "")
|
||||
af := indexFields(extractFields(a, ""))
|
||||
bf := indexFields(extractFields(b, ""))
|
||||
|
||||
res := make(map[string][]interface{})
|
||||
for name, f := range af {
|
||||
|
@ -68,3 +70,12 @@ func diff(a, b interface{}) map[string][]interface{} {
|
|||
|
||||
return res
|
||||
}
|
||||
|
||||
func indexFields(fields []field) map[string]field {
|
||||
res := make(map[string]field)
|
||||
for _, f := range fields {
|
||||
res[f.Path] = f
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ func TestExtractFields(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
fields := extractFields(c, "")
|
||||
fields := indexFields(extractFields(c, ""))
|
||||
testField := func(fname, kind string, val interface{}) {
|
||||
if f, ok := fields[fname]; ok {
|
||||
if f.Kind != kind {
|
||||
|
|
11
secondly.go
11
secondly.go
|
@ -24,6 +24,10 @@ var (
|
|||
|
||||
// SetupFlags sets up Confection configuration flags.
|
||||
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")
|
||||
}
|
||||
|
||||
|
@ -99,14 +103,13 @@ func OnChange(field string, fun func(oldVal, newVal interface{})) {
|
|||
|
||||
func bootstrap() {
|
||||
if configFile == "" {
|
||||
panic("path to config file is not set")
|
||||
log.Fatalln("path to config file is not set")
|
||||
}
|
||||
if fileExist(configFile) {
|
||||
log.Println("Loading config file")
|
||||
readConfig()
|
||||
} else {
|
||||
log.Println("Config file not found, saving an empty one")
|
||||
writeConfig()
|
||||
log.Fatalln("Config file not found")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,7 +134,7 @@ func writeConfig() {
|
|||
func updateConfig(body []byte) {
|
||||
dupe := duplicate(config)
|
||||
if err := json.Unmarshal(body, dupe); err != nil {
|
||||
log.Println("Failed to update config")
|
||||
panic("Failed to update config")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue