Skip to content

Commit

Permalink
Merge pull request #14 from g4s8/i13
Browse files Browse the repository at this point in the history
fix: generate type with field type ast ref
  • Loading branch information
g4s8 committed Mar 12, 2024
2 parents 19e804d + 7b40d73 commit 4c5f257
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
18 changes: 18 additions & 0 deletions _examples/typeonly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "time"

//go:generate go run ../ -output typeonly.md -type Config

type Config struct {
// Some time.
SomeTime MyTime `env:"SOME_TIME"`
}

type MyTime time.Time

func (t *MyTime) UnmarshalText(text []byte) error {
tt, err := time.Parse("2006-01-02", string(text))
*t = MyTime(tt)
return err
}
6 changes: 6 additions & 0 deletions _examples/typeonly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Environment Variables

## Config

- `SOME_TIME` - Some time.

9 changes: 6 additions & 3 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import (
"strings"
)

// visitor nodes types
//go:generate go run golang.org/x/tools/cmd/stringer@v0.19.0 -type=nodeKind
type nodeKind int

// visitor nodes kinds
const (
nodeUnknown int = iota
nodeUnknown nodeKind = iota
nodeType
nodeRoot
nodeStruct
nodeField
)

type visitorNode struct {
kind int
kind nodeKind
typeName string // type name if node is a type or field type name if node is a field
names []string // it's possible that a field has multiple names
doc string // field or type documentation or comment if doc is empty
Expand Down
11 changes: 6 additions & 5 deletions inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ func (i *inspector) traverseAST(root *visitorNode, targetName string) []*EnvScop
scopes := make([]*EnvScope, 0, len(root.children))
logger := logger()
for _, child := range root.children {
if child.kind != nodeType && child.kind != nodeStruct {
panic(fmt.Sprintf("expected type node root child, got %v", child.kind))
}

if !i.all && targetName != child.typeName {
logger.Printf("inspector: (traverse) skipping node: %v", child.typeName)
continue
}

if child.kind != nodeType && child.kind != nodeStruct {
panic(fmt.Sprintf("expected type node root child, got %v (%v)", child.kind, child.typeName))
}

logger.Printf("inspector: (traverse) process node: %v", child.typeName)

if scope := newScope(child, i.useFieldNames); scope != nil {
Expand All @@ -78,7 +79,7 @@ func newScope(node *visitorNode, useFieldNames bool) *EnvScope {
}

logger := logger()
logger.Printf("inspecctor: (scope) got node: %v", node.names)
logger.Printf("inspector: (scope) got node: %v", node.names)

scope := &EnvScope{
Name: node.names[0],
Expand Down
27 changes: 27 additions & 0 deletions nodekind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c5f257

Please sign in to comment.