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