mirror of
https://github.com/lightbend/config.git
synced 2025-03-22 15:20: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
|
// 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) {
|
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);
|
char c = s.charAt(i);
|
||||||
if (Character.isLetterOrDigit(c) || c == ' ')
|
|
||||||
|
if (Character.isLetterOrDigit(c) || c == '-' || c == '_')
|
||||||
continue;
|
continue;
|
||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
|
@ -41,12 +41,38 @@ class PathTest extends TestUtils {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
def pathRender() {
|
def pathRender() {
|
||||||
assertEquals("foo", path("foo").render())
|
case class RenderTest(expected: String, path: Path)
|
||||||
assertEquals("foo.bar", path("foo", "bar").render())
|
|
||||||
assertEquals("foo.\"bar*\"", path("foo", "bar*").render())
|
val tests = Seq(
|
||||||
assertEquals("\"foo.bar\"", path("foo.bar").render())
|
// simple one-element case
|
||||||
assertEquals("foo bar", path("foo bar").render())
|
RenderTest("foo", path("foo")),
|
||||||
assertEquals("\"\".\"\"", path("", "").render())
|
// 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
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user