Repository navigation
Zenith (Go SDK): allow omitting JSON types in structs - #6057
Conversation
There was a problem hiding this comment.
Just a little bug waiting to happen! 🪲
Thankfully, all it was potentially doing was giving an incorrect error message, the behavior wasn't affected.
6c40235 to
0b0eed4
Compare
|
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:
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). |
3b6d3f0 to
9e538dc
Compare
vito
left a comment
There was a problem hiding this comment.
Nice! Went over the changes and tests, it all makes sense to me.
sipsma
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
9e538dc to
12524f3
Compare
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]>
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]>
Signed-off-by: Justin Chadwell <[email protected]>
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]>
Signed-off-by: Justin Chadwell <[email protected]>
12524f3 to
1933d63
Compare
* 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]>
* 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]>
* 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]>
* 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]>
* 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]>
* 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]>
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
OriginalNamesupport 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
daggertags. 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.