Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing anonymous nested structs fails #5

Closed
nics opened this issue Feb 6, 2024 · 4 comments · Fixed by #6
Closed

Parsing anonymous nested structs fails #5

nics opened this issue Feb 6, 2024 · 4 comments · Fixed by #6
Assignees

Comments

@nics
Copy link

nics commented Feb 6, 2024

Parsing this config struct panics on the nested struct:

type Config struct {
	Repo struct {
		Conn  string  `env:"CONN,notEmpty"`
	} `envPrefix:"REPO_"`
}

Error message:

main.(*inspector).parseField(0xc0000ca400, 0xc0000f2380)
        /Users/xxx/go/pkg/mod/github.com/g4s8/[email protected]/inspector.go:207 +0x678
main.(*inspector).Visit(0xc0000ca400, {0x1236658?, 0xc0000f21c0?})
        /Users/xxx/go/pkg/mod/github.com/g4s8/[email protected]/inspector.go:136 +0x2e5
go/ast.Walk({0x1236018?, 0xc0000ca400?}, {0x1236658?, 0xc0000f21c0?})
        /usr/local/Cellar/go/1.21.6/libexec/src/go/ast/walk.go:51 +0x5c
go/ast.Walk({0x1236018?, 0xc0000ca400?}, {0x1236ec8?, 0xc0000f2400?})
        /usr/local/Cellar/go/1.21.6/libexec/src/go/ast/walk.go:344 +0x1b85
go/ast.walkDeclList(...)
        /usr/local/Cellar/go/1.21.6/libexec/src/go/ast/walk.go:38
go/ast.Walk({0x1236018?, 0xc0000ca400?}, {0x1236608?, 0xc0000ba8c0?})
        /usr/local/Cellar/go/1.21.6/libexec/src/go/ast/walk.go:366 +0x1b05
main.(*inspector).inspect(0xc0000ca400, {0x1236608?, 0xc0000ba8c0?})
        /Users/xxx/go/pkg/mod/github.com/g4s8/[email protected]/inspector.go:66 +0x5b
main.(*inspector).inspectFile(0xc0000ca400, {0xc000012107, 0x9})
        /Users/xxx/go/pkg/mod/github.com/g4s8/[email protected]/inspector.go:61 +0x128
main.(*generator).generate(0xc0000f0600, {0x1235fd8, 0xc000090400})
        /Users/xxx/go/pkg/mod/github.com/g4s8/[email protected]/generator.go:91 +0xc5
main.run(0xc0000e7e60)
        /Users/xxx/go/pkg/mod/github.com/g4s8/[email protected]/main.go:116 +0x618
main.main()
        /Users/xxx/go/pkg/mod/github.com/g4s8/[email protected]/main.go:64 +0x112

Thanks for a handy tool!

@g4s8
Copy link
Owner

g4s8 commented Feb 6, 2024

Thanks for reporting! I'll look into this problem

@g4s8 g4s8 self-assigned this Feb 6, 2024
g4s8 added a commit that referenced this issue Feb 6, 2024
If the field is `envPrefix`-ed anonymous structure, then
inspector handles this structure with random-generated type name
and use field doc and comments as struct attributes.

Fix: #5
@g4s8 g4s8 closed this as completed in #6 Feb 6, 2024
@g4s8
Copy link
Owner

g4s8 commented Feb 6, 2024

The fix should be available in v0.1.1 version.

@nics
Copy link
Author

nics commented Feb 6, 2024

Hi @g4s8 thanks it works now. Note that I get a segfault if there is no description comment.

So this works:

type Config struct {
       // Repository configuration
       Repo struct {
		Conn  string  `env:"CONN,notEmpty"`
	} `envPrefix:"REPO_"`
}

But this segfaults:

type Config struct {
	Repo struct {
		Conn  string  `env:"CONN,notEmpty"`
	} `envPrefix:"REPO_"`
}

@g4s8
Copy link
Owner

g4s8 commented Feb 6, 2024

Sorry, my bad. The panic was fixed in v0.1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants