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

syntax/typedjson: expose shfmt's "typed JSON" as Go APIs #903

Merged
merged 1 commit into from
Jul 20, 2022

Conversation

mvdan
Copy link
Owner

@mvdan mvdan commented Jul 19, 2022

(see commit message)

Fixes #885.

@mvdan mvdan requested a review from riacataquian July 19, 2022 18:43
@mvdan
Copy link
Owner Author

mvdan commented Jul 19, 2022

cc @JounQin @cilki

FYI @JounQin, the PR to make this work with tinygo will come after. Right now it doesn't work, but we are not far:

../syntax/typedjson/json.go:78:33: VisibleFields not declared by package reflect
../syntax/typedjson/json.go:89:21: StructOf not declared by package reflect

@JounQin
Copy link
Contributor

JounQin commented Jul 19, 2022

It is so great to hear!

Copy link
Collaborator

@riacataquian riacataquian left a comment

Choose a reason for hiding this comment

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

The move to typedjson looks neat :)

syntax/typedjson/json_test.go Outdated Show resolved Hide resolved
Create a new package with docs, copy the code, and expose the API.
The following changes were made to the API:

* Name the main APIs Encode and Decode.
* Added options structs, to give us flexibility in the future.
* The "pretty" option is now an Indent string, like encoding/json.

Note that we now require "Type" keys to come first in each object.
This is not required right now, nor is it enforced by our decoder yet,
but it will be necessary to implement a performant decoder.
This is because we can only decode fields once we know the type;
if the "Type" key comes later, we must decode twice or buffer tokens.

The typed JSON encoding also changes slightly: since we want Encode and
Decode to work on syntax.Node rather than syntax.File, the root node
needs to include a "Type" JSON key as well.

This also makes shfmt's --to-json behave nicer on empty files;
rather than emitting `{}` lacking any information at all,
it now emits an empty file node in tthe form of `{"Type":"File"}`.

Finally, we apply some minor refactors to the code and tests.

Fixes #885.
@mvdan mvdan force-pushed the syntax-typedjson branch from f5090b7 to 014ca89 Compare July 20, 2022 14:09
@mvdan mvdan merged commit 5e16174 into master Jul 20, 2022
@mvdan mvdan deleted the syntax-typedjson branch July 20, 2022 15:29
@JounQin
Copy link
Contributor

JounQin commented Jan 6, 2024

Hi, @mvdan , are we close to tinygo support? Is there still any blocking?

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.

Expose ToJson and FromJson methods
3 participants