jmattheis / goverter

Generate type-safe Go converters by simply defining an interface
https://goverter.jmattheis.de/
MIT License
496 stars 46 forks source link

How to generate output in same namespace as config? #35

Closed morganhein closed 1 year ago

morganhein commented 1 year ago

Have you read the project readme?

Describe your question I have an situation that is maybe not-idiomatic, but we have some unique constraints.

I need to generate files in the same directory/namespace as the goverter config. The file gets generated, but the generated file tries to import the current namespace, which obviously doesn't work.

Is there some config/flag I can get goverter to detect it's working with a single namespace, and doesn't need to import itself?

I forked goverter and mangled the runner_test.go and added a scenario that generates output in the same package as the imported models/extended methods, and it worked... which leads me to believe something in application bootstrapping is just assuming a namespace must always be imported?

File structure is as follows:

public-api/pkg/button/goverter_input.go
public-api/pkg/button/some_models.go
public-api/internal/service/models/more_models.go

and I want to generate to:

public-api/pkg/button/converter_gen.go

Error is as follows:

-: import cycle not allowed: import stack: [github.com/morganhein/public-api/pkg/button github.com/morganhein/public-api/pkg/button]
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.

package button

import (
    models "github.com/morganhein/public-api/internal/service/models"
    "github.com/morganhein/public-api/pkg/button"    // <--- don't need this, and references here should not require a namespace
)
jmattheis commented 1 year ago

You need to define the absolute package path via -packagePath when calling goverter, then only necessary imports will be done.

Example:

goverter -output=./generated.go -packageName=simple -packagePath=github.com/jmattheis/goverter/example/simple github.com/jmattheis/goverter/example/simple
morganhein commented 1 year ago

Jannis, thanks for the quick response!

I tried that and it does not work. Here's some screenshots illustrating:

image

image

image

Am I missing something?

morganhein commented 1 year ago

The unionpacific_edi.go is the goverter input here. That was not clear.

morganhein commented 1 year ago

I think I see the issue. The package path is a go path, not a file path....

edit doesn't work still..hmmm

jmattheis commented 1 year ago

You need to delete the old generated file. Goverter needs type information for generating the converter, but currently there is an import cycle, so it can't compile the code.

morganhein commented 1 year ago

Ugh. That's a little embarrassing.

Case closed. Thank you!

jmattheis commented 1 year ago

No problem, I'll improve the error message. It'll look like this:

could not load package github.com/jmattheis/goverter/execution

/ABSOLUTE/execution/input.go:6:1: expected declaration, found oops

Goverter cannot generate converters when there are compile errors because it
requires the type information from the compiled sources.