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

Infer output:package from output:file #181

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

applejag
Copy link
Contributor

Heyo! I wanted to try looking into the code base.

I started coding this before I saw your response on #180 (comment). But I'm wondering how well this implementation compares with how you envision this feature.

As I'm brand new to this code base then you probably have some ideas on how to do this better :)

Closes #180

Copy link
Owner

@jmattheis jmattheis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! It does work, but I've some remarks that remove the need for doing another packages.Load.

config/converter.go Outdated Show resolved Hide resolved
config/converter.go Show resolved Hide resolved
example/format/interfacefunction-samepackage/input.go Outdated Show resolved Hide resolved
config/converter.go Outdated Show resolved Hide resolved
docs/reference/output.md Show resolved Hide resolved
docs/reference/output.md Show resolved Hide resolved
Copy link

codecov bot commented Jan 29, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 21 lines in your changes missing coverage. Please review.

Project coverage is 95.52%. Comparing base (3ad3902) to head (91845c7).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
pkgload/pkgload.go 69.64% 12 Missing and 5 partials ⚠️
config/converter.go 85.71% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #181      +/-   ##
==========================================
- Coverage   95.92%   95.52%   -0.41%     
==========================================
  Files          49       49              
  Lines        3144     3215      +71     
==========================================
+ Hits         3016     3071      +55     
- Misses        101      112      +11     
- Partials       27       32       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor Author

@applejag applejag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! It does work, but I've some remarks that remove the need for doing another packages.Load.

I think we do need to have another packages.Load as the output:file might point to a directory that we haven't loaded yet.

I've worked in some caching in d79e0bb which prevents double loading of files we've already loaded.

docs/reference/output.md Show resolved Hide resolved
config/converter.go Show resolved Hide resolved
config/converter.go Outdated Show resolved Hide resolved
example/format/interfacefunction-samepackage/input.go Outdated Show resolved Hide resolved
config/converter.go Outdated Show resolved Hide resolved
@applejag applejag requested a review from jmattheis January 30, 2025 16:57
@@ -0,0 +1,43 @@
input:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if pkg, ok := g.lookupAbsDir[absDir]; ok {
return pkg.Name, pkg.PkgPath, nil
}
// Skipping build tags as they're not used when only using packages.NeedName
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should use the build tags, because this can produce some flaky edge-cases which should happen less with the build tags.

  • someone uses goverter gen with specified output:package
  • has the file locally
  • removes local output:package
  • goverter gen will still use the old package name
  • another uses checks out the repo, and the generated file would get a different package.

It doesn't feel right, when the generated file can affect the goverter generation.

if !filepath.IsAbs(c.OutputFile) {
absOutputDir = filepath.Join(
filepath.Dir(rawConverter.FileName),
filepath.Dir(c.OutputFile))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output:file may include @cwd to reference the current working directory. this has to be added here too.

lookup: map[string]*packages.Package{},
locals: map[string]map[string]method.LocalOpts{},
lookupPkgPath: map[string]*packages.Package{},
lookupAbsDir: map[string]*packages.Package{},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it took a little longer to review, I had the idea in mind to reuse the existing package loading for this feature, as it is quite similar. There is already this feature to have relative paths in goverter:extend https://github.com/jmattheis/goverter/blob/main/scenario/extend_external_relative.yml, and the package detection could work similarly by detecting a relative path, and joining it with the conversion package path.

It had some rough edges but the end result looks good IMO what do you think? main...alternative this only loads the package for the package name and constructs the package path via path.join.

There is one thing that's not supported, getting the package name of a package outside of the go module goverter is executed in. But IMO this isn't bad, as the normal go tooling works the same. e.g.

$ cd goverter
$ go run ../../gotify/server
directory ../../gotify/server outside main module or its selected dependencies

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 this pull request may close these issues.

Auto set package name with goverter:output:file
2 participants