Skip to content

feat(serialize): overload table to render json on cmd end#1

Open
servalD wants to merge 1 commit intomainfrom
poc-serialize-json-overload
Open

feat(serialize): overload table to render json on cmd end#1
servalD wants to merge 1 commit intomainfrom
poc-serialize-json-overload

Conversation

@servalD
Copy link
Copy Markdown

@servalD servalD commented Jan 8, 2025

Why this should be merged

The addition of a RenderJSON method to the table.Writer enables native JSON rendering without requiring external transformations or custom code. This simplifies workflows for JSON-based applications. It enhances the flexibility of go-pretty while maintaining the existing rendering capabilities.


How this works

A new PrintIfJson method is added to the table.Writer. It formats the table data (headers and rows) into structured JSON at the end of the command.

  • Flat table example:
    As table:

    +---------------------------------------------------------------------------------------------------------------------------+
    |                                                       ASH VALIDATORS                                                      |
    +------------------------------------------+---------------------------------------------------+--------+-------------------+
    | NODE ID                                  | VALIDATION ID                                     | WEIGHT | REMAINING BALANCE |
    +------------------------------------------+---------------------------------------------------+--------+-------------------+
    | NodeID-ECKiPtNQJrDYoGKma2yKaKzvuJT7V4HCz | bV23Ev9wL4pGYgfqVk88awYfshkGzfdgQ3w12h1hurX2r2hBR |   100  |               0.1 |
    +------------------------------------------+---------------------------------------------------+--------+-------------------+
    

    As json:

     {
      "ash_validators": [
        {
          "node_id": "NodeID-ECKiPtNQJrDYoGKma2yKaKzvuJT7V4HCz",
          "remaining_balance": "0.1",
          "validation_id": "bV23Ev9wL4pGYgfqVk88awYfshkGzfdgQ3w12h1hurX2r2hBR",
          "weight": "100"
        }
      ]
    }
  • Grouped data example:
    As table:

    +--------------------------------------------------------------------------------------------------------------------------------+
    |                                                               ASH                                                              |
    +---------------+----------------------------------------------------------------------------------------------------------------+
    | Name          | ash                                                                                                            |
    +---------------+----------------------------------------------------------------------------------------------------------------+
    | VM ID         | jvFW8PbuerjWravW4Gbfe8W1GsVSQNPrLncseZG8vTX5nwqWh                                                              |
    +---------------+----------------------------------------------------------------------------------------------------------------+
    | VM Version    | v0.7.0                                                                                                         |
    +---------------+----------------------------------------------------------------------------------------------------------------+
    | Validation    | Proof Of Authority                                                                                             |
    +---------------+--------------------------+-------------------------------------------------------------------------------------+
    | Local Network | ChainID                  | 7777                                                                                |
    |               +--------------------------+-------------------------------------------------------------------------------------+
    |               | SubnetID                 | GEieSy2doZ96bpMo5CuHPaX1LvaxpKZ9C72L22j94t6YyUb6X                                   |
    |               +--------------------------+-------------------------------------------------------------------------------------+
    |               | BlockchainID (CB58)      | fj65yEMzzb91Lo3EBawiHRqevQv2zoYGrH8CuopydnLGfFH56                                   |
    |               +--------------------------+-------------------------------------------------------------------------------------+
    |               | BlockchainID (HEX)       | 0x57ee6b52e3d3c948aca98e1ce4440e143bfacc7f8c1628a27b740423fd6ca12a                  |
    |               +--------------------------+-------------------------------------------------------------------------------------+
    |               | RPC Endpoint             | http://127.0.0.1:43287/ext/bc/fj65yEMzzb91Lo3EBawiHRqevQv2zoYGrH8CuopydnLGfFH56/rpc |
    +---------------+--------------------------+-------------------------------------------------------------------------------------+
    

    As json:

    {
      "ash": {
        "local_network": {
          "blockchainid_cb58": "fj65yEMzzb91Lo3EBawiHRqevQv2zoYGrH8CuopydnLGfFH56",
          "blockchainid_hex": "0x57ee6b52e3d3c948aca98e1ce4440e143bfacc7f8c1628a27b740423fd6ca12a",
          "chainid": "7777",
          "rpc_endpoint": "http://127.0.0.1:43287/ext/bc/fj65yEMzzb91Lo3EBawiHRqevQv2zoYGrH8CuopydnLGfFH56/rpc",
          "subnetid": "GEieSy2doZ96bpMo5CuHPaX1LvaxpKZ9C72L22j94t6YyUb6X"
        },
        "name": "ash",
        "validation": "Proof Of Authority",
        "vm_id": "jvFW8PbuerjWravW4Gbfe8W1GsVSQNPrLncseZG8vTX5nwqWh",
        "vm_version": "v0.7.0"
      }
    }

How this was tested

  1. As the logic of commands was not altered, the testing task is about checking terminal output
  2. Tested flat and hierarchical table structures.
  3. Verified JSON rendering consistency using tools like jq to validate the format.
  4. Tested on all commands ( no impact when not using --json )

How is this documented

  • Code comments

How to test it (impacted command)

Run the following commands to validate JSON rendering integration:

  • Build the project:
    ./scripts/build.sh
  • Start a network with JSON output:
    ./bin/avalanche network start -j | jq
    ./bin/avalanche blockchain create ash
    ./bin/avalanche blockchain deploy ash -j -l -o | jq
    ./bin/avalanche blockchain describe -j ash | jq
    ./bin/avalanche validator list ash -l -j | jq
    ./bin/avalanche blockchain upgrade apply ash --local --force -j | jq

Related feature request

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.

1 participant