mirror of
https://github.com/lightbend/config.git
synced 2025-03-19 05:40:26 +08:00
improve path rendering to avoid quoting hyphenated keys like foo-bar
This commit is contained in:
parent
81ce6038c7
commit
a3988e9b03
@ -139,11 +139,25 @@ final class Path {
|
||||
}
|
||||
|
||||
// this doesn't have a very precise meaning, just to reduce
|
||||
// noise from quotes in the rendered path
|
||||
// noise from quotes in the rendered path for average cases
|
||||
static boolean hasFunkyChars(String s) {
|
||||
for (int i = 0; i < s.length(); ++i) {
|
||||
int length = s.length();
|
||||
|
||||
if (length == 0)
|
||||
return false;
|
||||
|
||||
// if the path starts with something that could be a number,
|
||||
// we need to quote it because the number could be invalid,
|
||||
// for example it could be a hyphen with no digit afterward
|
||||
// or the exponent "e" notation could be mangled.
|
||||
char first = s.charAt(0);
|
||||
if (!(Character.isLetter(first)))
|
||||
return true;
|
||||
|
||||
for (int i = 1; i < length; ++i) {
|
||||
char c = s.charAt(i);
|
||||
if (Character.isLetterOrDigit(c) || c == ' ')
|
||||
|
||||
if (Character.isLetterOrDigit(c) || c == '-' || c == '_')
|
||||
continue;
|
||||
else
|
||||
return true;
|
||||
|
@ -41,12 +41,38 @@ class PathTest extends TestUtils {
|
||||
|
||||
@Test
|
||||
def pathRender() {
|
||||
assertEquals("foo", path("foo").render())
|
||||
assertEquals("foo.bar", path("foo", "bar").render())
|
||||
assertEquals("foo.\"bar*\"", path("foo", "bar*").render())
|
||||
assertEquals("\"foo.bar\"", path("foo.bar").render())
|
||||
assertEquals("foo bar", path("foo bar").render())
|
||||
assertEquals("\"\".\"\"", path("", "").render())
|
||||
case class RenderTest(expected: String, path: Path)
|
||||
|
||||
val tests = Seq(
|
||||
// simple one-element case
|
||||
RenderTest("foo", path("foo")),
|
||||
// simple two-element case
|
||||
RenderTest("foo.bar", path("foo", "bar")),
|
||||
// non-safe-char in an element
|
||||
RenderTest("foo.\"bar*\"", path("foo", "bar*")),
|
||||
// period in an element
|
||||
RenderTest("\"foo.bar\"", path("foo.bar")),
|
||||
// hyphen and underscore
|
||||
RenderTest("foo-bar", path("foo-bar")),
|
||||
RenderTest("foo_bar", path("foo_bar")),
|
||||
// starts with hyphen
|
||||
RenderTest("\"-foo\"", path("-foo")),
|
||||
// starts with number
|
||||
RenderTest("\"10foo\"", path("10foo")),
|
||||
// empty elements
|
||||
RenderTest("\"\".\"\"", path("", "")),
|
||||
// internal space
|
||||
RenderTest("\"foo bar\"", path("foo bar")),
|
||||
// leading and trailing spaces
|
||||
RenderTest("\" foo \"", path(" foo ")),
|
||||
// trailing space only
|
||||
RenderTest("\"foo \"", path("foo ")))
|
||||
|
||||
for (t <- tests) {
|
||||
assertEquals(t.expected, t.path.render())
|
||||
assertEquals(t.path, Parser.parsePath(t.expected))
|
||||
assertEquals(t.path, Parser.parsePath(t.path.render()))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user