Skip to content

Commit

Permalink
Modified as per the review.
Browse files Browse the repository at this point in the history
  • Loading branch information
liorokman committed Apr 12, 2024
1 parent 1d70297 commit e56910c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
56 changes: 29 additions & 27 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions templates/markdown/type.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down
4 changes: 2 additions & 2 deletions test/templates/markdown/type.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down
6 changes: 3 additions & 3 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit e56910c

Please sign in to comment.