Repository navigation
Closed
Description
A current pain point of our SDKs is that any time you want to write your own util that's not in our API, it has to "break the chain".
e.g.
func AddAFewMounts(c *dagger.Container) *dagger.Container {
return c.
WithMountedDirectory("/foo", c.Host().Directory("foo").
WithMountedDirectory("/bar", c.Host().Directory("bar")
}
func main() {
ctr := Container().
From("alpine").
WithWorkdir("/foo")
ctr = AddAFewMounts(ctr) // The chain is broken!!!!!!
output, err := ctr.Exec("blah").Stdout().Contents()
...
}This isn't a huge deal but can get more annoying the more modular utils you want to write.
Instead, we could have our codegen clients include a .With(...) method on chainable objects that could be used like this:
func AddAFewMounts(c *dagger.Container) *dagger.Container {
return c.
WithMountedDirectory("/foo", c.Host().Directory("foo").
WithMountedDirectory("/bar", c.Host().Directory("bar")
}
func main() {
output, err := Container().
From("alpine").
WithWorkdir("/foo").
With(AddAFewMounts).
Exec("blah").Stdout().Contents()
...
}In Go, With could have signatures like:
func (c *Container) With(f func(*Container)) *Container {
return f(c)
}
...
func (d *Directory) With(f func(*Directory)) *Directory {
return f(d)
}I imagine similar signatures could be generated for our other SDKs (?)
I think this would be a small-to-medium size improvement to DX and may be an interesting place for future extensions to integrate (maybe).
### Todos
- [x] https://github.com/dagger/dagger/pull/4332
- [x] https://github.com/dagger/dagger/pull/4898
- [x] https://github.com/dagger/dagger/pull/4922
- [ ] Apply to a subset of types [selectively](https://github.com/dagger/dagger/issues/3885#issuecomment-1502081027)
- [ ] Standardize *docstrings*
- [ ] Maybe standardize tests
- [ ] https://github.com/dagger/dagger/issues/5236
Reacted by Nipuna Perera, Andrea Luzzardi, Helder Correia, Alex Suraci, Joel Longtine and Marcos Nils
Metadata
Metadata
Assignees
Labels
No labels
Activity
dolanor commentedon Nov 17, 2022
I think I like this proposal!
helderco commentedon Nov 17, 2022
I like that. I want to experiment with that pattern.
aluzzardi commentedon Nov 17, 2022
I like it!
Maybe it's too complex, but a more powerful thing would be to follow the
http.Handlermiddleware pattern and return a callback. This would allow to parameterize theWith()statement:NOTE: This is just a pattern, doesn't require any changes from original proposal
helderco commentedon Jan 5, 2023
Just saw #4173 and immediately thought of this.
Before
After
slumbering commentedon Jan 6, 2023
Is the
With()implemented in Python @helderco ?helderco commentedon Jan 6, 2023
No, but I want to 😍
slumbering commentedon Jan 6, 2023
Yes me too 😍
slumbering commentedon Jan 9, 2023
PR opened for Node.js SDK:
with()method to chain objects #4332helderco commentedon Feb 13, 2023
Looking at this and I see two changes.
Need to pass the client
When you call
c.Host()..., you mean to call this on the client (i.e., rootQuery), notContainer. So I think these functions need 2 arguments: the current instance and the client.Why not return other types?
Instead of only returning the same type, why not allow any type to be returned? For example,
Container.Directoryreturns adagger.Directory, this would allow more flexibility.This can be solved with generics. Here's Python's implementation, doesn't even have to be codegen'ed:
EDIT: disregard this question. The concept of
Withmethods mean it's expected to return the same type, just with some additions.11 remaining items
sipsma commentedon Apr 6, 2023
Depends on exactly what you mean, especially in terms of "generate native chain steps".
If you just mean being able to generate an SDK that has the util baked in like
ctr.AddAFewMounts(), then yes that would be totally possible. This would actually solve an open question with the current extension implementation where support for extending types from core or other extensions isn't possible, you can only define your own types+methods (due to restrictions in the type systems of languages like Go). Our solution to supportingWithutils here would be our solution to that problem too.If by "generate native chain steps", you mean that we'd be able to convert the actual implementation of a util func from one language to another, then no I don't think that's feasible in general. If we arbitrarily limited the utils to be very simple chains of method calls, then maybe, but that would rule out using loops, conditionals, libraries, etc. That would feel like a big restriction without much benefit.
shykes commentedon Apr 6, 2023
Yes I meant that one :)
Yup, I agree - that's a non-goal.
TomChv commentedon Apr 6, 2023
I'm not sure I understand what you mean. I added additional patterns in #4907 to verify it works according to #4898 tests.
helderco commentedon Apr 6, 2023
I mean this:
dagger/codegen/generator/nodejs/templates/src/object.ts.gtpl
Lines 42 to 46 in ff7653d
I suggest two examples: a simple one without any external dependencies, and a second one with the example above but using a factory function.
There's also the following test:
dagger/sdk/nodejs/api/test/api.spec.ts
Lines 220 to 224 in ff7653d
helderco commentedon Apr 10, 2023
NodeJS has an implementation and Go implementation has been submitted, so I'm going to make a quick Python implementation as well.
Self-assigning to push this forward and syncing between SDKs.
helderco commentedon Apr 10, 2023
SDK implementations
with()method to chain objects #4332with_(…)convenience #4922Drift: apply to which objects?
NodeJS first implemented using codegen, on all object types. However, in the Go implementation, @jlongtine is applying only to
ContainerandDirectory. He raised the question "What do you imagine doing withfunc (c *CacheVolume).With(...) *CacheVolume? He has a point 🤔However, I prefer either applying to all types or finding some rule to be more selective, but I'd rather not manually hardcode which ones to select. Otherwise, if a new type comes along where this could be useful, someone has to remember to add that type to the list.
I submitted a proposal to return simple objects in codegen. That requires codegen to tell the difference between a plain object (e.g.,
EnvVariable,Port,Label) and a normal object type likeContainerandDirectory. If in addition we excludeID()and deprecated fields, selecting only the non-empty objects that are left, already excludesEnvVariable,Port,Label,CacheVolume,Socket, etc.So the Python implementation is not using codegen (for now), but has a general implementation in the base type. If the return simple objects proposal gets accepted, that implementation will automatically change the base class for these objects, thus not including
with_()in these cases.In the meantime, we can also do a follow up to implement this detection in all SDKs, fixing this drift, while also solving the first step of that proposal. I don’t think it’s necessary to hold on merging the current PRs waiting for this though.
helderco commentedon Apr 12, 2023
All SDKs have an implementation, but there’s a slight drift:
After these tasks, this issue can be closed.
tests: add additional test on chain operations
tests: add additional test on chain operations (#4907)
Generate "with" method helper to support chainable utils (see dagger/…
helderco commentedon Jul 11, 2023
I found a better rule for selectively choosing which types to add this method: object types that have at least one field that returns the current type. I'll submit a PR soon that'll close this issue.