Skip to content

Commit

Permalink
Merge pull request #32 from g4s8/i30
Browse files Browse the repository at this point in the history
fix: resolve embedded field types
  • Loading branch information
g4s8 authored Oct 31, 2024
2 parents 3c8918d + acefaee commit f8c78f7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
28 changes: 28 additions & 0 deletions ast/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ func (tr FieldTypeRef) String() string {
return ""
}

func (tr FieldTypeRef) IsBuiltIn() bool {
switch tr.Name {
case
"string",
"int",
"int8",
"int16",
"int32",
"int64",
"uint",
"uint8",
"uint16",
"uint32",
"uint64",
"float32",
"float64",
"bool",
"byte",
"rune",
"complex64",
"complex128":
return true

default:
return false
}
}

func (fs *FileSpec) String() string {
return fs.Name
}
Expand Down
15 changes: 14 additions & 1 deletion ast/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,21 @@ func findCommentLine(c *ast.Comment, fset *token.FileSet, file *ast.File) int {
}

func getFieldSpec(n *ast.Field, pkg string) *FieldSpec {
names := extractFieldNames(n)
allPrivate := true
for _, name := range names {
if strings.ToLower(name[:1]) != name[:1] {
allPrivate = false
break
}
}
if len(names) > 0 && allPrivate {
// skip private fields
return nil
}

var fs FieldSpec
fs.Names = extractFieldNames(n)
fs.Names = names
if !getFieldTypeRef(n.Type, &fs.TypeRef) {
// unsupported field type
return nil
Expand Down
13 changes: 11 additions & 2 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ func (c *Converter) ScopeFromType(res *TypeResolver, t *ast.TypeSpec) *EnvScope
func (c *Converter) DocItemsFromFields(res *TypeResolver, prefix string, fields []*ast.FieldSpec) []*EnvDocItem {
var items []*EnvDocItem
for _, f := range fields {
debug.Logf("\t# CONV: field [%s]\n", strings.Join(f.Names, ","))
debug.Logf("\t# CONV: field [%s] type=%s flen=%d\n",
strings.Join(f.Names, ","), f.TypeRef, len(f.Fields))
if len(f.Names) == 0 {
// embedded field
if len(f.Fields) == 0 {
// resolve embedded types
tpe := res.Resolve(&f.TypeRef)
if tpe != nil {
f.Fields = tpe.Fields
}
}
items = append(items, c.DocItemsFromFields(res, prefix, f.Fields)...)
continue
}
Expand All @@ -82,10 +90,11 @@ func (c *Converter) DocItemsFromField(resolver *TypeResolver, prefix string, f *
children = c.DocItemsFromFields(resolver, prefix, f.Fields)
debug.Logf("\t# CONV: struct %q (%d childrens)\n", f.TypeRef.String(), len(children))
case ast.FieldTypeSelector, ast.FieldTypeIdent, ast.FieldTypeArray, ast.FieldTypePtr:
if newPrefix == "" {
if f.TypeRef.IsBuiltIn() {
break
}
tpe := resolver.Resolve(&f.TypeRef)
debug.Logf("\t# CONV: resolve %q -> %v\n", f.TypeRef.String(), tpe)
if tpe == nil {
fmt.Fprintf(os.Stderr, "Failed to resolve type %q\n", f.TypeRef.String())
break
Expand Down
1 change: 0 additions & 1 deletion converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func TestConvertDocItems(t *testing.T) {
Tag: `env:"FIELD4,notEmpty,expand"`,
},
},
Tag: `envPrefix:"PREFIX_"`,
TypeRef: ast.FieldTypeRef{
Kind: ast.FieldTypeStruct,
},
Expand Down
16 changes: 16 additions & 0 deletions testdata/parser/embedded.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ package testdata

import "time"

type ServerConfig struct {
// Host of the server.
Host string `env:"HOST"`
}

type Config struct {
ServerConfig

// Start date.
Start Date `env:"START,notEmpty"`
}
Expand All @@ -26,9 +33,18 @@ testcase:
pkg: testdata
export: true
types:
- name: ServerConfig
export: false
fields:
- names: [Host]
doc: Host of the server.
tag: env:"HOST"
type_ref: {name: string, kind: Ident}
- name: Config
export: true
fields:
- names: []
type_ref: {name: ServerConfig, kind: Ident}
- names: [Start]
doc: Start date.
tag: env:"START,notEmpty"
Expand Down
4 changes: 2 additions & 2 deletions testdata/parser/funcs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package testdata
//go:generate envdoc -output test.md --all
type aconfig struct {
// this is some value
somevalue string `env:"SOME_VALUE" envDefault:"somevalue"`
Somevalue string `env:"SOME_VALUE" envDefault:"somevalue"`
}

// when this function is present, go generate panics with "expected type node root child, got nodeField ()".
Expand All @@ -34,7 +34,7 @@ testcase:
- name: aconfig
export: true
fields:
- names: [somevalue]
- names: [Somevalue]
doc: this is some value
tag: env:"SOME_VALUE" envDefault:"somevalue"
type_ref: {name: string, kind: Ident}

0 comments on commit f8c78f7

Please sign in to comment.