Skip to content

Commit

Permalink
Support single quotes in table name
Browse files Browse the repository at this point in the history
Fixes #36
  • Loading branch information
mwanji committed Jul 27, 2016
1 parent 2d2a2c2 commit 214143f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# toml4j Changelog

## 0.7.1 / 2016-07-27

* [Support literal strings in table names](https://github.com/mwanji/toml4j/issues/36) (thanks to __[bruno-medeiros](https://github.com/bruno-medeiros)__)

## 0.7.0 / 2016-07-12

## Added
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/moandjiezana/toml/Identifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private static boolean isValidTable(String name, Context context) {
break;
}

if (c == '"') {
if (Keys.isQuote(c)) {
if (!quoteAllowed) {
valid = false;
} else if (quoted && trimmed.charAt(i - 1) != '\\') {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/moandjiezana/toml/IdentifierConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Identifier convert(String s, AtomicInteger index, Context context) {

for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
char c = s.charAt(i);
if (c == '"' && (i == 0 || s.charAt(i - 1) != '\\')) {
if (Keys.isQuote(c) && (i == 0 || s.charAt(i - 1) != '\\')) {
quoted = !quoted;
name.append('"');
name.append(c);
} else if (c == '\n') {
index.decrementAndGet();
break;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/moandjiezana/toml/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static Keys.Key[] split(String key) {
current = new StringBuilder();
continue;
}
if (c == '"' && (i == 0 || key.charAt(i - 1) != '\\')) {
if (isQuote(c) && (i == 0 || key.charAt(i - 1) != '\\')) {
quoted = !quoted;
indexable = false;
}
Expand All @@ -60,6 +60,10 @@ static Keys.Key[] split(String key) {

return splitKey.toArray(new Key[0]);
}

static boolean isQuote(char c) {
return c == '"' || c == '\'';
}

private Keys() {}
}
28 changes: 28 additions & 0 deletions src/test/java/com/moandjiezana/toml/TableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ public void should_get_empty_table() throws Exception {
public void should_return_null_for_missing_table() throws Exception {
assertNull(new Toml().getTable("a"));
}

@Test
public void should_accept_table_name_with_basic_string() {
Toml toml = new Toml().read("[\"a\"]\nb = 'b'");

assertEquals("b", toml.getString("\"a\".b"));
}

@Test
public void should_accept_table_name_part_with_basic_string() {
Toml toml = new Toml().read("[target.\"cfg(unix)\".dependencies]\nb = 'b'");

assertEquals("b", toml.getString("target.\"cfg(unix)\".dependencies.b"));
}

@Test
public void should_accept_table_name_with_literal_string() {
Toml toml = new Toml().read("['a']\nb = 'b'");

assertEquals("b", toml.getString("'a'.b"));
}

@Test
public void should_accept_table_name_part_with_literal_string() {
Toml toml = new Toml().read("[target.'cfg(unix)'.dependencies]\nb = 'b'");

assertEquals("b", toml.getString("target.'cfg(unix)'.dependencies.b"));
}

@Test
public void should_return_null_when_navigating_to_missing_value() throws Exception {
Expand Down

5 comments on commit 214143f

@bruno-medeiros
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this line: if (!Character.isWhitespace(prev) && prev != '.' && prev != '"') { in isValidTable(String name, Context context ? Shouldn't it be

if (!Character.isWhitespace(prev) && prev != '.' && !Keys.isQuote(prev)) {

@mwanji
Copy link
Owner Author

@mwanji mwanji commented on 214143f Aug 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out I can't get a unit test to fail because of the quote check and can even eliminate it altogether! I've pushed the unit tests to the wip branch (commit). Do you see anything I could test to make it fail?

@bruno-medeiros
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at the Toml spec again, it seems if (!Character.isWhitespace(prev) && prev != '.') { might be the correct code. In other words, this:

["asdfsd" "asdfsf"]

Would not be a valid table header, but it would have been allowed with your previous code.

@bruno-medeiros
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, just noticed now, there is an also an error in the way your are parsing quoted keys, as illustrated by these tests that should pass, but fail:

  @Test
  public void test() {
    Toml toml = new Toml().read("\"127.0.0.1\" = \"value\"");

    assertEquals("value", toml.getString("127.0.0.1"));
  }
  @Test
  public void test2() {
    Toml toml = new Toml().read("[dog.\"tater.man\"]\ntype = \"pug\"");

    assertEquals("pub", toml.getTable("dog").getTable("tater.man").getString("type"));
  }

test2 is based on the example mentioned here: https://github.com/toml-lang/toml#table , look for:

[dog."tater.man"]
type = "pug"

example.

@mwanji
Copy link
Owner Author

@mwanji mwanji commented on 214143f Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue seems to be the same as #33 . Could you comment or add a thumbs up there if you agree that the behaviour should be changed?

Please sign in to comment.