Skip to content

Go SDK: Pass objects by reference instead of ID - #3598

Merged
aluzzardi merged 1 commit into
dagger:mainfrom
aluzzardi:go-sdk-pass-by-object
Nov 1, 2022
Merged

aluzzardi merged 1 commit into
dagger:mainfrom
aluzzardi:go-sdk-pass-by-object

Conversation

@aluzzardi

Copy link
Copy Markdown
Contributor

Partial implementation of #3558. Example:

Before:

modules := c.Directory()
for _, f := range []string{"go.mod", "go.sum", "sdk/go/go.mod", "sdk/go/go.sum"} {
	fileID, err := workdir.File(f).ID(ctx)
	if err != nil {
		return err
	}

	modules = modules.WithCopiedFile(f, fileID)
}

After:

modules := c.Directory()
for _, f := range []string{"go.mod", "go.sum", "sdk/go/go.mod", "sdk/go/go.sum"} {
	modules = modules.WithCopiedFile(f, workdir.File(f))
}

See our changes to magefile.go and various tests for other examples.

Caveats

  • Some arguments are named id in the API (e.g. id FileID), but they're not IDs in the SDK (e.g. id *File)
  • Currently it doesn't work on optional fields, because they're structures. I don't know yet how to get around that without recursive reflection (blargh)
  • With everything is generic and would work with any ID, however I've had to hardcode a few things to work around these limitations:
    • Top-level selectors (e.g. File(id FileID)) become weird (File(id *File)) -- if you already have the file, what's the point of having a function to look it up? I've kept them as File(id FileID)
    • I don't have a good heuristic to go from DirectoryID to Directory (stripping the ID is not enough, for instance it's CacheID -> CacheVolume), so the conversion map is hardcoded
    • We could potentially analyze the schema and look for objects that have an id field pointing to a scalar -- and recording that as scalar->object conversion

@aluzzardi

Copy link
Copy Markdown
Contributor Author

btw -- @sipsma I was able to use a common mechanism in XDX (used when we return e.g. a *Container from an extension rather than a ContainerID).

This could be adapted so it also works with XDX arguments (e.g. take a *Directory instead of a DirectoryID).

@aluzzardi
aluzzardi force-pushed the go-sdk-pass-by-object branch 3 times, most recently from d1fd478 to fe16963 Compare October 31, 2022 19:41

@sipsma sipsma left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it doesn't work on optional fields, because they're structures. I don't know yet how to get around that without recursive reflection (blargh)

That's the main thing that stuck out to me. I feel like this is worth fixing before we release it, otherwise there's still a huge gotcha.

I can't think of anything besides recursive reflection either off the top of my head.

We could potentially analyze the schema and look for objects that have an id field pointing to a scalar -- and recording that as scalar->object conversion

Yeah, so I guess this makes "id" a very special field to have. I think I'm fine with that though. The only other thing would be a directive or something that let you manually create the association. But in the future we won't want to expose that to code-first extensions, so at that time we may very well end up making "id" a special field anyways.


All that being said, this is amazing! Such a huge improvement from a relatively straightforward modification. I also love that when you inline queries as args to other queries you can start to actually see the structure of the DAG in your code 😎

@aluzzardi

Copy link
Copy Markdown
Contributor Author

That's the main thing that stuck out to me. I feel like this is worth fixing before we release it, otherwise there's still a huge gotcha.

I could potentially do this in MarshalGQL, but the downside is we lose parallelism (in this PR, all IDs are looked up at once in parallel whereas with more "dynamic" MarshalGQL it would be on the fly, sequentially).

I'll try to find a workaround.

Note: Initially I thought this could be a problem for later since there's no function that takes an optional ID today. However, that is going to change with #3119 (platformVariants), which is a tricky one (optional array of ContainerID -- definitely need recursive reflection for that one).

Yeah, so I guess this makes "id" a very special field to have. I think I'm fine with that though. The only other thing would be a directive or something that let you manually create the association. But in the future we won't want to expose that to code-first extensions, so at that time we may very well end up making "id" a special field anyways.

Yeah, we might have to rethink that with extensions.

The rule of thumb seemed to make sense: If you have a scalar named FooID and a type named Foo with an id type of FooID -- then there's an association. Can't think of a case where this fails, but I'm sure extensions will bring questions here.

