-
Notifications
You must be signed in to change notification settings - Fork 884
Fix complex packages import paths #463
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
Conversation
Regex expressions to match the typ ename and package name. Credits for the regex exp goes to /u/Lee_Dailey on Reddit.
I now noticed that the same author (guregu) has another package with the import path This import path, with the implementation in this PR will fail, given the go_type
New idea to address the issueI am prone to believe that a foolproof approach could be to modify the config file structure adding separate fields for the import path and the type name so that we don't have to do any kind of guess work on how the package name is structured and how the type names are called. If @kyleconroy can chime in I might attempt to implement this approach. However, being it a bit of an overhaul on several layers of the package I'd prefer to settle down with an accepted line of work before commencing anything. |
I'm going to need to think about the way forward a bit, so I suggest you pause work on the PR. The issue is that we're trying to parse three parts of information from a single string: the import path, the package name, and the type identifier. I think you're correct that we need to use a struct for this instead of a string. I also want to solve #177 at the same time. My current proposal looks like this: overrides:
- go_type:
import: "gopkg.in/guregu/null.v3"
package: "null"
type: "String"
db_type: "string"
"null": true |
Having explicit declaration of the three entries surely pinpoints what the user wants and helps in generating proper code. However, as of now, I'm not sure #177 could be solved by such approach, if I understood correctly the issue some kind of additional entry would be needed to mark the override to not be imported. Nonetheless seems to me that the structure you proposed is a good base starting point. #413 on the other hand seems to me more easily addressable, if the config gets changed it could be an occasion to rethink the naming of the entry Since the null entry defaults to false I personally find that Here the same struct with the null field addressed. overrides:
- go_type:
import: "gopkg.in/guregu/null.v3"
package: "null"
type: "String"
db_type: "string"
is_null: true Overall, from my understanding of the package, addressing #255 and #413 at the same time is more easily done in this PR. #177 requires more brainstorming beyond the config struct itself. |
I think this solves #177 quite well. You just leave out the import and package declarations overrides:
- go_type:
type: "State"
column: "job.state" |
This surely needs to be addressed but I fear to make mess in the codebase
Took me long enough, eventually all tests passed 🥱😪, I couldn't stand leaving PR a step away from completeness. I eventually spun up a CT on my server to build and test the PR and can confirm that the PR works as expected you can check the authors example in Awaiting a comment and review @kyleconroy Recap of the changes madeNew format of the YAML configuration file to use (fields structure and nesting is the same for JSON)Note that this change also brings the replacement of the overrides:
- go_type:
import: "gopkg.in/guregu/null.v3"
package: "null"
type: "String"
db_type: "string"
is_null: true This is reflected by the new struct Renamed
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in a few comments, we can't change the type of the go_type
parameter in a backwards-incompatible way. I'm happy to take over the PR from here if you don't want to implement backwards compatibility. Let me know.
// name of the golang type to use, e.g. `github.com/segmentio/ksuid.KSUID` | ||
GoType string `json:"go_type" yaml:"go_type"` | ||
// Import path, package name and type name as you would type them in the IDE | ||
GoTypeParam GoTypeParams `json:"go_type" yaml:"go_type"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a backwards incompatible change. The GoTypeParams
struct needs to work if go_type
is a string. This will require a custom UnmarshalJSON
method.
Not implemented because on L213 we first need to find how to differentiate from a builtin type and custom type. I've left a comment on an idea on how to approach it.
I didn't think about it but I stand corrected, it is indeed necessary.
I will happily hand over the PR to be taken care of! =) Would have loved to contribute more but at this point I wouldn't even know how to proceed to implement backwards compatibility and I believe my PR is already "sloppy" enough for the junior dev I am. It's a task it's sincerely best left to you. Checked the "Allow edits from maintainers" so you should be good to go. I'm also sorry for the tons of commits I made which undoubtedly pollute the commit history, I've been more prone to a "commit often, stay safe" but I'm sure that before a merge most of them can be squashed. |
Apparently the endttoend test suite only searches for, and support, JSON configs
changed the folder structure to be compliant like the all the others and changed the path and name tag as in the other endtoend folders
I clearly misunderstood how the endtoend test is performed
I modified the sqlc.json to not emit them but I forgot to regenerate the code for a proper comparison test
No need to apologize. We squash commits for PRs, so all your commits just turn into one :) |
Any update about on which milestone we can expect this feature to be implemented? |
I'm hoping for the next release, 1.5.0. I've been focused on some large architecture changes, so this fell to the wayside. Sorry for the delay. |
No troubles!
Just wanted an update :) glad to hear it might land in 1.5.0
Il lun 22 giu 2020, 17:28 Kyle Conroy <notifications@github.com> ha scritto:
… I'm hoping for the next release, 1.5.0. I've been focused on some large
architecture changes, so this fell to the wayside. Sorry for the delay.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#463 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA3WOCJZKF4YLTOSR27LJY3RX52CHANCNFSM4MR6SYHQ>
.
|
You could just load the import path with https://godoc.org/golang.org/x/tools/go/packages and see what the name of the package is (the source is going to be available at sqlc generate time). Or you could always explicitly name the import in the generated code, |
The code around this is a bit weird/neglected. This looks like it tries to massage the package name to strip common prefixes and suffixes, but then the carefully crafted I think it'd be a pretty easy refactor to discard all this logic and just explicitly name all non-stdlib imports. |
Sorry that this PR has languished. I'm heads down working on MySQL support. I promise this is next, but it's still going to be a while. |
It surely needs polishing or a complete rewrite using @tv42 suggested method. For what's worth I've been using my fork with these modifications since April without issues so far. |
Any update on this? |
Haven't heard anything from @kyleconroy but I honestly think the new proposed approach is waaaay smoother than my amateurish approach. I'd be glad to see a polished and merged into master PR to address the issue. This PR "just works" but I do recognise is far from clean code. Please do go ahead. |
EDIT
Please skip directly to this reply as none of what I made matters anymore since I found another import path case which fails with this current PR.
See #462 and #255
Attempting to fix the parsing of complex package names by leveraging regex capabilities to capture the relevant parts.
After smashing my head for an hour I asked for help on Reddit, details of the regex expression can be found here, credits for the regex go to /u/Lee_Dailey.
I've also added the relevant test entries which used to fail and now they pass.
Notes
I've left the original code as commented out for reference, it should be removed if the PR is to be accepted to avoid polluting the code with unnecessary lines.
The regex expression can be easily batch tested here to ensure it satisfies package import names, I am not aware of any other strange import path other than
gopkg.in/guregu/null.v3/zero
. To nitpick the issue we should find other packages with complex import paths. I can't guarantee that this will work with every package ever.Two assumptions on fully qualified names have been made to build the regex:
This led to formulate these two regex: