Skip to content

Commit

Permalink
Fix dhall-to-yaml to quote special strings (#1474)
Browse files Browse the repository at this point in the history
Fixes #1472

This also refactors the code to a form that was easier for me to understand
  • Loading branch information
Gabriella439 authored Oct 25, 2019
1 parent 90315e9 commit 8e7586b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 28 deletions.
35 changes: 8 additions & 27 deletions dhall-json/src/Dhall/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,16 @@ jsonToYaml json documents quoted =
$ fmap (Data.ByteString.Lazy.toStrict. (Data.YAML.Aeson.encodeValue' schemaEncoder YT.UTF8). (:[]))
$ Data.Vector.toList elems
_ -> Data.ByteString.Lazy.toStrict (Data.YAML.Aeson.encodeValue' schemaEncoder YT.UTF8 [json])

where
defaultSchemaEncoder = YS.setScalarStyle style Y.coreSchemaEncoder

defaultEncodeStr s = case () of
()
| "\n" `Text.isInfixOf` s -> Right (YE.untagged, YE.Literal YE.Clip YE.IndentAuto, s)
| YS.isAmbiguous Y.coreSchemaResolver s -> Right (YE.untagged, YE.SingleQuoted, s)
| otherwise -> Right (YE.untagged, YE.Plain, s)

style s = case s of
Y.SNull -> Right (YE.untagged, YE.Plain, "null")
Y.SBool bool -> Right (YE.untagged, YE.Plain, YS.encodeBool bool)
Y.SFloat double -> Right (YE.untagged, YE.Plain, YS.encodeDouble double)
Y.SInt int -> Right (YE.untagged, YE.Plain, YS.encodeInt int)
Y.SStr text -> defaultEncodeStr text
Y.SUnknown t v -> Right (t, YE.SingleQuoted, v)
style (Y.SStr s)
| "\n" `Text.isInfixOf` s =
Right (YE.untagged, YE.Literal YE.Clip YE.IndentAuto, s)
| quoted =
Right (YE.untagged, YE.SingleQuoted, s)
style s =
YS.schemaEncoderScalar Y.coreSchemaEncoder s

customStyle (Y.SStr s) = case () of
()
| "\n" `Text.isInfixOf` s -> Right (YE.untagged, YE.Literal YE.Clip YE.IndentAuto, s)
| otherwise -> Right (YE.untagged, YE.SingleQuoted, s)
customStyle scalar = (YS.schemaEncoderScalar defaultSchemaEncoder) scalar

customSchemaEncoder = YS.setScalarStyle customStyle defaultSchemaEncoder

schemaEncoder = if quoted
then customSchemaEncoder
else defaultSchemaEncoder
schemaEncoder = YS.setScalarStyle style Y.coreSchemaEncoder
#else
Data.ByteString.Lazy.toStrict $ case (documents, json) of
(True, Data.Aeson.Array elems)
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 @@ -36,6 +36,9 @@ testTree =
, testDhallToYaml
Dhall.Yaml.defaultOptions
"./tasty/data/normal"
, testDhallToYaml
Dhall.Yaml.defaultOptions
"./tasty/data/special"
, testDhallToYaml
(Dhall.Yaml.defaultOptions { Dhall.Yaml.quoted = True })
"./tasty/data/quoted"
Expand Down
2 changes: 1 addition & 1 deletion dhall-json/tasty/data/normal.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
bool_value: true
int_value: 1
string_value: 2000-01-01
string_value: "2000-01-01"
text: |
Plain text
yes: y
1 change: 1 addition & 0 deletions dhall-json/tasty/data/special.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ foo = "*" }
1 change: 1 addition & 0 deletions dhall-json/tasty/data/special.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: "*"

0 comments on commit 8e7586b

Please sign in to comment.