Skip to content

Commit

Permalink
Encode CIDR string using single quote
Browse files Browse the repository at this point in the history
This change makes CIDR string encoded using the same quote as IP address.

Fixes clovyr#7
  • Loading branch information
TristanCacqueray committed Jul 3, 2020
1 parent fdaabaf commit d7728bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ encodeText canMultiline alwaysQuote level s
| canMultiline && "\n" `Text.isSuffixOf` s = encodeLines level (Text.lines s)
-- s is a number, date, or boolString; single-quote
| Text.all isNumberOrDateRelated s || isBoolString = singleQuote
-- s is a cird; single-quote
| isDigit headS && Text.all isCIDR tailS = 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
Expand All @@ -134,6 +136,7 @@ encodeText canMultiline alwaysQuote level s
noQuote = b (Text.Encoding.encodeUtf8 s)
singleQuote = bs "'" <> noQuote <> bs "'"
headS = Text.head s
tailS = Text.tail s
unquotable -- s is unquotable if all are True
=
s /= "" && -- s is not empty
Expand All @@ -152,6 +155,7 @@ encodeText canMultiline alwaysQuote level s
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == '/' || c == '_' || c == '.' || c == '='
isNumberOrDateRelated c = isDigit c || c == '.' || c == 'e' || c == '-'
isCIDR c = isDigit c || c == '.' || c == '/'
isAllowed c = isSafeAscii c || c == '-' || c == ':' || c == ' '

encodeLines :: Int -> [Text] -> Builder
Expand Down
24 changes: 22 additions & 2 deletions test/Test/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ data TestCase =

testCases :: [TestCase]
testCases =
[tcDataTypes, tcNestedListsAndObjects, tcQuoted, tcEmptyList, tcHelloWorld]
[tcDataTypes, tcNestedListsAndObjects, tcQuoted, tcNetworkQuote, tcEmptyList, tcHelloWorld]

tcEmptyList :: TestCase
tcEmptyList =
Expand Down Expand Up @@ -132,6 +132,7 @@ tcNestedListsAndObjects =
{
"command": [
"/data/bin/foo",
"/42",
"--port=7654"
],
"image": "ubuntu:latest",
Expand Down Expand Up @@ -180,6 +181,7 @@ spec:
containers:
- command:
- /data/bin/foo
- /42
- "--port=7654"
image: ubuntu:latest
name: "{{ .Release.Name }}-container"
Expand Down Expand Up @@ -209,6 +211,24 @@ tcQuoted =
, tcAlwaysQuote = True
}

tcNetworkQuote :: TestCase
tcNetworkQuote =
TestCase
{ tcName = "Network CIDR single quote"
, tcInput =
[s|
{ "ip": "127.0.0.1"
, "net": "127.0.0.1/8"
}
|]
, tcOutput =
[s|ip: '127.0.0.1'
net: '127.0.0.1/8'
|]
, tcAlwaysQuote = False
}


tcHelloWorld :: TestCase
tcHelloWorld =
TestCase
Expand All @@ -225,7 +245,7 @@ tcHelloWorld =
}
}
]

}
}
|]
Expand Down

0 comments on commit d7728bf

Please sign in to comment.