Skip to content
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

Fix dhall-to-yaml to quote special strings #1474

Merged
merged 2 commits into from
Oct 25, 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
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: "*"