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

Commits on Jul 20, 2022

  1. syntax/typedjson: expose shfmt's "typed JSON" as Go APIs

    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 committed Jul 20, 2022
    Configuration menu
    Copy the full SHA
    014ca89 View commit details
    Browse the repository at this point in the history