Skip to content

Sort record and union fields when CBOR-encoding #835

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

Merged
merged 4 commits into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dhall/src/Dhall/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,15 @@ encode (Record xTs₀) =
TList [ TInt 7, TMap xTs₁ ]
where
xTs₁ = do
(x₀, _T₀) <- Dhall.Map.toList xTs₀
(x₀, _T₀) <- Dhall.Map.toList (Dhall.Map.sort xTs₀)
let x₁ = TString x₀
let _T₁ = encode _T₀
return (x₁, _T₁)
encode (RecordLit xts₀) =
TList [ TInt 8, TMap xts₁ ]
where
xts₁ = do
(x₀, t₀) <- Dhall.Map.toList xts₀
(x₀, t₀) <- Dhall.Map.toList (Dhall.Map.sort xts₀)
let x₁ = TString x₀
let t₁ = encode t₀
return (x₁, t₁)
Expand All @@ -338,7 +338,7 @@ encode (Union xTs₀) =
TList [ TInt 11, TMap xTs₁ ]
where
xTs₁ = do
(x₀, _T₀) <- Dhall.Map.toList xTs₀
(x₀, _T₀) <- Dhall.Map.toList (Dhall.Map.sort xTs₀)
let x₁ = TString x₀
let _T₁ = encode _T₀
return (x₁, _T₁)
Expand All @@ -348,7 +348,7 @@ encode (UnionLit x t₀ yTs₀) =
t₁ = encode t₀

yTs₁ = do
(y₀, _T₀) <- Dhall.Map.toList yTs₀
(y₀, _T₀) <- Dhall.Map.toList (Dhall.Map.sort yTs₀)
let y₁ = TString y₀
let _T₁ = encode _T₀
return (y₁, _T₁)
Expand Down
4 changes: 3 additions & 1 deletion dhall/tests/Dhall/Test/QuickCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Test.Tasty (TestTree)

import qualified Codec.Serialise
import qualified Data.Coerce
import qualified Data.List
import qualified Dhall.Map
import qualified Data.Sequence
import qualified Dhall.Binary
Expand Down Expand Up @@ -124,7 +125,8 @@ instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (Map k v) where
arbitrary = do
n <- Test.QuickCheck.choose (0, 2)
kvs <- Test.QuickCheck.vectorOf n ((,) <$> arbitrary <*> arbitrary)
return (Dhall.Map.fromList kvs)
-- Sorting the fields here because serialization needs them in order
return (Dhall.Map.fromList (Data.List.sortOn fst kvs))

shrink =
map Dhall.Map.fromList
Expand Down