-
Notifications
You must be signed in to change notification settings - Fork 755
JSON: use JSONSerialization instead of Yams #702
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
JSON: use JSONSerialization instead of Yams #702
Conversation
Hey 👋 I'm Eve, the friendly bot watching over SwiftGen 🤖 Thanks a lot for your contribution! Seems like everything is in order 👍 You did a good job here! 🤝 Generated by 🚫 Danger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Looks good 👍
I just think we need to move some unit tests around to split YAML tests in 2 before merging though.
- Currently, our Unit tests are only on Yaml, and we just have one
.json
fixture used by the Yaml tests. We should instead move thatjson
fixture in its dedicatedTests/Fixtures/Resources/JSON
folder, and maybe add one or two other JSONs (like maybe one whose root is a dict, another with an array instead…) - Then add a
Tests/SwiftGenKitTests/JsonTests.swift
test file and move thefunc testJSON()
test case (fromYamlTests.swift
) in there (as well as create 1 or 2 more test cases for 1 or 2 other JSON fixtures?)- You'll have to add an entry to the
Fixtures.Directory
(seeTestsHelper.swift
), and create the corresponding subdirectory inFixtures/StencilContexts
for the JSON contexts to be generated there for the tests - Then be sure to change the
sub:
parameter in the implementation oftestJSON()
to use that newFixture.Directory.json
- Once you've done those changes, use the
SwiftGenKit - Generate Contexts
Xcode scheme to automatically re-generate the unit test fixture files - Tip: I suggest you to delete the content of the
Fixtures/StencilContexts/Yaml
before running that scheme, so that any file that is not needed (especiallyconfiguration.yml
context generated fromconfiguration.json
input file will likely have to be deleted from the Yaml StencilContext subdir) will not stay there and only the necessary files will be re-created
- You'll have to add an entry to the
- Finally, split the
YAML-Demo
page in the demo Playground in 2 separate pages, extracting the JSON example in its own page
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just couple of small things, rest seems good (assuming it's mostly a copy&paste from the yaml parser).
Tagging this 6.2.1 for now, unless it's done before we create the 6.2.0 PR. |
I didn't quite understand the playground. Is that embedded code generated by SwiftGen automatically? |
Currently we copy code from one of the generated files (for the output tests), and sometimes process them a bit as needed (to keep the pages as minimal as possible) |
hmm, do we need to make any changes here then? since the generated code will be the same, I don't see why we need to update the playgrounds |
If the test changes that @AliSoftware mentioned are implemented, then that'll mean that the resources and generated code will change (and we'll have a separate JSON page instead of 1 for both JSON & YAML). We'll get to that when we've covered the test changes. |
i've updated the tests, but the thing is that the template tests still are using ymls. I don't think this is too much of a deal right now, so I wonder if we can do this clean up later (as we update the templates to use |
Ok, for the playground that's my bad, I missed that we already had a I think I confused this with the case of some playground pages in which we do have multiple examples in a single playground page (for example the The only thing you could include in that PR regarding playground changes is to update the examples – for example the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Latest changes seems to address everything, except that comment below about the tiny change left in TemplatesTests/
; once this is addressed I think we'll be ready to merge! 🎉
Tests/Fixtures/Generated/JSON/inline-swift4/all-customName.swift
Outdated
Show resolved
Hide resolved
Tests/Fixtures/Generated/JSON/inline-swift5/compilation-configuration.yml
Outdated
Show resolved
Hide resolved
Tests/Fixtures/Generated/JSON/inline-swift4/compilation-configuration.yml
Outdated
Show resolved
Hide resolved
Tests/Fixtures/Generated/JSON/runtime-swift4/compilation-configuration.yml
Outdated
Show resolved
Hide resolved
Tests/Fixtures/Generated/JSON/runtime-swift5/compilation-configuration.yml
Outdated
Show resolved
Hide resolved
Looks like the tests are failing because the template is not stable anymore, the order of the keys is changing between runs: internal enum JSONFiles {
- internal static let items: [[String: Any]] = [[AnyHashable("id"): "1", AnyHashable("name"): "Anna"], [AnyHashable("name"): "Bob", AnyHashable("id"): "2"]]
+ internal static let items: [[String: Any]] = [[AnyHashable("name"): "Anna", AnyHashable("id"): "1"], [AnyHashable("name"): "Bob", AnyHashable("id"): "2"]]
} This is not surprising given that |
Ah, this has indeed been a problem in other places in the past. This is why we order most of the content alphabetically in most of the places. Like the order of the entries in We might need to do the same for the JSON content then. Indeed not sure why this was not a problem before. Maybe it was sheer luck that the order on macOS (our dev machines) and Linux (CI) were the same in the past implementation. Maybe it was due to some implementation details of In any case the fact is that |
PS: in case you were thinking of using the special build setting that we can use in Xcode to make Dictionary hashes be stable across runs for testing purposes, this is not really a solution since we also need to generate stable outputs in production, not just during tests, so that the generated code is also stable when the user runs |
Note on that "sorting of keys" issue: What has changed that defeats that check? And is there something that can be adressed in Stencil? I'm preparing a release there, so we can include a fix if needed. |
so I think the issue is not the order in the template, but the order inside a template variable |
The template outputs variables using a for loop: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs to update the docs at Documentation/SwiftGenKit Contexts/JSON.md
. Note that this currently is a symlink to the Yaml.md
file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub is being difficult, so can't point to the right line, but:
In the json context documentation, you can remove the documents
part (plural), we don't usually document deprecated properties in the Docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allright this looks pretty complete to me. Maybe @AliSoftware wants to do a last pass, otherwise we can merge this. The (functional) change itself is pretty small after all.
idk what's up with GitHub this evening. Can't post a PR review nor comment inline either.
|
btw wondering if we should merge this in |
There are issues with the inline templates in general:
Seeing as those are already present in the current & older releases, I don't think this PR should be held back because of them. |
Ok, updated my previous comment with my latest feedback. Mainly just a typo and a question about including |
# Conflicts: # Tests/Fixtures/Generated/JSON/inline-swift4/all-customName.swift # Tests/Fixtures/Generated/JSON/inline-swift4/all-forceFileNameEnum.swift # Tests/Fixtures/Generated/JSON/inline-swift4/all-publicAccess.swift # Tests/Fixtures/Generated/JSON/inline-swift4/all.swift # Tests/Fixtures/Generated/JSON/inline-swift5/all-customName.swift # Tests/Fixtures/Generated/JSON/inline-swift5/all-forceFileNameEnum.swift # Tests/Fixtures/Generated/JSON/inline-swift5/all-publicAccess.swift # Tests/Fixtures/Generated/JSON/inline-swift5/all.swift # Tests/Fixtures/Generated/JSON/runtime-swift4/all-customname.swift # Tests/Fixtures/Generated/JSON/runtime-swift4/all-forceFileNameEnum.swift # Tests/Fixtures/Generated/JSON/runtime-swift4/all-preservePath.swift # Tests/Fixtures/Generated/JSON/runtime-swift4/all-publicAccess.swift # Tests/Fixtures/Generated/JSON/runtime-swift4/all.swift # Tests/Fixtures/Generated/JSON/runtime-swift5/all-customname.swift # Tests/Fixtures/Generated/JSON/runtime-swift5/all-forceFileNameEnum.swift # Tests/Fixtures/Generated/JSON/runtime-swift5/all-preservePath.swift # Tests/Fixtures/Generated/JSON/runtime-swift5/all-publicAccess.swift # Tests/Fixtures/Generated/JSON/runtime-swift5/all.swift
I'm merging everything and processing the feedback (+ fixing the typos in other places). Note: @AliSoftware we already use the |
👍 cool. I see that you fixed
Ah, right. Let's keep just another nit: I've also noticed that we seem to use inconsistent case in fine names for |
Fixes #698