Tidy up logic
This commit is contained in:
parent
f5a875b743
commit
e7d10843db
94
config.go
94
config.go
|
@ -13,24 +13,24 @@ type (
|
||||||
config interface{}
|
config interface{}
|
||||||
}
|
}
|
||||||
configField struct {
|
configField struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
IsRequired bool `json:"is_required"`
|
Type string `json:"type"`
|
||||||
IsReadonly bool `json:"is_readonly"`
|
Value interface{} `json:"value"`
|
||||||
Title string `json:"title"`
|
IsRequired bool `json:"is_required"`
|
||||||
Description string `json:"description"`
|
IsReadonly bool `json:"is_readonly"`
|
||||||
Options []string `json:"options"`
|
Title string `json:"title"`
|
||||||
|
Options []string `json:"options"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tJson = "json"
|
tJson = "json"
|
||||||
tTitle = "title"
|
tTitle = "title"
|
||||||
tDescription = "description"
|
tAttrs = "attrs"
|
||||||
tAttrs = "attrs"
|
tOptions = "options"
|
||||||
tOptions = "options"
|
aRequired = "required"
|
||||||
aRequired = "required"
|
aReadonly = "readonly"
|
||||||
aReadonly = "readonly"
|
sep = ","
|
||||||
sep = ","
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *config) dump() ([]byte, error) {
|
func (c *config) dump() ([]byte, error) {
|
||||||
|
@ -60,56 +60,58 @@ 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,
|
||||||
Title: title,
|
Type: val.Kind().String(),
|
||||||
Description: description,
|
Title: title,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Skip field if no tags are set
|
if title != "" || len(attrs) == 0 || len(options) == 0 {
|
||||||
if title == "" && len(attrs) == 0 && len(options) == 0 {
|
// Substitute field name for title if none set
|
||||||
continue
|
if kind != reflect.Struct {
|
||||||
}
|
cf.Value = val.Interface()
|
||||||
|
|
||||||
// Substitute field name for title if none set
|
|
||||||
if title == "" {
|
|
||||||
cf.Title = field.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, attr := range attrs {
|
|
||||||
if attr == aRequired {
|
|
||||||
cf.IsRequired = true
|
|
||||||
}
|
}
|
||||||
if attr == aReadonly {
|
if title == "" {
|
||||||
cf.IsReadonly = true
|
cf.Title = field.Name
|
||||||
|
}
|
||||||
|
if len(options) > 0 {
|
||||||
|
cf.Options = strings.Split(options, sep)
|
||||||
|
}
|
||||||
|
for _, attr := range strings.Split(attrs, sep) {
|
||||||
|
if attr == aRequired {
|
||||||
|
cf.IsRequired = true
|
||||||
|
}
|
||||||
|
if attr == aReadonly {
|
||||||
|
cf.IsReadonly = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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(),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue