Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tuples to be converted to JsonNode by % in json.nim as discussed in #24082 . #24089

Open
wants to merge 6 commits into
base: devel
Choose a base branch
from

Conversation

siriuslee69
Copy link

@siriuslee69 siriuslee69 commented Sep 9, 2024

As discussed in issue #24082.

  • Added the tuple type as a legit input type for %, to be able to convert named and unnamed tuples. Also changed part of the function itself to correctly discern between named and anonymous/unnamed tuples. The code was copied directly from jsonutils.nim (slightly changed).
  • Additionally changed the openArray % proc to not allow tuples as array elements, such that
let 
   myArr = [someTuple1, someTuple2]
   myJsonNode = %myArr

cannot be compiled.

  • Added / Copied the isNamedTuple() proc from the jsonutils.nim file instead of adding a dependency by using import std/typetraits .

  • Added the respective tests in tjson.nim, both for the macro %* and the overloaded proc % for both anonymous/unnamed and named tuples.

  • As in jsonutils.nim, the named tuples are converted to JObject while the anonymous tuples are converted to JArray.

Quick explanation as to why we had to disallow tuples for the openArray (so people don't have to scroll through the other convo):
Curly brackets together with a key value pair, kind of like this: {"error": "No message"} will be compiled to [("error", "no message")] by Nim. Which is an array of tuples.
That means, if we do %{"error": "No message"}, the compiler will treat it as an array with a tuple inside.
It will look like this under the hood: %[("error", "No message")].
If we would pass this to the openarray function (with the new tuple % proc implemented), we should get:
[["error", "No message"]] .
And that is somehow a problem, idk why though tbh.

Related issues or issues that I copied code/comments from:
#16851, #12290, #16851, #10019, #10823

Allow tuples as input for `%`, but disallow them in the generic type of the `%` proc that accepts openArrays.
Additionally addedthe isNamedTuple(T) proc. Can instead also be imported from std/typetraits, but ig dependencies should be kept to a minimum.
added additional conversion info to the proc, copied it straight from an earlier issue
Forgot to change parts of the copied code from jsonutils.
@siriuslee69
Copy link
Author

I think this is what causes the fails... but I don't really know why. Don't know what a Cluster or Pix is tbh.
Maybe I made a mistake in the implementation somewhere? Idk. Will have to look through it tomorrow.

@planetis-m
Copy link
Contributor

There was an issue that would cause the generic overload with tuple|object to silently fail to apply. I don't remember any details sorry, but its related to: #16321

Something like proc myproc[T: object|tuple](o:T): void = ...
might not accept the tuple properly even though it's supposed to, such that:
myproc(mytup) doesn't work. 
Idk if that was the actual problem here, but just to be sure, I changed the respective functions.
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.

2 participants