@sipsma

sipsma commented Oct 31, 2022

Copy link
Copy Markdown
Contributor

no function that takes an optional ID today

The other existing one I noticed was WithMountedCache that optionally accepts a directory ID

@aluzzardi
aluzzardi force-pushed the go-sdk-pass-by-object branch 5 times, most recently from f22e71d to 55a838d Compare November 1, 2022 02:58

@vito vito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍 This is awesome.

Comment thread core/integration/container_test.go Resolved
Comment thread core/integration/platform_test.go Resolved
Comment thread core/integration/platform_test.go Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing these will show up in the SDK godoc now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, yes :/

Problem stems from these being defined in the root package (e.g. api.gen.go) and then used in internal/querybuilder -- can't use private functions there.

Unless there's a way out?

I've used the (crappy) XX convention to somewhat make it explicit in code completion that they're not to be toyed with.

Perhaps we should use gRPC convention (XXX_Whatever) or make them part of the public API by having more generic name/meaning

@vito vito Nov 1, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm yeah unfortunate but not a dealbreaker, makes sense why it's needed.

The only idea I have is to move it to an embedded type, which I think will keep it from showing up in the godoc (and MAYBE we can even make the type private...?), but it has a dependency on r for r.ID so that won't help. Unless it just has its own ID(ctx) implementation somehow.

edit: Also it might not even hide it from the godoc. Not sure. 🙃

Failing that it might be worth following the gRPC/protobuf convention, just because, but not a blocker.

@aluzzardi

Copy link
Copy Markdown
Contributor Author

@sipsma @vito @helderco PTAL :)

I just pushed a nasty workaround (separate commit) to keep those scalar based (e.g. File(id FileID))

Not proud of it, but it is what it is. Wondering if we're going to get rid of those ID-based selectors altogether in the future

At least for now nothing breaks (including netlify's secret lookup) -- I think we need to address that (secret management) and hiding IDs in XDX before we can remove top-level ID lookups (otherwise it's impossible for an extension to take a DirectoryID as input -- there's no way to retrieve the underlying Directory back, and there's currently no way to take Directory as input directly)

@aluzzardi
aluzzardi force-pushed the go-sdk-pass-by-object branch 2 times, most recently from 6dcd949 to 7ffebe9 Compare November 1, 2022 19:07
Comment thread sdk/go/internal/querybuilder/marshal.go Outdated

@sipsma sipsma Nov 1, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using reflect.Value as a map key makes me slightly nervous since the docs say "To compare two Values, compare the results of the Interface method. Using == on two Values does not compare the underlying values they represent." (though the exact meaning of that statement is ambiguous): https://pkg.go.dev/reflect#Value

My fear would be that since there's an unsafe.Pointer in the Value struct then we could have code that works most of the time but randomly doesn't cause the go runtime moved a value around in memory or weird stuff like that.

This is all completely hypothetical though, easily could be a non-issue. If there's a really easy way to avoid needing to cache this beforehand in this map, then maybe it's worth it. Otherwise maybe just a comment here would be useful in case we hit a weird bug someday and are looking over this code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% -- I feel really weird about this.

The alternative would be to make a key somehow -- but it gets messy, fast (array, structs, etc).

The simplest way though is to just get rid of this and do things sequentially.

query builder is already parallelizing arguments -- this second parallelization is at the argument level (e.g. run a container array in parallel, for instance the one you added on Publish)

Maybe it's overkill though and I can just remove this? WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'll try something else, should be quick.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah if it's easy to just remove that extra parallelization for now that seems like the right approach. We can easily re-add it in the future if needed and get a better understanding of the reflect/unsafe stuff.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

I found a way to keep the parallelism, get rid of the map key AND simplify the code! It was over engineered to begin with.

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment about possible reflect weirdness, but I'm okay if we just leave a code comment there for now. Otherwise LGTM!

@aluzzardi
aluzzardi force-pushed the go-sdk-pass-by-object branch from 7ffebe9 to e3997b2 Compare November 1, 2022 23:36
@aluzzardi
aluzzardi force-pushed the go-sdk-pass-by-object branch from e3997b2 to 8b7d944 Compare November 1, 2022 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants