Skip to content

Zenith (Go SDK): allow omitting JSON types in structs - #6057

Merged
sipsma merged 8 commits into
dagger:mainfrom
jedevc:fix-json-struct-returns
Nov 14, 2023
Merged

sipsma merged 8 commits into
dagger:mainfrom
jedevc:fix-json-struct-returns

Conversation

@jedevc

@jedevc jedevc commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Fixes #5860.

Essentially, we configure the Go SDK to make sure that we always request the name of the field that we want the server to send/receive from. If we ask for field "A", we should get "A" and not the camel-cased "a". As a nice effect to do this, we need to add OriginalName support for fields. (@helderco I remember you saying something about this somewhere, but I couldn't find where you mentioned it, sorry).

This is still... not great (but it's an alright stop-gap). It fixes the temporary nastiness of needing these json tags, but we still have the issue that the user might still want to use JSON tags - it would be awesome if we could have our own dagger tags. For example, I need to maintain a manual mapping of structs in one of my modules to work around this, since I don't want to expose the GitHub API JSON into the Dagger API: https://github.com/jedevc/daggerverse/blob/main/github/main.go#L79-L106. Unfortunately, this requires a custom JSON marshaller, since I don't see an easy way to configure the tag we should use.

@jedevc jedevc added the zenith label Nov 3, 2023
@jedevc
jedevc requested review from helderco and sipsma November 3, 2023 15:58
Comment thread core/schema/module.go Outdated

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.

Just a little bug waiting to happen! 🪲

Thankfully, all it was potentially doing was giving an incorrect error message, the behavior wasn't affected.

@jedevc
jedevc force-pushed the fix-json-struct-returns branch from 6c40235 to 0b0eed4 Compare November 8, 2023 15:57
@jedevc

jedevc commented Nov 8, 2023

Copy link
Copy Markdown
Contributor Author

Ok, I pushed a couple extra commits to this of weird things I discovered while poking around working on ID-able types for modules (cc @vito).

It turns out you couldn't:

  • Nest non-idable types, so a module's type Foo wouldn't be allowed to contain Bar (but you could perfectly reasonably contain []Bar because of the next bullet point).
  • Return lists of idable types from a function in a module, we weren't handling unpacking lists of ids into lists of objects.

I've refactored and added the code and tests to do this into this PR (while not explicitly related, they're smaller fixes that make sense with the changes to how field resolvers work in this PR - I can split this up into smaller PRs if desired).

@jedevc
jedevc force-pushed the fix-json-struct-returns branch 3 times, most recently from 3b6d3f0 to 9e538dc Compare November 9, 2023 14:35

@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.

Nice! Went over the changes and tests, it all makes sense to me.

@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.

Only significant comments are on tests, otherwise looks good!

Unfortunately, this requires a custom JSON marshaller, since I don't see an easy way to configure the tag we should use.

Definitely not something to tackle here since this PR is just an incremental improvement, but in general I think we could probably support custom tags by generating MarshalJSON and UnmarshalJSON methods for the user's objects. I think that'd allow us to have our own struct tags but then just plug into the existing json marshaling for the most part.

Comment thread core/integration/module_test.go Resolved
Comment thread core/integration/module_test.go Resolved
Comment thread core/schema/module.go Outdated
Comment on lines 808 to 809

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.

nit: I think it might be a little safer to just change p.Info.FieldName to be field.OriginalName before passing to graphql.DefaultResolveFn, rather than asserting on the map type and changing it there.

Then we don't need an assumption about p.Source being map[string]any specifically. As far as I can think, that should always be the case today, but technically the graphql server lib allows it to be more than that and maybe we'd want to take advantage of that someday? It just feels like a nice assumption to remove if trivial.

If this is less easy than it sounds no worries, not a blocker.

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.

Yeahhh, this is entirely right and very trivial. I could have sworn I tried this and it didn't work - but it clearly does, so let's just use that.

Somehow this wasn't causing massive bugs? This meant we were getting the
wrong name/type in the resolver.

Signed-off-by: Justin Chadwell <[email protected]>
...if it's present. The server now correctly sends requests to the
module that match what it asks for (regardless of what casing the user
wanted). So we now need to ask for the right thing.

Signed-off-by: Justin Chadwell <[email protected]>
Previously this would just fail (but would allow nesting lists of
objects!). This was because the condition we were checking didn't take
into account non-idable object types (which is what's returned from
modules).

Also, this prevents the previously weird case of allowing a module to
declare it returns a directory and then return a *different* core type,
like a container, which would actually not error immediately.

Signed-off-by: Justin Chadwell <[email protected]>
This also applies to fields that are lists of core-idable types.

Essentially, this translates to returning a list of ids, which we need
to lift to a list of objects. The easiest way to do this is by having a
little helper func.

Signed-off-by: Justin Chadwell <[email protected]>
@sipsma
sipsma force-pushed the fix-json-struct-returns branch from 12524f3 to 1933d63 Compare November 14, 2023 23:38
@sipsma
sipsma merged commit e1d69ed into dagger:main Nov 14, 2023
@jedevc
jedevc deleted the fix-json-struct-returns branch November 15, 2023 06:10
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* fix: capture field value at start of loop

Somehow this wasn't causing massive bugs? This meant we were getting the
wrong name/type in the resolver.

Signed-off-by: Justin Chadwell <[email protected]>

* feat: allow OriginalName for fields

Signed-off-by: Justin Chadwell <[email protected]>

* fix: go codegen should request the json field name

...if it's present. The server now correctly sends requests to the
module that match what it asks for (regardless of what casing the user
wanted). So we now need to ask for the right thing.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add test for structs with json combinations

Signed-off-by: Justin Chadwell <[email protected]>

* test: fixup test to detect id instead of ID

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow nested structs from module functions

Previously this would just fail (but would allow nesting lists of
objects!). This was because the condition we were checking didn't take
into account non-idable object types (which is what's returned from
modules).

Also, this prevents the previously weird case of allowing a module to
declare it returns a directory and then return a *different* core type,
like a container, which would actually not error immediately.

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow returning lists of core-idable types

This also applies to fields that are lists of core-idable types.

Essentially, this translates to returning a list of ids, which we need
to lift to a list of objects. The easiest way to do this is by having a
little helper func.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add new test cases suggested in code review

Signed-off-by: Justin Chadwell <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Christian Schlatter <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* fix: capture field value at start of loop

Somehow this wasn't causing massive bugs? This meant we were getting the
wrong name/type in the resolver.

Signed-off-by: Justin Chadwell <[email protected]>

* feat: allow OriginalName for fields

Signed-off-by: Justin Chadwell <[email protected]>

* fix: go codegen should request the json field name

...if it's present. The server now correctly sends requests to the
module that match what it asks for (regardless of what casing the user
wanted). So we now need to ask for the right thing.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add test for structs with json combinations

Signed-off-by: Justin Chadwell <[email protected]>

* test: fixup test to detect id instead of ID

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow nested structs from module functions

Previously this would just fail (but would allow nesting lists of
objects!). This was because the condition we were checking didn't take
into account non-idable object types (which is what's returned from
modules).

Also, this prevents the previously weird case of allowing a module to
declare it returns a directory and then return a *different* core type,
like a container, which would actually not error immediately.

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow returning lists of core-idable types

This also applies to fields that are lists of core-idable types.

Essentially, this translates to returning a list of ids, which we need
to lift to a list of objects. The easiest way to do this is by having a
little helper func.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add new test cases suggested in code review

Signed-off-by: Justin Chadwell <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* fix: capture field value at start of loop

Somehow this wasn't causing massive bugs? This meant we were getting the
wrong name/type in the resolver.

Signed-off-by: Justin Chadwell <[email protected]>

* feat: allow OriginalName for fields

Signed-off-by: Justin Chadwell <[email protected]>

* fix: go codegen should request the json field name

...if it's present. The server now correctly sends requests to the
module that match what it asks for (regardless of what casing the user
wanted). So we now need to ask for the right thing.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add test for structs with json combinations

Signed-off-by: Justin Chadwell <[email protected]>

* test: fixup test to detect id instead of ID

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow nested structs from module functions

Previously this would just fail (but would allow nesting lists of
objects!). This was because the condition we were checking didn't take
into account non-idable object types (which is what's returned from
modules).

Also, this prevents the previously weird case of allowing a module to
declare it returns a directory and then return a *different* core type,
like a container, which would actually not error immediately.

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow returning lists of core-idable types

This also applies to fields that are lists of core-idable types.

Essentially, this translates to returning a list of ids, which we need
to lift to a list of objects. The easiest way to do this is by having a
little helper func.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add new test cases suggested in code review

Signed-off-by: Justin Chadwell <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* fix: capture field value at start of loop

Somehow this wasn't causing massive bugs? This meant we were getting the
wrong name/type in the resolver.

Signed-off-by: Justin Chadwell <[email protected]>

* feat: allow OriginalName for fields

Signed-off-by: Justin Chadwell <[email protected]>

* fix: go codegen should request the json field name

...if it's present. The server now correctly sends requests to the
module that match what it asks for (regardless of what casing the user
wanted). So we now need to ask for the right thing.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add test for structs with json combinations

Signed-off-by: Justin Chadwell <[email protected]>

* test: fixup test to detect id instead of ID

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow nested structs from module functions

Previously this would just fail (but would allow nesting lists of
objects!). This was because the condition we were checking didn't take
into account non-idable object types (which is what's returned from
modules).

Also, this prevents the previously weird case of allowing a module to
declare it returns a directory and then return a *different* core type,
like a container, which would actually not error immediately.

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow returning lists of core-idable types

This also applies to fields that are lists of core-idable types.

Essentially, this translates to returning a list of ids, which we need
to lift to a list of objects. The easiest way to do this is by having a
little helper func.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add new test cases suggested in code review

Signed-off-by: Justin Chadwell <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* fix: capture field value at start of loop

Somehow this wasn't causing massive bugs? This meant we were getting the
wrong name/type in the resolver.

Signed-off-by: Justin Chadwell <[email protected]>

* feat: allow OriginalName for fields

Signed-off-by: Justin Chadwell <[email protected]>

* fix: go codegen should request the json field name

...if it's present. The server now correctly sends requests to the
module that match what it asks for (regardless of what casing the user
wanted). So we now need to ask for the right thing.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add test for structs with json combinations

Signed-off-by: Justin Chadwell <[email protected]>

* test: fixup test to detect id instead of ID

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow nested structs from module functions

Previously this would just fail (but would allow nesting lists of
objects!). This was because the condition we were checking didn't take
into account non-idable object types (which is what's returned from
modules).

Also, this prevents the previously weird case of allowing a module to
declare it returns a directory and then return a *different* core type,
like a container, which would actually not error immediately.

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow returning lists of core-idable types

This also applies to fields that are lists of core-idable types.

Essentially, this translates to returning a list of ids, which we need
to lift to a list of objects. The easiest way to do this is by having a
little helper func.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add new test cases suggested in code review

Signed-off-by: Justin Chadwell <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
schlapzz pushed a commit to schlapzz/dagger that referenced this pull request Nov 24, 2023
* fix: capture field value at start of loop

Somehow this wasn't causing massive bugs? This meant we were getting the
wrong name/type in the resolver.

Signed-off-by: Justin Chadwell <[email protected]>

* feat: allow OriginalName for fields

Signed-off-by: Justin Chadwell <[email protected]>

* fix: go codegen should request the json field name

...if it's present. The server now correctly sends requests to the
module that match what it asks for (regardless of what casing the user
wanted). So we now need to ask for the right thing.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add test for structs with json combinations

Signed-off-by: Justin Chadwell <[email protected]>

* test: fixup test to detect id instead of ID

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow nested structs from module functions

Previously this would just fail (but would allow nesting lists of
objects!). This was because the condition we were checking didn't take
into account non-idable object types (which is what's returned from
modules).

Also, this prevents the previously weird case of allowing a module to
declare it returns a directory and then return a *different* core type,
like a container, which would actually not error immediately.

Signed-off-by: Justin Chadwell <[email protected]>

* fix: allow returning lists of core-idable types

This also applies to fields that are lists of core-idable types.

Essentially, this translates to returning a list of ids, which we need
to lift to a list of objects. The easiest way to do this is by having a
little helper func.

Signed-off-by: Justin Chadwell <[email protected]>

* test: add new test cases suggested in code review

Signed-off-by: Justin Chadwell <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
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.

🐞 Can't access *Directory info from field directly from return type of the module Zenith: fix Go struct field json serialization

3 participants