1
0
Fork 0

Update code generator for plugin's dyn conf

This commit is contained in:
Ludovic Fernandez 2021-07-15 15:58:08 +02:00 committed by GitHub
parent 87fd51d7ec
commit 6ae50389e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 18 deletions

View file

@ -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 {