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

transaction id: add --output-[json,text] flag to control format of the output #1005

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

smelc
Copy link
Contributor

@smelc smelc commented Jan 6, 2025

Changelog

- description: |
    transaction id: add --output-[json,text] flag to control format of the output
# uncomment types applicable to the change:
  type:
  - feature        # introduces a new feature
  # - breaking       # the API has changed in a breaking way
  - compatible     # the API has changed but is non-breaking
  # - optimisation   # measurable performance improvements
  # - refactoring    # QoL changes
  # - bugfix         # fixes a defect
  # - test           # fixes/modifies tests
  # - maintenance    # not directly related to the code
  # - release        # related to a new release preparation
  # - documentation  # change in code docs, haddocks...

Context

How to trust this PR

  • New tests are added
  • You can try the new test before the commit introducing the feature, and see it still works on HEAD so the default behavior didn't change

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

@smelc smelc force-pushed the smelc/transaction-txid-add-output-flags branch 2 times, most recently from 384cec0 to 97a7728 Compare January 7, 2025 14:41
@smelc smelc marked this pull request as ready for review January 7, 2025 14:41
@smelc smelc force-pushed the smelc/transaction-txid-add-output-flags branch 4 times, most recently from 6ea2dfd to 406ddfe Compare January 8, 2025 16:51
@smelc smelc requested review from a team as code owners January 8, 2025 16:51
@smelc smelc force-pushed the smelc/transaction-txid-add-output-flags branch 2 times, most recently from 17c25e3 to f7b4604 Compare January 9, 2025 10:33
Copy link
Contributor

@palas palas left a comment

Choose a reason for hiding this comment

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

Looks good. I just have two suggestions.

cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs Outdated Show resolved Hide resolved
Comment on lines 1731 to 1732
[ make OutputFormatJson "JSON" "json" False
, make OutputFormatText "TEXT" "text" True
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be best to have the default be a separate variable and compare to it, since we cannot ensure that there are no several True values in this list. Also the parser could already return the default instead of a Maybe, cause that would avoid having coupling with the implementation code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be best to have the default be a separate variable and compare to it, since we cannot ensure that there are no several True values in this list

@palas> done:

@@ -1728,14 +1728,15 @@ pOutputFormatJsonOrText kind =
 pTxIdOutputFormatJsonOrText :: Parser OutputFormatJsonOrText
 pTxIdOutputFormatJsonOrText =
   asum
-    [ make OutputFormatJson "JSON" "json" False
-    , make OutputFormatText "TEXT" "text" True
+    [ make OutputFormatJson "JSON" "json"
+    , make OutputFormatText "TEXT" "text"
     ]
  where
-  make format desc flag_ default_ =
+  default_ = OutputFormatText
+  make format desc flag_ =
     Opt.flag' format $
       mconcat
-        [ Opt.help $ "Format output as " <> desc <> (if default_ then " (the default)." else ".")
+        [ Opt.help $ "Format output as " <> desc <> (if format == default_ then " (the default)." else ".")
         , Opt.long ("output-" <> flag_)
         ]
 
diff --git a/cardano-cli/src/Cardano/CLI/Types/Common.hs b/cardano-cli/src/Cardano/CLI/Types/Common.hs
index 87253b0c7..d23ee97f2 100644
--- a/cardano-cli/src/Cardano/CLI/Types/Common.hs
+++ b/cardano-cli/src/Cardano/CLI/Types/Common.hs
@@ -545,7 +545,7 @@ data TxMempoolQuery
 data OutputFormatJsonOrText
   = OutputFormatJson
   | OutputFormatText
-  deriving Show
+  deriving (Eq, Show)

Also the parser could already return the default instead of a Maybe, cause that would avoid having coupling with the implementation code.

@palas> could we? Because we want the flags to be optional, to preserve the existing behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you can have a parser that returns the default on absence of a flag. I have done it somewhere, let me check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@palas> ah yes I could simply lift a value to Parser at the level of a Parser (Maybe a). I have the same issue for #1010 🙂

Copy link
Contributor Author

@smelc smelc Jan 10, 2025

Choose a reason for hiding this comment

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

@palas> this works (for #1010):

pDRepIdOutputFormat :: Parser IdOutputFormat
pDRepIdOutputFormat =
  asum [make IdOutputFormatHex "hex", make IdOutputFormatBech32 "bech32"]
    <|> pure default_
 where
  default_ = IdOutputFormatBech32
  make format flag_ =
    Opt.flag' format $
      mconcat
        [ Opt.help $
            "Format drep id output as "
              <> flag_
              <> (if format == default_ then " (the default)." else ".")
        , Opt.long ("output-" <> flag_)
        ]

Copy link
Contributor

Choose a reason for hiding this comment

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

That is it, I had forgotten the exact details

Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

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

LGTM. Was this requested?

@smelc
Copy link
Contributor Author

smelc commented Jan 9, 2025

LGTM. Was this requested?

@Jimbo4350> no, I witnessed this was a possible improvement to consistency of our CLI interface when using the id command for another work.

@smelc smelc force-pushed the smelc/transaction-txid-add-output-flags branch from ab49f8f to 12b76f5 Compare January 10, 2025 08:32
@smelc smelc enabled auto-merge January 10, 2025 08:32
@smelc smelc added this pull request to the merge queue Jan 10, 2025
@smelc smelc removed this pull request from the merge queue due to a manual request Jan 10, 2025
@smelc smelc force-pushed the smelc/transaction-txid-add-output-flags branch from 53a7bc4 to 6efbe9f Compare January 10, 2025 10:32
Copy link
Contributor

@palas palas left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@smelc smelc enabled auto-merge January 10, 2025 13:44
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.

era transaction txid: add --output-json and --output-text
3 participants