Skip to content

Commit

Permalink
Fix {json,yaml}-to-dhall marshaling into empty lists (#1284)
Browse files Browse the repository at this point in the history
This is essentially the same fix as #1186, applied in more places
  • Loading branch information
Gabriella439 authored and mergify[bot] committed Sep 6, 2019
1 parent 71bb569 commit eee7524
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 3 deletions.
6 changes: 3 additions & 3 deletions dhall-json/src/Dhall/JSONToDhall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ dhallFromJSON (Conversion {..}) expressionType =

let records = (fmap f . Seq.fromList . HM.toList) keyExprMap

let typeAnn = if HM.null o then Just mapValue else Nothing
let typeAnn = if HM.null o then Just t else Nothing

return (D.ListLit typeAnn records)
| noKeyValMap
Expand All @@ -467,7 +467,7 @@ dhallFromJSON (Conversion {..}) expressionType =
loop (App D.List t) (A.Array a)
= let f :: [ExprX] -> ExprX
f es = D.ListLit
(if null es then Just t else Nothing)
(if null es then Just (App D.List t) else Nothing)
(Seq.fromList es)
in f <$> traverse (loop t) (toList a)

Expand Down Expand Up @@ -545,7 +545,7 @@ dhallFromJSON (Conversion {..}) expressionType =
let elements = Seq.fromList (fmap outer (Vector.toList a))

elementType
| null elements = Just "JSON"
| null elements = Just (D.App D.List "JSON")
| otherwise = Nothing

in D.App (D.Field "json" "array") (D.ListLit elementType elements)
Expand Down
3 changes: 3 additions & 0 deletions dhall-json/tasty/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ testTree =
"./tasty/data/quoted"
, testJSONToDhall "./tasty/data/emptyAlternative"
, testJSONToDhall "./tasty/data/emptyObject"
, testJSONToDhall "./tasty/data/emptyList"
, testJSONToDhall "./tasty/data/emptyObjectStrongType"
, testJSONToDhall "./tasty/data/emptyListStrongType"
, Test.Tasty.testGroup "Nesting"
[ testDhallToJSON "./tasty/data/nesting0"
, testDhallToJSON "./tasty/data/nesting1"
Expand Down
17 changes: 17 additions & 0 deletions dhall-json/tasty/data/emptyList.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
λ(JSON : Type)
λ ( json
: { array :
List JSON JSON
, bool :
Bool JSON
, null :
JSON
, number :
Double JSON
, object :
List { mapKey : Text, mapValue : JSON } JSON
, string :
Text JSON
}
)
json.array ([] : List JSON)
1 change: 1 addition & 0 deletions dhall-json/tasty/data/emptyList.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
17 changes: 17 additions & 0 deletions dhall-json/tasty/data/emptyListSchema.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(JSON : Type)
( json
: { array :
List JSON JSON
, bool :
Bool JSON
, null :
JSON
, number :
Double JSON
, object :
List { mapKey : Text, mapValue : JSON } JSON
, string :
Text JSON
}
)
JSON
1 change: 1 addition & 0 deletions dhall-json/tasty/data/emptyListStrongType.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[] : List Natural
1 change: 1 addition & 0 deletions dhall-json/tasty/data/emptyListStrongType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions dhall-json/tasty/data/emptyListStrongTypeSchema.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List Natural
1 change: 1 addition & 0 deletions dhall-json/tasty/data/emptyObjectStrongType.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[] : List { mapKey : Text, mapValue : Natural }
1 change: 1 addition & 0 deletions dhall-json/tasty/data/emptyObjectStrongType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions dhall-json/tasty/data/emptyObjectStrongTypeSchema.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List { mapKey : Text, mapValue : Natural }

0 comments on commit eee7524

Please sign in to comment.