Update code generator for plugin's dyn conf
This commit is contained in:
parent
87fd51d7ec
commit
6ae50389e6
3 changed files with 22 additions and 18 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
@ -183,15 +184,15 @@ func (c Centrifuge) writeStruct(name string, obj *types.Struct, rootPkg string,
|
|||
continue
|
||||
}
|
||||
|
||||
values, ok := lookupTagValue(obj.Tag(i), "json")
|
||||
if len(values) > 0 && values[0] == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
b.WriteString(fmt.Sprintf("\t%s %s", field.Name(), fType))
|
||||
|
||||
tags := obj.Tag(i)
|
||||
if tags != "" {
|
||||
tg := extractJSONTag(tags)
|
||||
|
||||
if tg != `json:"-"` {
|
||||
b.WriteString(fmt.Sprintf(" `%s`", tg))
|
||||
}
|
||||
if ok {
|
||||
b.WriteString(fmt.Sprintf(" `json:\"%s\"`", strings.Join(values, ",")))
|
||||
}
|
||||
|
||||
b.WriteString("\n")
|
||||
|
@ -202,16 +203,19 @@ func (c Centrifuge) writeStruct(name string, obj *types.Struct, rootPkg string,
|
|||
return b.String()
|
||||
}
|
||||
|
||||
func extractJSONTag(value string) string {
|
||||
fields := strings.Fields(value)
|
||||
|
||||
for _, field := range fields {
|
||||
if strings.HasPrefix(field, `json:"`) {
|
||||
return field
|
||||
}
|
||||
func lookupTagValue(raw, key string) ([]string, bool) {
|
||||
value, ok := reflect.StructTag(raw).Lookup(key)
|
||||
if !ok {
|
||||
return nil, ok
|
||||
}
|
||||
|
||||
return ""
|
||||
values := strings.Split(value, ",")
|
||||
|
||||
if len(values) < 1 {
|
||||
return nil, true
|
||||
}
|
||||
|
||||
return values, true
|
||||
}
|
||||
|
||||
func extractPackage(t types.Type) string {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue