Skip to content

Support for constructors in modules - #6135

Merged
sipsma merged 9 commits into
dagger:mainfrom
sipsma:constructor
Nov 22, 2023
Merged

sipsma merged 9 commits into
dagger:mainfrom
sipsma:constructor

Conversation

@sipsma

@sipsma sipsma commented Nov 20, 2023 •

Copy link
Copy Markdown
Contributor

Closes #6132

package main

func New(foo Optional[string]) *Test {
	return &Test{
		Foo: foo.GetOr("defaultfoo"),
	}
}

type Test struct {
	Foo string
}

func (m *Test) Echo() string {
	return m.Foo
}

dagger call echo on that module now returns defaultfoo and dagger call --foo=abc echo now returns abc.

In general constructors can accept the same kinds of arguments as any other function, e.g. this is also valid:

package main

import (
	"context"
)

func New(
	ctx context.Context,
	foo Optional[string],
	bar Optional[[]int],
) (*Test, error) {
	bs, err := dag.Container().From("alpine:3.18").File("/etc/alpine-release").Contents(ctx)
	if err != nil {
		return nil, err
	}

	return &Test{
		Foo:  foo.GetOr("defaultfoo"),
		Bar:  bar.GetOr([]int{1, 2, 3}),
		Blah: string(bs),
	}, nil
}

type Test struct {
	Foo  string
	Bar  []int
	Blah string
}

Overall idea:

  1. WithConstructor is an api on TypeDef
    • Currently, it's only allowed to call this on the "main module object" (checked in server-side code)
    • I put the api on TypeDef rather than Module though because I want to leave open the option to allow this on other objects in the future. If we ever support the notion of importing selected objects from other modules (rather than just all or nothing) then this could make sense. If we never go down that route, no harm done.
  2. For the internal SDK interface, invoking a constructor is done by setting the ParentName to the object and leaving the FunctionName blank.
    • I don't absolutely love this, but it feels reasonable enough. Open to other suggestions though.

TODOs:

  • Support the constructor args in the CLI
  • Support constructor in Python
  • General cleanup + more case handling (lots of copying chunks of code, skipping cases, etc. right now)
    • (memo for self) Important case to handle is no functions on object; only fields. This should be possible now and actually results in meaningful use cases ("constructor-only")
  • Integ tests

@sipsma
sipsma requested review from helderco and vito November 20, 2023 23:47
@sipsma

sipsma commented Nov 21, 2023 •

Copy link
Copy Markdown
Contributor Author

@helderco the updates needed to our cobra setup are not incredibly obvious; I've poked around a bit but wondering if you have any pointers before I delve deeper.

Basically, we need to add module-specific flags above any subcommands, e.g. dagger call --foo=bar echo.

If I add those flags here then I can get them to show up in the output of dagger call --help:

sipsma@dagger_dev:~/repo/github.com/sipsma/daggerverse/test$ dagger call --help 
✔ dagger call [0.00s]
┃ Call a module function and print the result.                                                                                                                     
┃                                                                                                                                                                  
┃ On a container, the stdout will be returned. On a directory, the list of entries, and on a file, its contents.                                                   
┃                                                                                                                                                                  
┃ Usage:                                                                                                                                                           
┃   dagger call [flags] [command [flags]]...                                                                                                                       
┃   dagger call [command]                                                                                                                                          
┃                                                                                                                                                                  
┃ Available Commands:                                                                                                                                              
┃   echo                                                                                                                                                           
┃   foo                                                                                                                                                            
┃                                                                                                                                                                  
┃ Flags:                                                                                                                                                           
┃       --focus        Only show output for focused commands. (default true)                                                                                       
┃       --foo string                                                                                                                                               
┃   -h, --help         help for call                                                                                                                               
┃   -m, --mod string   Path to dagger.json config file for the module or a directory containing that file. Either local path (e.g. "/path/to/some/dir") or a github
┃ epo (e.g. "github.com/dagger/dagger/path/to/some/subdir").                                                                                                       
┃                                                                                                                                                                  

But then when I try to actually use the flag I just get an error very quickly:

sipsma@dagger_dev:~/repo/github.com/sipsma/daggerverse/test$ dagger call --foo=yoyoyo echo
Error: unknown flag: --foo

I think the problem is that we parse flags in the PreRun here but we ideally shouldn't be parsing them until later now.

My default next path to try would be to refactor the withEngineAndTUI function to not be callback based but instead just return some closer so that we can initialize the engine and tui early in PreRunE and then keep it running in RunE. I think(?) that should allow us to load the main module object and related constructor flags early while not throwing a wrench in the ordering of cobra parsing/initialization we have setup very carefully.

But let me know if that makes sense to you too or if you see any better approaches 🙂

@helderco

Copy link
Copy Markdown
Contributor

@sipsma I think we can just re-parse the flags for call before --help.

@sipsma

sipsma commented Nov 21, 2023

Copy link
Copy Markdown
Contributor Author

@helderco okay eventually figured out the right incantation, the real key part was allowing unknown flags in this specific way here: 7fce686#diff-711069b715d2ad35d6eb28753227a571e815b68256d043a8d4337a9593c2a7daR265

Otherwise we hit a catch-22 where we can't parse global flags related to the TUI (i.e. --progress) until we've started the engine because there may be flags under call that are not yet known, but we can't know those flags until we've started the engine.

But allowing unknown flags there fixes it. We still re-parse later as you suggested so if anything is actually not recognized we will eventually error out, just a little bit down the line.

@sipsma

sipsma commented Nov 21, 2023

Copy link
Copy Markdown
Contributor Author

Not sure if this is a pre-existing issue, but just noticed while testing manually that if you have a broken module (syntax error, whatever) and try to run dagger call --help, you no longer get help output; you only get the error from the module.

I think in this situation it makes sense to show both; we can't show the full --help output since we can't load all the constructor flags, but we can at least show you the base --help output without the module specific subcommands/flags

Comment thread cmd/dagger/functions.go Resolved
Comment thread cmd/dagger/functions.go Resolved
Comment thread cmd/dagger/functions.go Resolved
Comment thread cmd/dagger/module.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could make some fragments in this query to make it easier to manage 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agree this is reaching the tipping point, but I'm actually not sure off the top of my head how you actually do that with our client... would we just include the fragments in this big blob and then reference them in the query? I have not yet touched fragments in graphql somehow, probably time to :-)

@helderco

Copy link
Copy Markdown
Contributor

This is noice, I plan on adding Python support tomorrow. 🙂

@helderco

Copy link
Copy Markdown
Contributor

For the internal SDK interface, invoking a constructor is done by setting the ParentName to the object and leaving the FunctionName blank.

  • I don't absolutely love this, but it feels reasonable enough. Open to other suggestions though.

Yeah, I agree. Feels reasonable, in line with empty ParentName for module registration. No immediate improvement comes to mind.

@sipsma

sipsma commented Nov 22, 2023

Copy link
Copy Markdown
Contributor Author

Not sure if this is a pre-existing issue, but just noticed while testing manually that if you have a broken module (syntax error, whatever) and try to run dagger call --help, you no longer get help output; you only get the error from the module.

I think in this situation it makes sense to show both; we can't show the full --help output since we can't load all the constructor flags, but we can at least show you the base --help output without the module specific subcommands/flags

Fixed this here: b746ecd

@sipsma sipsma changed the title WIP: support for constructors in modules Support for constructors in modules Nov 22, 2023
@sipsma
sipsma marked this pull request as ready for review November 22, 2023 03:09
@sipsma
sipsma requested review from a team as code owners November 22, 2023 03:09
This file used to just have Function in it a while back, but has since
expanded to be all typedefs; Function isn't even the "main point"
anymore.

Renaming to fix the confusion before it grows deeper.

Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
@helderco

Copy link
Copy Markdown
Contributor

I have a working Python implementation, just need to cleanup and fit the integration test next to Go. One of those cases with different module sources but same testing code.

@helderco helderco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wasn't able to finish Python today but I can do it in a follow up so this isn't blocked.

@sipsma

sipsma commented Nov 22, 2023

Copy link
Copy Markdown
Contributor Author

I wasn't able to finish Python today but I can do it in a follow up so this isn't blocked.

Oh no worries at all, totally fine as a follow up. Thank you so much for the help!

@sipsma
sipsma merged commit 14bf4a4 into dagger:main Nov 22, 2023
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* chore: rename function.graphqls to typedef.graphqls

This file used to just have Function in it a while back, but has since
expanded to be all typedefs; Function isn't even the "main point"
anymore.

