Skip to content

Modules: support for constructors #6132

Closed

Description

@sipsma

Right now Module authors define a top-level object for their module, can include fields in it, and can attach functions to it, but they can't define any arguments for constructing the module object itself.

This is not a limitation of the underlying graphql API; we create a field under Query for the module but we don't currently give authors any way of setting arguments on it. This creates some "impedance mismatch" since other objects in the module can implicitly have constructors since they get returned by functions. It also results in awkward APIs + code that should be much simpler.

e.g. right now in Go you have to write something like:

type CoolMod struct {
   Foo Optional[string]
   Bar Optional[string]
}

func (m CoolMod) WithFoo(foo string) CoolMod {
  m.Foo = Opt(foo)
  return m
}

func (m CoolMod) WithBar(bar string) CoolMod {
  m.Bar = Opt(bar)
  return m
}

func (m CoolMod) SayHi() string {
   return "Hi: " + foo.GetOr("defaultfoo") + bar.GetOr("defaultbar")
}

Which results in CLI users making calls like:

dagger call -m coolmod with-foo --foo=fooval with-bar --bar=barval say-hi

Instead, authors should be able to write something like this in Go:

func New(foo Optional[string], bar Optional[string]) CoolMod {
  return CoolMod{Foo: foo.GetOr("defaultfoo"), Bar: bar.GetOr("defaultbar")}
}

type CoolMod struct {
   Foo string
   Bar string
}

func (m CoolMod) SayHi() string {
   return "Hi: " + foo + bar
}

Which results in CLI users making calls like:

dagger call -m coolmod --foo=fooval --bar=barval say-hi

Go doesn't have formal constructors, but according to Effective Go the New func pattern is probably as close to a standard as exists, so seems reasonable to use here.

For Python and other languages that actually have constructors as built-in notions, we should obviously just use those.


To be clear, this is not intended to solve every case of awkwardness around the with* style APIs being used in the CLI or other general DX warts. It, however, will avoid quite a few of them in common cases where they are arising right now and in general is just a useful+straightforward feature to have.

cc @helderco @vito @shykes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions