-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor import-loading to simplify the type-generation code
genqlient has some code (`imports.go`) dedicated to tracking which imports we need and avoiding conflicts, as well as converting a (restricted) Go expression like `map[string]github.com/me/mypkg.MyType` to an import (`github.com/me/mypkg`) and a type-reference (`map[string]mypkg.MyType`) to be used in the context of that import, and at least making some attempt to track conflicts. (Right now the conflict-avoidance is not very smart, and not very well tested, but it comes up rarely anyway.) Sadly, that code was a bit cumbersome to use, because you had to first register the imports (typically from `convert.go`), then use them (often from the template). In this commit I refactor the order we write things in order to allow a significant simplification of how we import; in particular we no longer have to guess in advance what imports which template will need; it can just do `{{ref <expr>}}` as before, and it just works. To do this, I: - changed the importer to have only one API, which adds the import if needed, and returns the reference either way - added a check that we don't add imports after they're written - reorganized the toplevel templates a bit to make sure that check never fires; we now generate all the types and operations, then write the imports and glue it all together This removes a bunch of silly code, and should simplify the process of adding custom (un)marshalers (#38). While I was at it, I put the documentation of what expressions we support in a more visible place, and added a type-assertion that your custom context type implements context.Context (if applicable). Test plan: make check Reviewers: marksandstrom, mahtab, jvoll, adam, miguel, steve
- Loading branch information
1 parent
50b2f56
commit 2297e02
Showing
9 changed files
with
132 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package {{.Config.Package}} | ||
|
||
// Code generated by github.com/Khan/genqlient, DO NOT EDIT. | ||
|
||
{{.Imports}} | ||
|
||
{{if and (ne .Config.ContextType "-") (ne .Config.ContextType "context.Context") }} | ||
// Check that context_type from genqlient.yaml implements context.Context. | ||
var _ {{ref "context.Context"}} = ({{ref .Config.ContextType}})(nil) | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.