Skip to content

Commit

Permalink
fix(yaml): force quoting when Text values contains a colon char
Browse files Browse the repository at this point in the history
About
dhall-lang#1939 (comment)

With the current aeson-yaml version used in Dhall, a text value
containing a colon like this one: `Location: https://example.org` is
not quoted in the yaml representation:

```
> dhall-to-yaml <<< '{ headers = ["Location: https://example.org"] }'
headers:
  - Location: https://example.org
```

which is a bug as it doesn't represent the same data.
  • Loading branch information
paulrbr-fl committed Aug 31, 2020
1 parent 7810c4f commit 56bdc06
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dhall-yaml/src/Dhall/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ jsonToYaml json documents quoted =
style (Y.SStr s)
| "\n" `Text.isInfixOf` s =
Right (YE.untagged, YE.Literal YE.Clip YE.IndentAuto, s)
| quoted || Text.all isNumberOrDateRelated s || isBoolString =
| quoted || Text.all isNumberOrDateRelated s || isBoolString || Text.any isColon s =
Right (YE.untagged, YE.SingleQuoted, s)
where
-- For backwards compatibility with YAML 1.1, we need to add the following to the set of boolean values:
-- https://yaml.org/type/bool.html
isBoolString = Text.length s <= 5 &&
Text.toLower s `elem` ["y", "yes", "n", "no", "true", "false", "on", "off"]
isNumberOrDateRelated c = Char.isDigit c || c == '.' || c == 'e' || c == '-'
isColon c = c == ':'
style s =
YS.schemaEncoderScalar Y.coreSchemaEncoder s

Expand Down

0 comments on commit 56bdc06

Please sign in to comment.