Skip to content

codegen: Selecting sibling fields #3577

Closed

Description

@helderco

I know that we're not yet optimizing for single queries by allowing to select multiple sibling fields but my brain keeps bringing it up and I'm fond of discussing possibilities here if anyone has ideas to add this feature.

Uncached Exec?

The common example that comes to mind for this is when you want to select both stdout and stderr from an exec, although there's a separate issue to attempt to simplify that:

In any case, those are two different scalars that need to both be selected. The current usage is to select the fields independently:

cmd := alpine.Exec(...)
stdout, err := cmd.Stdout().Contents(ctx)
if err != nil {
  return nil
}
stderr, err = cmd.Stderr().Contents(ctx)
if err != nil {
  return nil
}

Which causes two queries to execute. It's not a problem because Exec should be cached.

So far so good, but I keep remembering that in Europa (v0.2) there were a lot of times where it was perfectly valid to use always: true to skip the cache. Granted, in most cases it was about debugging and we may have better tools for that (e.g., dagger attach), but I seem to remember there were valid cases outside of this as well.

Useful to make a list if someone remembers some, to see how we can cover them in the new API.

Container.envVariables

Right now there's an issue with returning a list of EnvVariable:

@dolanor is going to take a look at that, the issue being that lists are executed automatically, just like scalars, but in this case it's a list of objects and we need to select fields from those.

The way I imagine it can be fixed is to track from the chain when it should return a list but still return the builder object (EnvVariable) as a single so only when unmarshaling the result to to the right thing:

vars, err := ctr.EnvVariables().Name(ctx)
// vars being []string{"VAR1", "VAR2"}

First of all, Name() here needs to return the right type ([]string instead of string).

Secondly, what if you need both Name and Value? You can select all fields from an object automatically if you return a list of objects but if the object is something more complex like File you may be requesting too much very easily. And if not for this you'd have two lists (one for Name and another for Value) that you'd need to combine manually if you want the pair.

Lastly, it may just be easier, in this case, to return instead a list of strings with pair of values: []string{"VAR1=one", "VAR2=two"} but we may end up with an unavoidable situation like this.

Other cases

I haven't looked more closely but even if not for the above there may be other cases where it's useful to select siblings, even if in the future. Just something to think about 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions