Repository navigation
Description
The pattern of writing modules that use With* functions to enable updates of object state in a builder-pattern/"fluent" style works pretty well for codegen clients, but looks clunky in the CLI.
@goller has a great example here: https://github.com/goller/daggerverse/blob/60619b38448b058743c52abb3dd3b94be1b1f1c0/flags/main.go#L3-L26
If you embrace the with* pattern your CLI calls look like dagger call with-flag-1 --flag 1 style-1.
But if you instead embrace just putting a bunch of args in a function call you get a nicer dagger call style-2 --flag-1 1. However that comes at the expense of being more clunky for codegen clients and module authors.
This issue is to discuss what we can do to improve this situation.
Some initial thoughts:
- The idea that's been brought up a few times of a "constructor" function of the top-level module object would not solve the general problem, but I think it would eliminate quite a few of the cases where it's popping up today, so feels relevant here.
So in Go if your module is named Foo you should be able to write something like this (strawman proposal):
func NewFoo(arg1 string, arg2 Optional[string]) *Foo {
return &Foo{Field1: arg1, Field2: arg2.GetOr("default")}
}
type Foo struct {
Field1 string
Field2 string
}
func (f *Foo) MyFunction(arg3 string) { ... }In Python we'd just use the standard __init__
Then invoking would look like:
- CLI:
dagger call -m github.com/foo/foo --arg1 arg1val --arg2 arg2val my-function --arg3 arg3val - Go:
dag.Foo("arg1val", FooOpts{Arg2: "arg2val"}).MyFunction("arg3val") - Python:
dag.foo("arg1val", arg2="arg2val").my_function("arg3val")
This at least eliminates the cases where users are writing With* functions just because there's no other way to set fields in the module root object right now.
- This is a half-baked thought at best, but I have been getting the sense that we may want to somehow formalize the concept of "setters".
We technically already generate getters for fields (more a result of how graphql works than anything), so I wonder if we should do something along the lines of autogenerating setters for fields too (while allowing users to override and provide their own if desired).
This would not only help reduce boilerplate code in modules, but I could imagine it giving a path to also allowing the CLI to turn clunky with-* subcommands into flags.
I need to sit down and think through this more, may totally break down in the details, but it feels like something could be there.
- Another half-baked thought: whatever the solution is here, it's going to impact/overlap w/ support for objects as args, which will be unlocked by feat: make module types id-able #6102
Activity
sipsma commentedon Nov 16, 2023
@shykes just reminded me of a discussion a while back about supporting execution of "language-native" one-liners, e.g. (strawman flag names):
Or if you want to open a full interactive interpreter we can re-use
dagger shellfunctionality. This would all work of course without actually needing the language toolchain on your local machine since we'd just be re-using module SDK functionality.This "solves" the problem by just giving a more expressive interface to calling dagger functionality than what we can manage to fit into standard "posix-y flags".
Not mutually exclusive with other fixes and something we'll likely want to do regardless.
helderco commentedon Nov 16, 2023
I've thought about the first constructor idea and I think that would be nice. 👍
Been wondering about something related to that but more like an SDK convenience. In #6117 I've created multiple object types that are modular and reusable, but they're not consumable as is externally because there's a difference in GraphQL between an object type and an input object type.
Problem is that I find myself duplicating in a top level function the constructor that the class already has:
https://github.com/dagger/dagger/blob/60bc4c985293bc45ac093ba37da47159e53244ab/sdk/python/dev/src/main/__init__.py#L118-L127
https://github.com/dagger/dagger/blob/60bc4c985293bc45ac093ba37da47159e53244ab/sdk/python/dev/src/main/docs.py#L7-L10
Which leads me to repeat not only the arguments but also their documentation and the object's, pushing me to find ways to reuse them:
https://github.com/dagger/dagger/blob/60bc4c985293bc45ac093ba37da47159e53244ab/sdk/python/dev/src/main/__init__.py#L65-L66
So I've been thinking about adding a convenience to somehow expose a function from the object's constructor automatically.
But that would extend to the main object for the model, which would make it easier to share state between multiple objects like the source code
Directory.sipsma commentedon Nov 20, 2023
@helderco I split the constructor idea specifically out to a separate issue here, just FYI: #6132
sipsma commentedon Mar 19, 2024
Since adding constructors I think we've avoided the worst of this issue and I'm not really convinced there's much more to do other than what's covered in other issues (support for inline code snippets, calling Functions from SDKs running on the host, etc.). So gonna close this out for now.