Skip to content

Modules: Support for interfaces #6213

Closed

Description

@sipsma

(Just realized I never made a public github issue for this, creating now for some preliminary discussion as I've started working on the implementation)

As part of dagger/dagger#6102 (comment) we realized that support for interfaces will enable some important use cases that would otherwise require opening up the gates to dependency version hell (see the discussion on the linked PR for more details on the dependency hell problem). Desire for interface support has also come up quite a bit previously in other contexts.

Examples of usefulness:

  1. Modules can accept types from other modules without needing an explicit dependency on them (or even awareness of them at all)
  2. We have quite a few repetitive APIs in core such as export and publish that are all defined on different types; giving clients the ability to be agnostic of the underlying type may be beneficial to both user code and even our own internal code (such as the CLI).
  3. Our existing requirements around what an SDK module is (a module that has ModuleRuntime and Codegen functions w/ certain signatures) could be formalized and enforced more gracefully
  4. Interfaces may give routes to mocking out objects for the purposes of testing (though this idea in particular is still in the "vague" stage)

Here's a toy example of what should become valid code with this support in place (using Go, but in general is language agnostic):

Mallard module (no deps):

type Mallard struct{}
 
func (Mallard) Quack() string {
 return "mallard quack"
}

Crested module (no deps):

type Crested struct{}
 
func (Crested) Quack() string {
 return "crested quack"
}

Pond module (no deps):

type Pond struct {
 Ducks []Duck
}
 
type Duck interface {
 Quack(context.Context) (string, error)
}
 
func (m Pond) WithDuck(duck Duck) Pond {
 m.Ducks = append(m.Ducks, duck)
 return m
}
 
func (m *Pond) QuackAll(ctx context.Context) (string, error) {
 var quacks []string
 for _, duck := range m.Ducks {
  quack, err := duck.Quack(ctx)
  if err != nil {
   return "", err
  }
  quacks = append(quacks, quack)
 }
 return strings.Join(quacks, "\n"), nil
}

Caller module (deps on Mallard, Crested and Pond):

type Caller struct{}
 
func (m * Caller) Test(ctx context.Context) (string, error) {
 return dag.Pond().
  WithDuck(dag.Mallard()).
  WithDuck(dag.Crested()).
  QuackAll(ctx)
}

Activity

  1. self-assigned this
    on Dec 4, 2023
  2. sipsma commented on Dec 4, 2023

    @sipsma
    ContributorAuthor

    One tangential issue that will likely come out of this is that we may want to (backwards-compatibly) adjust how opts are accepted in the Go SDK.

    The problem is that right now non-required function args get grouped into a concrete struct *Opts.

    I think we probably will want to change that from a concrete struct to an interface definition. We will still generate the concrete *Opts struct too but it will just implement the interface that is accepted. This should be a backwards compatible change: https://go.dev/play/p/KNquXNEuuyx

    If we don't do that, it will be impossible to define an interface that accepts a object with signatures including non-required args.

    This idea is still incomplete though; we need to also ensure that the interface accepted for Opts doesn't itself include the concrete struct or anything like that. One option is to just use any, which is meh but may be okay. But it's worth more thought; I haven't invested a ton of time on this path yet, just wanted to call it out early. There's almost certainly more routes available to solving this problem too, but will cross that bridge when we get there.

  3. sipsma commented on Dec 4, 2023

    @sipsma
    ContributorAuthor

    Another thing that came up in previous discussion was whether this means we're ruling out languages that don't support interfaces (or otherwise are an awkward fit).

    I actually don't think that should be too hard to deal with. SDKs for a given language can also implement equivalent support for this by only using concrete types. The basic outline would be:

    1. For a interface definition, generate a concrete type for that interface
    2. Give users an explicit mechanism for converting arbitrary types to that concrete interface object (which errors out if the input type doesn't actually implement it)

    For SDKs that do support notions of interfaces (and equivalent duck-typing concepts), that's essentially what they'll be doing underneath the hood anyways. So the only difference is that languages w/out an interface-like concept will require their users be a bit more explicit about conversions and have to rely slightly more on runtime type checking than compile time. But that seems like a perfectly fair tradeoff.

  4. helderco commented on Jan 5, 2024

    @helderco
    Contributor

    Sorry for unintended sync issue with Linear.

  5. added
    kind/consistency
    and removed
    kind/consistency
    on Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/sdkkind/consistency

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions