Repository navigation
Go SDK: Query Builder - #3125
Conversation
9af193d to
f046a4a
Compare
✅ Deploy Preview for cloak-docs ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
d182c55 to
d1b0c12
Compare
Open Question: Argument PassingThis is a Go-specific issue since it doesn't support optional arguments (in contrast to TypeScript / Python, which do have optional arguments). These are the typical Go approaches to this problem:
dir.WithNewFile("/foo")
dir.WithNewFile("/foo", dagger.WithDirectoryWithNewFileContents("blah"))
dir.WithNewFile("/foo", nil)
dir.WithNewFile("/foo", &dagger.DirectoryWithNewFileOpts{Contents: "blah"})
dir.WithNewFile(dagger.DirectoryWithNewFileOpts{Name: "/foo"})
contents := "blah"
dir.WithNewFile(dagger.DirectoryWithNewFileOpts{Name: "/foo", Contents: &blah})
Where I'm leaningFind a GOOD solution for |
55987b1 to
cc52c17
Compare
Is this actually needed in practice? ie. is there any instance of
Another Con is that you need to pass
|
|
Question: how does this all work with extensions? Are they supported 1) completely, 2) partially, 3) not at all? |
|
for I'm gonna play with generics and interface to try to find a better way. And SUPER job btw. A way better experience already, for sure! |
Yes, it all works with extensions, semi-completely. Currently there's a whitelist to only generate the new core API (and not the old one, not extensions, etc). Only because there's still some edge cases I'm not handling yet, but it will be gone soon. Generation is designed so it can handle the unexpected. Basically, as long as an extension can load, it's 100% guaranteed code generation will work reliably, without name clashing, etc.
Yes because of the above (extensions). They could clash with core, but also among themselves. Basically, the name mangling right now is
|
cc52c17 to
05807e1
Compare
There was a problem hiding this comment.
@dolanor suggested we could use sub-packages
I like the way things end up looking with this, but based on past experience doing this with the older codegen it becomes annoying to have to import from a bunch of packages. The go automatic import guesser would often guess wrong and import from another nearby package that was generating similar code. Doesn't rule it out entirely but something to keep in mind.
I spent a bit and couldn't come up with anything else reasonable that felt natural. I'd probably lean towards Option 2 at this point honestly unless something else comes up. The need for nil is not that bad and it would perhaps be okay to make it optional with something like
func (r *Directory) WithNewFile(path string, options ...DirectoryWithNewFileOpts) *Directory {
// just apply the values set in the structs in options in order provided, though most of the time users will just have 0 or 1
}The variadic is a bit odd but it allows you to skip nil, has consistent logic behavior and retains type safety. So worth considering maybe.
I agree that it feels like there might be something really clever possible w/ generics or interfaces, but unless we can figure that out soon not worth blocking on IMO. This is just Go being Go, not worth fighting it too hard.
There was a problem hiding this comment.
nit: using the ast package to get the package name is probably only a few lines of code: https://pkg.go.dev/go/parser#ParseDir
Just to handle cases where dir name doesn't match package name. There's a mode for stopping after the package clause (PackageClauseOnly), so shouldn't slow things down. If it's harder than a couple of quick lines of code it's no worries at all though, not remotely a blocker
There was a problem hiding this comment.
Thanks, done!
I've still kept the old logic in there, but it first tries to check if a package name is already set.
Most of the time (I guess?) client gen will output into its own directory (e.g. -o api/api.gen.go) so there's no other Go code to parse.
There was a problem hiding this comment.
It's not a big deal at all right now, but I think it would probably result in simpler code in the long run if we can just use vektah/gqlparser to construct these graphql operation strings. I used Formatter.FormatSchemaDocument in the code-first stuff, I think you can use Formatter.FormatQueryDocument for this. Even though we don't need a whole document, from what I can tell briefly glancing you can use that for individual queries too.
If all the above is actually true (and it may not be), then I just suspect it will be easier to maintain and more robust relative to string construction. But if the string construction works that's all that matters for now :-)
There was a problem hiding this comment.
Awesome -- I just checked and I think it would work.
We could use it in func (s *Selection) Build() to construct the query.
MarshalGQL would still exist and be pretty similar to today (AFAIK gqlparser doesn't provide any Go->AST helper), but it would construct an ast.Value rather than a string.
Should've used it from the get go -- the reason it's this way is because I started with the graphb library, later hit a wall, and re-implemented parts of it which were using string construction.
Since we're a bit short in time, I filed #3332 so we can get this merged and get people using it, will clean up later.
Yes, and we also stopped doing that because of cross-package complications. For instance, It kinda worked (but gave us headaches) with an operations file since every operation was a full blown independent query, but with query building every field might be a cross-package reference.
Yep, that's where I'm at too. Basically: dir.WithNewFile("/foo")
dir.WithNewFile("/foo", dagger.DirectoryWithNewFileOpts{
Contents: "blah",
})I think that's the most Go idiomatic way that completely avoids name collisions and such. Contrast with the current implementation, for reference: dir.WithNewFile("/foo")
dir.WithNewFile("/foo", dagger.WithDirectoryWithNewFileContents("blah")) |
|
Implemented as a separate commit. Used @sipsma s suggestion -- no need to pass e.g: // this works, no `nil`
Exec()
// can pass the opts as a struct
Exec(dagger.ContainerExecOpts{
Args: []string{"ls"},
Stdin: "foo",
}
// this, although weird, also works
Exec(
dagger.ContainerExecOpts{
Args: []string{"ls"},
}, dagger.ContainerExecOpts{
Stdin: "foo",
},
) |
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
6ef9c99 to
ea45de3
Compare
Signed-off-by: Andrea Luzzardi <[email protected]>
|
@aluzzardi Will this mean we need to remember to |
|
@vito Yes, CI should be doing it! I was about to add that into a Makefile then figured it's a good opportunity to dogfood, should add that into a magefile or something :) |
Go SDK: Query Builder based Code Generation
operations.graphqlfilecore.Container().From("alpine").Exec([]string{"echo", "hello world"}).Stdout().Contents(ctx)Example