diff --git a/src/Data/Aeson/Yaml.hs b/src/Data/Aeson/Yaml.hs index 4280d67..c9070c3 100644 --- a/src/Data/Aeson/Yaml.hs +++ b/src/Data/Aeson/Yaml.hs @@ -122,38 +122,24 @@ 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 = @@ -161,14 +147,11 @@ encodeText canMultiline alwaysQuote level s "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 diff --git a/test/Test/Data/Aeson/Yaml.hs b/test/Test/Data/Aeson/Yaml.hs index 0eb52a0..55c5fc4 100644 --- a/test/Test/Data/Aeson/Yaml.hs +++ b/test/Test/Data/Aeson/Yaml.hs @@ -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 @@ -99,7 +94,6 @@ nullValue: null numberString: '12345' piString: '3.14' "quoted ! key": true -trailingSpace: 'trailing space ' |] , tcAlwaysQuote = False } @@ -187,7 +181,7 @@ spec: - command: - /data/bin/foo - "--port=7654" - image: 'ubuntu:latest' + image: ubuntu:latest name: "{{ .Release.Name }}-container" ports: - containerPort: 7654