-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2092: Implement Multi-Asset JSON instances r=dcoutts a=Jimbo4350 Multi-Asset JSON is of the following format: ```json { "fooPolicyId": { "fooAssetName": 42 }, "barPolicyId": { "barAssetName": 84 }, "lovelace": 3 } ``` Co-authored-by: Jordan Millar <[email protected]>
- Loading branch information
Showing
6 changed files
with
248 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Test.Cardano.Api.Typed.Value | ||
( tests | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.List (sort, groupBy) | ||
import Data.Aeson | ||
import qualified Data.Map.Strict as Map | ||
|
||
import Cardano.Api.Typed | ||
|
||
import Hedgehog (Property, discover, forAll, property, tripping, (===)) | ||
import Test.Cardano.Api.Typed.Gen | ||
|
||
import Test.Tasty (TestTree) | ||
import Test.Tasty.Hedgehog.Group (fromGroup) | ||
|
||
prop_roundtrip_Value_JSON :: Property | ||
prop_roundtrip_Value_JSON = | ||
property $ do v <- forAll genValue | ||
tripping v encode eitherDecode | ||
|
||
|
||
prop_roundtrip_Value_flatten_unflatten :: Property | ||
prop_roundtrip_Value_flatten_unflatten = | ||
property $ do v <- forAll genValue | ||
valueFromNestedRep (valueToNestedRep v) === v | ||
|
||
prop_roundtrip_Value_unflatten_flatten :: Property | ||
prop_roundtrip_Value_unflatten_flatten = | ||
property $ do | ||
v <- forAll genValueNestedRep | ||
canonicalise v === valueToNestedRep (valueFromNestedRep v) | ||
|
||
canonicalise :: ValueNestedRep -> ValueNestedRep | ||
canonicalise = | ||
ValueNestedRep | ||
. filter (not . isZeroOrEmpty) | ||
. map filterZeros | ||
. map (foldl1 mergeBundle) | ||
. groupBy samePolicyId | ||
. sort | ||
. (\(ValueNestedRep bundles) -> bundles) | ||
where | ||
samePolicyId ValueNestedBundleAda{} | ||
ValueNestedBundleAda{} = True | ||
samePolicyId (ValueNestedBundle pid _) | ||
(ValueNestedBundle pid' _) = pid == pid' | ||
samePolicyId _ _ = False | ||
|
||
-- Merge together bundles that have already been grouped by same PolicyId: | ||
mergeBundle (ValueNestedBundleAda q) | ||
(ValueNestedBundleAda q') = | ||
ValueNestedBundleAda (q <> q') | ||
|
||
mergeBundle (ValueNestedBundle pid as) | ||
(ValueNestedBundle pid' as') | pid == pid' = | ||
ValueNestedBundle pid (Map.unionWith (<>) as as') | ||
|
||
mergeBundle _ _ = error "canonicalise.mergeBundle: impossible" | ||
|
||
filterZeros b@ValueNestedBundleAda{} = b | ||
filterZeros (ValueNestedBundle pid as) = | ||
ValueNestedBundle pid (Map.filter (/=0) as) | ||
|
||
isZeroOrEmpty (ValueNestedBundleAda q) = q == 0 | ||
isZeroOrEmpty (ValueNestedBundle _ as) = Map.null as | ||
|
||
|
||
|
||
-- ----------------------------------------------------------------------------- | ||
|
||
tests :: TestTree | ||
tests = fromGroup $$discover | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters