Skip to content

Commit

Permalink
fixup leading 0s round-trip problem
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoutts committed Sep 15, 2020
1 parent 6d4efe6 commit bf8a544
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions cardano-api/src/Cardano/Api/MetaData.hs
Expand Up @@ -233,9 +233,11 @@ metadataFromJson schema =
return (k', v')

convTopLevelKey :: Text -> Either TxMetadataJsonError Word64
convTopLevelKey k = maybe (Left (TxMetadataJsonToplevelBadKey k)) Right
. parseAll pUnsigned
$ k
convTopLevelKey k =
case parseAll (pUnsigned <* Atto.endOfInput) k of
Just n | n <= fromIntegral (maxBound :: Word64)
-> Right (fromIntegral n)
_ -> TxMetadataJsonToplevelBadKey k

validateMetadataValue :: TxMetadataValue -> Either TxMetadataRangeError ()
validateMetadataValue v =
Expand Down Expand Up @@ -502,10 +504,8 @@ parseAll p = either (const Nothing) Just
. Atto.parseOnly p
. Text.encodeUtf8

pUnsigned :: Atto.Parser Word64
pUnsigned = decimal <* Atto.endOfInput
where
decimal = do
pUnsigned :: Atto.Parser Integer
pUnsigned = do
bs <- Atto.takeWhile1 Atto.isDigit
-- no redundant leading 0s allowed, or we cannot round-trip properly
guard (not (BS.length bs > 1 && BSC.head bs == '0'))
Expand All @@ -514,7 +514,7 @@ pUnsigned = decimal <* Atto.endOfInput
step a w = a * 10 + fromIntegral (w - 48)

pSigned :: Atto.Parser Integer
pSigned = Atto.signed Atto.decimal
pSigned = Atto.signed pUnsigned

pBytes :: Atto.Parser ByteString
pBytes = do
Expand Down

0 comments on commit bf8a544

Please sign in to comment.