diff --git a/processor/processor.go b/processor/processor.go index 91b0857..0ab040e 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -330,7 +330,7 @@ func (p *processor) processType(pkg *loader.Package, parentType *types.Type, t g underlying = pkg.TypesInfo.TypeOf(info.RawSpec.Type) } if underlying.String() == "string" { - typeDef.Values = extractConstantsForAliasedString(pkg, typeDef.Name) + typeDef.EnumValues = lookupConstantValuesForAliasedType(pkg, typeDef.Name) } typeDef.UnderlyingType = p.processType(pkg, typeDef, underlying, depth+1) p.addReference(typeDef, typeDef.UnderlyingType) @@ -576,34 +576,36 @@ func (p *processor) parseMarkers() { } } -func extractConstantsForAliasedString(pkg *loader.Package, name string) []types.Const { - consts := []types.Const{} - for _, f := range pkg.Syntax { - for _, d := range f.Decls { - switch n := d.(type) { - case *ast.GenDecl: - if n.Tok == token.CONST { - for _, s := range n.Specs { - switch v := s.(type) { - case *ast.ValueSpec: - if id, ok := v.Type.(*ast.Ident); ok && id.String() == name { - if len(v.Values) == 1 { - if b, ok := v.Values[0].(*ast.BasicLit); ok { - consts = append(consts, types.Const{ - // Remove the '"' signs from the start and end of the value - Name: b.Value[1 : len(b.Value)-1], - Doc: v.Doc.Text(), - }) - } - } - } - default: - } - } +func lookupConstantValuesForAliasedType(pkg *loader.Package, aliasTypeName string) []types.EnumValue { + values := []types.EnumValue{} + for _, file := range pkg.Syntax { + for _, decl := range file.Decls { + node, ok := decl.(*ast.GenDecl) + if !ok || node.Tok != token.CONST { + continue + } + for _, spec := range node.Specs { + // look for constant declaration + v, ok := spec.(*ast.ValueSpec) + if !ok { + continue + } + // value type must match the alias type name and have exactly one value + if id, ok := v.Type.(*ast.Ident); !ok || id.String() != aliasTypeName || len(v.Values) != 1 { + continue + } + // convert to a basic type to access to the value + b, ok := v.Values[0].(*ast.BasicLit) + if !ok { + continue } - default: + values = append(values, types.EnumValue{ + // remove the '"' signs from the start and end of the value + Name: b.Value[1 : len(b.Value)-1], + Doc: v.Doc.Text(), + }) } } } - return consts + return values } diff --git a/templates/markdown/type.tpl b/templates/markdown/type.tpl index 7b99257..b84eb97 100644 --- a/templates/markdown/type.tpl +++ b/templates/markdown/type.tpl @@ -36,9 +36,9 @@ _Appears in:_ {{ end -}} -{{ if $type.Values -}} +{{ if $type.EnumValues -}} | Field | Description | -{{ range $type.Values -}} +{{ range $type.EnumValues -}} | `{{ .Name }}` | {{ markdownRenderFieldDoc .Doc }} | {{ end -}} {{ end -}} diff --git a/test/templates/markdown/type.tpl b/test/templates/markdown/type.tpl index ceed63c..9aaf809 100644 --- a/test/templates/markdown/type.tpl +++ b/test/templates/markdown/type.tpl @@ -39,9 +39,9 @@ _Appears in:_ {{ end -}} -{{ if $type.Values -}} +{{ if $type.EnumValues -}} | Field | Description | -{{ range $type.Values -}} +{{ range $type.EnumValues -}} | `{{ .Name }}` | {{ markdownRenderFieldDoc .Doc }} | {{ end -}} {{ end -}} diff --git a/types/types.go b/types/types.go index b2e5d44..05f690b 100644 --- a/types/types.go +++ b/types/types.go @@ -110,7 +110,7 @@ type Type struct { ValueType *Type `json:"valueType"` // for maps Fields Fields `json:"fields"` // for structs References []*Type `json:"-"` // other types that refer to this type - Values []Const `json:"values"` // for values of constants of aliased types + EnumValues []EnumValue `json:"enumValues"` // for enum values of aliased string types } func (t *Type) IsBasic() bool { @@ -371,8 +371,8 @@ func (gvd GroupVersionDetails) SortedKinds() []string { return kindsList } -// Const describes a constant value for enumerations -type Const struct { +// EnumValue describes a constant value for enumerations +type EnumValue struct { Name string Doc string }