Skip to content

Commit

Permalink
Merge pull request #10 from clovyr/revert-9-gabriel/quote_more
Browse files Browse the repository at this point in the history
Revert "Single-quote more pathological strings"
  • Loading branch information
patrickmn authored Jul 27, 2020
2 parents 8293589 + 439ad35 commit c0e9ae4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
25 changes: 4 additions & 21 deletions src/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,53 +122,36 @@ encodeText canMultiline alwaysQuote level s
-- s is a value, not a map key, and contains newlines; can be inserted
-- literally with `|` syntax
| canMultiline && "\n" `Text.isSuffixOf` s = encodeLines level (Text.lines s)

| shouldSingleQuote = singleQuote

-- s is a number, date, or boolString; single-quote
| Text.all isNumberOrDateRelated s || isBoolString = singleQuote
-- s should be quoted, AND s is not unsafe; single-quote
| alwaysQuote && unquotable = singleQuote
-- s should be quoted, OR s might be unsafe; double-quote
| alwaysQuote || not unquotable = bl $ Data.Aeson.encode s

-- otherwise; no quotes
| otherwise = noQuote
where
noQuote = b (Text.Encoding.encodeUtf8 s)

shouldSingleQuote =
(Text.all isNumberOrDateRelated s && Text.any isDigit s)
|| isBoolString
|| Text.isPrefixOf " " s
|| Text.isSuffixOf " " s
-- Quote if the string looks like a key
|| Text.isInfixOf ":" s
-- s should be quoted, AND s is not unsafe; single-quote
|| (alwaysQuote && unquotable)

singleQuote = bs "'" <> noQuote <> bs "'"

headS = Text.head s

unquotable -- s is unquotable if all are True
=
s /= "" && -- s is not empty
Text.all isAllowed s && -- s consists of acceptable chars
(Data.Char.isAlpha headS || -- head of s is a char in A-Z or a-z or indicates a filepath
headS == '/')

isBoolString
| Text.length s > 5 = False
| otherwise =
case Text.toLower s of
"true" -> True
"false" -> True
_ -> False

isSafeAscii c =
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == '/' || c == '_' || c == '.' || c == '='

isNumberOrDateRelated c = isDigit c || c == '.' || c == 'e' || c == '-'

isAllowed c = isSafeAscii c || c == '-' || c == ':' || c == ' '

encodeLines :: Int -> [Text] -> Builder
Expand Down
12 changes: 3 additions & 9 deletions test/Test/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,24 @@ tcDataTypes =
"piString": "3.14",
"expString": "1e3",
"leadingSpace" : " leading space",
"trailingSpace" : "trailing space ",
"colon" : "colon:",
"leadingSymbol" : "!leading symbol",
"asteriskString": "*",
"multiLine": "The first line is followed by the\nsecond line\n",
"multiLineWithSpaces": " This has extra\n spaces at the beginning\n",
"notMultiline": "This won't be\nmulti-lined",
"list": ["foo", "bar", "baz"],
"listEmpty": [],
"e": null
"listEmpty": []
}
|]
, tcOutput =
[s|asteriskString: "*"
boolString: 'true'
colon: 'colon:'
dateString: '2038-01-19'
e: null
emptyObject: {}
expString: '1e3'
isFalse: false
isTrue: true
leadingSpace: ' leading space'
leadingSpace: " leading space"
leadingSymbol: "!leading symbol"
list:
- foo
Expand All @@ -99,7 +94,6 @@ nullValue: null
numberString: '12345'
piString: '3.14'
"quoted ! key": true
trailingSpace: 'trailing space '
|]
, tcAlwaysQuote = False
}
Expand Down Expand Up @@ -187,7 +181,7 @@ spec:
- command:
- /data/bin/foo
- "--port=7654"
image: 'ubuntu:latest'
image: ubuntu:latest
name: "{{ .Release.Name }}-container"
ports:
- containerPort: 7654
Expand Down

0 comments on commit c0e9ae4

Please sign in to comment.