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

Encode CIDR string using single quote #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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