Renaming to fix the confusion before it grows deeper.

Signed-off-by: Erik Sipsma <[email protected]>

* WIP: support for constructors in modules

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup go sdk todos

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup core todos

Signed-off-by: Erik Sipsma <[email protected]>

* add support for constructor flags to CLI

Signed-off-by: Erik Sipsma <[email protected]>

* add integ tests

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback around flag validation

Signed-off-by: Erik Sipsma <[email protected]>

* handle dagger call --help on broken module

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback: move flag re-parsing to common func

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Christian Schlatter <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* chore: rename function.graphqls to typedef.graphqls

This file used to just have Function in it a while back, but has since
expanded to be all typedefs; Function isn't even the "main point"
anymore.

Renaming to fix the confusion before it grows deeper.

Signed-off-by: Erik Sipsma <[email protected]>

* WIP: support for constructors in modules

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup go sdk todos

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup core todos

Signed-off-by: Erik Sipsma <[email protected]>

* add support for constructor flags to CLI

Signed-off-by: Erik Sipsma <[email protected]>

* add integ tests

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback around flag validation

Signed-off-by: Erik Sipsma <[email protected]>

* handle dagger call --help on broken module

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback: move flag re-parsing to common func

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* chore: rename function.graphqls to typedef.graphqls

This file used to just have Function in it a while back, but has since
expanded to be all typedefs; Function isn't even the "main point"
anymore.

Renaming to fix the confusion before it grows deeper.

Signed-off-by: Erik Sipsma <[email protected]>

* WIP: support for constructors in modules

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup go sdk todos

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup core todos

Signed-off-by: Erik Sipsma <[email protected]>

* add support for constructor flags to CLI

Signed-off-by: Erik Sipsma <[email protected]>

* add integ tests

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback around flag validation

Signed-off-by: Erik Sipsma <[email protected]>

* handle dagger call --help on broken module

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback: move flag re-parsing to common func

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* chore: rename function.graphqls to typedef.graphqls

This file used to just have Function in it a while back, but has since
expanded to be all typedefs; Function isn't even the "main point"
anymore.

Renaming to fix the confusion before it grows deeper.

Signed-off-by: Erik Sipsma <[email protected]>

* WIP: support for constructors in modules

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup go sdk todos

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup core todos

Signed-off-by: Erik Sipsma <[email protected]>

* add support for constructor flags to CLI

Signed-off-by: Erik Sipsma <[email protected]>

* add integ tests

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback around flag validation

Signed-off-by: Erik Sipsma <[email protected]>

* handle dagger call --help on broken module

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback: move flag re-parsing to common func

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* chore: rename function.graphqls to typedef.graphqls

This file used to just have Function in it a while back, but has since
expanded to be all typedefs; Function isn't even the "main point"
anymore.

Renaming to fix the confusion before it grows deeper.

Signed-off-by: Erik Sipsma <[email protected]>

* WIP: support for constructors in modules

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup go sdk todos

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup core todos

Signed-off-by: Erik Sipsma <[email protected]>

* add support for constructor flags to CLI

Signed-off-by: Erik Sipsma <[email protected]>

* add integ tests

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback around flag validation

Signed-off-by: Erik Sipsma <[email protected]>

* handle dagger call --help on broken module

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback: move flag re-parsing to common func

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* chore: rename function.graphqls to typedef.graphqls

This file used to just have Function in it a while back, but has since
expanded to be all typedefs; Function isn't even the "main point"
anymore.

Renaming to fix the confusion before it grows deeper.

Signed-off-by: Erik Sipsma <[email protected]>

* WIP: support for constructors in modules

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup go sdk todos

Signed-off-by: Erik Sipsma <[email protected]>

* cleanup core todos

Signed-off-by: Erik Sipsma <[email protected]>

* add support for constructor flags to CLI

Signed-off-by: Erik Sipsma <[email protected]>

* add integ tests

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback around flag validation

Signed-off-by: Erik Sipsma <[email protected]>

* handle dagger call --help on broken module

Signed-off-by: Erik Sipsma <[email protected]>

* address helder feedback: move flag re-parsing to common func

Signed-off-by: Erik Sipsma <[email protected]>

---------

Signed-off-by: Erik Sipsma <[email protected]>
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.

Modules: support for constructors

2 participants