Tidy up logic

This commit is contained in:
Gregory Eremin 2015-01-18 18:23:58 +07:00
parent f5a875b743
commit e7d10843db

View File

@ -14,10 +14,11 @@ type (
} }
configField struct { configField struct {
Path string `json:"path"` Path string `json:"path"`
Type string `json:"type"`
Value interface{} `json:"value"`
IsRequired bool `json:"is_required"` IsRequired bool `json:"is_required"`
IsReadonly bool `json:"is_readonly"` IsReadonly bool `json:"is_readonly"`
Title string `json:"title"` Title string `json:"title"`
Description string `json:"description"`
Options []string `json:"options"` Options []string `json:"options"`
} }
) )
@ -25,7 +26,6 @@ type (
const ( const (
tJson = "json" tJson = "json"
tTitle = "title" tTitle = "title"
tDescription = "description"
tAttrs = "attrs" tAttrs = "attrs"
tOptions = "options" tOptions = "options"
aRequired = "required" aRequired = "required"
@ -60,44 +60,45 @@ func (c *config) meta(prefix string) []*configField {
var ( var (
fields = []*configField{} fields = []*configField{}
cval = reflect.ValueOf(c.config) cval = reflect.ValueOf(c.config)
typ = reflect.TypeOf(c.config) ctyp = reflect.TypeOf(c.config)
kind = cval.Kind() ckind = cval.Kind()
) )
if kind != reflect.Struct { if ckind != reflect.Struct {
panic(fmt.Errorf("Config is expected to be a Struct, not %s", kind.String())) panic(fmt.Errorf("Config is expected to be a Struct, not %s", ckind.String()))
} }
for i := 0; i < cval.NumField(); i++ { for i := 0; i < cval.NumField(); i++ {
var ( var (
field = typ.Field(i) field = ctyp.Field(i)
val = cval.Field(i) val = cval.Field(i)
kind = val.Kind()
jsonKey = field.Tag.Get(tJson) jsonKey = field.Tag.Get(tJson)
path = strings.Join([]string{prefix, jsonKey}, "/") path = strings.Join([]string{prefix, jsonKey}, "/")
title = field.Tag.Get(tTitle) title = field.Tag.Get(tTitle)
description = field.Tag.Get(tDescription) attrs = field.Tag.Get(tAttrs)
attrs = strings.Split(field.Tag.Get(tAttrs), sep) options = field.Tag.Get(tOptions)
options = strings.Split(field.Tag.Get(tOptions), sep)
cf = &configField{ cf = &configField{
Path: path, Path: path,
Type: val.Kind().String(),
Title: title, Title: title,
Description: description,
} }
) )
// Skip field if no tags are set if title != "" || len(attrs) == 0 || len(options) == 0 {
if title == "" && len(attrs) == 0 && len(options) == 0 {
continue
}
// Substitute field name for title if none set // Substitute field name for title if none set
if kind != reflect.Struct {
cf.Value = val.Interface()
}
if title == "" { if title == "" {
cf.Title = field.Name cf.Title = field.Name
} }
if len(options) > 0 {
for _, attr := range attrs { cf.Options = strings.Split(options, sep)
}
for _, attr := range strings.Split(attrs, sep) {
if attr == aRequired { if attr == aRequired {
cf.IsRequired = true cf.IsRequired = true
} }
@ -107,9 +108,10 @@ func (c *config) meta(prefix string) []*configField {
} }
fields = append(fields, cf) fields = append(fields, cf)
}
// Recursion here // Recursion here
if val.Kind() == reflect.Struct { if kind == reflect.Struct {
subconf := &config{ subconf := &config{
config: val.Interface(), config: val.Interface(),
} }