Skip to content

Conversation

@sevanspowell
Copy link
Contributor

  • As per Testing decimals #26, improve error messages when failing to parse a decimals value.
  • For well-known properties, we may be wanted to parse a String or some other type. I've improved the error messages by first trying to parse as an arbitrary JSON value, and it that fails with some "not a valid json value"-style errors, try to parse as a string.
  • It's only a quick fix, but it does provide better results:
$ token-metadata-creator entry ... --decimals "potato"
- option --decimals: Error in $: Failed reading: not a valid json value
+ option --decimals: Error in $: parsing Int failed, expected Number, but encountered String

$ token-metadata-creator entry ... --decimals 0x
- option --decimals: Error in $: endOfInput
+ option --decimals: Error in $: parsing Int failed, expected Number, but encountered String


$ token-metadata-creator entry ... --decimals 0.x
- option --decimals: Error in $: Failed reading: takeWhile1
+ option --decimals: Error in $: parsing Int failed, expected Number, but encountered String

@sevanspowell sevanspowell requested review from piotr-iohk and rvl May 18, 2021 03:54
@sevanspowell sevanspowell self-assigned this May 18, 2021
Copy link
Contributor

@rvl rvl left a comment

Choose a reason for hiding this comment

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

Looks like it will work. 👍

Copy link
Contributor

@piotr-iohk piotr-iohk left a comment

Choose a reason for hiding this comment

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

LGTM.

Thanks @sevanspowell!

BTW, do you have any clue why this other npm tests that I mentioned in #26 fails?

  1 failing

  1) token-metadata-creator
       Minimal workflow
         Add required fields:

      AssertionError: expected { Object (sequenceNumber, value, ...) } to deeply equal { Object (sequenceNumber, signatures, ...) }
      + expected - actual

       {
         "sequenceNumber": 0
         "signatures": []
      -  "value": "������"
      +  "value": "ギル"
       }

I think this used to work before 🤔

@rvl
Copy link
Contributor

rvl commented May 18, 2021

I suspect it's a genuine failure because of the change to string parsing on the CLI.
Text is always UTF-8 under JSON decoding, but the user's locale might be different.

@sevanspowell
Copy link
Contributor Author

Hmm, I ran those test locally and they all passed so I assumed they worked. Let me double check for you.

@sevanspowell sevanspowell force-pushed the feature/ADP-915-decimals-minimal branch from bb3c28e to f7d5b7b Compare May 19, 2021 01:11
@sevanspowell
Copy link
Contributor Author

Hmm, I ran those test locally and they all passed so I assumed they worked. Let me double check for you.

Confirmed @piotr-iohk, those npm/nodejs tests complete successfully 👍

@rvl
Copy link
Contributor

rvl commented May 19, 2021

Try reproducing it this way:

LANG=C npm run test

Copy link
Contributor

@rvl rvl left a comment

Choose a reason for hiding this comment

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

Here's an approximate fix for the test failure.

Comment on lines +212 to +213
asJSONString :: String -> Either String p
asJSONString = Aeson.parseEither parseWellKnown . Aeson.String . T.pack
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
asJSONString :: String -> Either String p
asJSONString = Aeson.parseEither parseWellKnown . Aeson.String . T.pack
asJSONString :: ByteString -> Either String p
asJSONString = (Aeson.parseEither parseWellKnown . bimap show Aeson.String) <=< T.decodeUtf8'

Use Data.Text.Encoding.decodeUtf8' to ensure that text is always treated as UTF-8.

Comment on lines +209 to +210
asJSONValue :: String -> Either String p
asJSONValue = (Aeson.parseEither parseWellKnown =<<) . Aeson.eitherDecodeStrict' . BC8.pack
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
asJSONValue :: String -> Either String p
asJSONValue = (Aeson.parseEither parseWellKnown =<<) . Aeson.eitherDecodeStrict' . BC8.pack
asJSONValue :: ByteString -> Either String p
asJSONValue = Aeson.parseEither parseWellKnown <=< Aeson.eitherDecodeStrict'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, that solution just moves the decoding error to the UTF-8 locale. The C locale works, but UTF-8 throws the same error. I'll look into the character encoding a bit more, I imagine I'm going to have to normalize the input stream.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which is exactly what you were saying... Sorry.

Copy link
Contributor

Choose a reason for hiding this comment

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

No prob!

@rvl rvl force-pushed the feature/ADP-915-decimals-minimal branch from f7d5b7b to c3aacda Compare May 20, 2021 06:39
@rvl
Copy link
Contributor

rvl commented May 20, 2021

I added the utf-8 locale fix that we have in cardano-wallet.
The problem with testing is that LANG=C also affects nodejs, so 🤷.
Let's merge.

@rvl rvl force-pushed the feature/ADP-915-decimals-minimal branch from c3aacda to 61a4ab8 Compare May 20, 2021 07:30
@piotr-iohk
Copy link
Contributor

@rvl @sevanspowell this is something I'm also able to reproduce manually. My default bash locale is:

$ echo $LANG
en_US.UTF-8

(so, I guess, nothing fancy)

Doing:

$ token-metadata-creator entry --init 919e8a1922aaa764b1d66407c6f62244e77081215f385b60a62091494861707079436f696e

$ token-metadata-creator entry 919e8a1922aaa764b1d66407c6f62244e77081215f385b60a62091494861707079436f696e \
  --name "ギル" \
  --description "Locale test!!!" \
  --policy /home/piotr/wb/tokens_minter/testnet/happy_coin/policy.script

results in this in the *.json.draft:

...
    "name": {
        "sequenceNumber": 0,
        "value": "������",
        "signatures": []
    }
...

@sevanspowell
Copy link
Contributor Author

@piotr-iohk is that with the changes @rvl committed a few minutes ago (61a4ab8)? I've just tested it myself with my bash locale the same as yours, but it worked for me.

@rvl
Copy link
Contributor

rvl commented May 20, 2021

Ah yes @piotr-iohk - good catch - I found there was something else missing in the locale-setting code.
Now it works, and I added a test case for it.

@piotr-iohk
Copy link
Contributor

@sevanspowell @rvl yes I was checking just before latest commits.
On the latest version 61a4ab8 it works fine 🎉.

@sevanspowell sevanspowell merged commit 9b967e8 into master May 21, 2021
@iohk-bors iohk-bors bot deleted the feature/ADP-915-decimals-minimal branch May 21, 2021 01:12
iohk-bors bot added a commit to cardano-foundation/cardano-wallet that referenced this pull request May 25, 2021
2661: CLI: Force command-line arguments to be UTF-8 decoded r=rvl a=rvl

### Issue Number

Bug found while doing ADP-915.

### Overview

Applies the same fix as in input-output-hk/offchain-metadata-tools#27 so that command-line arguments are also decoded as UTF-8 text, regardless of the user's locale setting.



2664: Fix slow ApiSharedWallet unit tests r=rvl a=rvl

### Issue Number

Found during ADP-902

### Overview

The `Cardano.Wallet.Api.Types` tests are running very slowly. Also the unit tests were sometimes timing out in CI.

Profiling showed that the `Arbitrary` instance of `ApiSharedWallet` was the slowest part.
This change improves the speed of this generator.
Now it is the OpenAPI validation which is the slowest part.

### Comments

We may also be able to reduce the size of arbitrary `ApiSharedWallet` values, so that validation gets faster. Because it's still not especially quick. Something like the `instance Arbitrary WalletName` change in this PR could probably be applied.


Co-authored-by: Rodney Lorrimar <[email protected]>
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.

4 participants