diff --git a/cmd/internal/gen/centrifuge.go b/cmd/internal/gen/centrifuge.go index 87c09f272..ddef63429 100644 --- a/cmd/internal/gen/centrifuge.go +++ b/cmd/internal/gen/centrifuge.go @@ -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 { diff --git a/go.mod b/go.mod index 2e47b13ec..cdeee2366 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,7 @@ require ( github.com/tinylib/msgp v1.0.2 // indirect github.com/traefik/gziphandler v1.1.2-0.20210212101304-175e0fad6888 github.com/traefik/paerser v0.1.4 - github.com/traefik/yaegi v0.9.19 + github.com/traefik/yaegi v0.9.20 github.com/uber/jaeger-client-go v2.29.1+incompatible github.com/uber/jaeger-lib v2.2.0+incompatible github.com/unrolled/render v1.0.2 diff --git a/go.sum b/go.sum index 5f1d9d3fb..8c893918a 100644 --- a/go.sum +++ b/go.sum @@ -1174,8 +1174,8 @@ github.com/traefik/gziphandler v1.1.2-0.20210212101304-175e0fad6888 h1:GMY0C+M/w github.com/traefik/gziphandler v1.1.2-0.20210212101304-175e0fad6888/go.mod h1:sLqwoN03tkluITKL+lPEZbfsJQU2suYoKbrR/HeV9aM= github.com/traefik/paerser v0.1.4 h1:/IXjV04Gf6di51H8Jl7jyS3OylsLjIasrwXIIwj1aT8= github.com/traefik/paerser v0.1.4/go.mod h1:FIdQ4Y92ulQUGSeZgxchtBKEcLw1o551PMNg9PoIq/4= -github.com/traefik/yaegi v0.9.19 h1:ze01+pVtKmxSogy0wlAPSvm2LoDYuZj2LdH3S6GxHcQ= -github.com/traefik/yaegi v0.9.19/go.mod h1:FAYnRlZyuVlEkvnkHq3bvJ1lW5be6XuwgLdkYgYG6Lk= +github.com/traefik/yaegi v0.9.20 h1:G05/iDMD3cepEr9QsVGpmCc3N8FQCdUWA3Vlff2WgbA= +github.com/traefik/yaegi v0.9.20/go.mod h1:FAYnRlZyuVlEkvnkHq3bvJ1lW5be6XuwgLdkYgYG6Lk= github.com/transip/gotransip/v6 v6.2.0 h1:0Z+qVsyeiQdWfcAUeJyF0IEKAPvhJwwpwPi2WGtBIiE= github.com/transip/gotransip/v6 v6.2.0/go.mod h1:pQZ36hWWRahCUXkFWlx9Hs711gLd8J4qdgLdRzmtY+g= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8=