Skip to content
Snippets Groups Projects
Commit 4437659f authored by Christoph Thelen's avatar Christoph Thelen
Browse files

Added number support for keys

parent 11c58140
No related merge requests found
......@@ -35,7 +35,11 @@ public class NovaView extends View {
}
public void setStartKeyArray(String key) {
this.startKey = encode("[\"" + key + "\"]");
if (isNumber(key)) {
this.startKey = encode("[" + key + "]");
} else {
this.startKey = encode("[\"" + key + "\"]");
}
}
public void setStartKeyArray(String... keys) {
......@@ -48,7 +52,11 @@ public class NovaView extends View {
}
public void setEndKeyArray(String key) {
this.endKey = encode("[\"" + key + "\"]");
if (isNumber(key)) {
this.endKey = encode("[" + key + "]");
} else {
this.endKey = encode("[\"" + key + "\"]");
}
}
public void setEndKeyArray(String... keys) {
......@@ -75,7 +83,9 @@ public class NovaView extends View {
private String toJsonArray(String[] strings) {
StringBuilder sb = new StringBuilder();
for (String str : strings) {
if (str.equals("{}")) {
if (isNumber(str)) {
sb.append(str + ",");
} else if (str.equals("{}")) {
sb.append(str + ",");
} else {
sb.append("\"" + str + "\"" + ",");
......@@ -88,8 +98,15 @@ public class NovaView extends View {
}
private String quote(String string) {
if (isNumber(string)) {
return encode(string);
}
return encode("\"" + string + "\"");
}
private boolean isNumber(String string) {
return string.matches("^[0-9]+$");
}
private String encode(String string) {
try {
......
......@@ -90,6 +90,30 @@ public class NovaViewTest {
assertEncodedEquals("key", "[\"foo\",\"bar\",{}]", v.getQueryString());
}
@Test
public void arrayKeysShouldNotEnquoteNumbers() {
NovaView v = new NovaView(null);
v.setKey("foo", "bar", "2");
assertEncodedEquals("key", "[\"foo\",\"bar\",2]", v.getQueryString());
}
@Test
public void singleKeysShouldNotEnquoteNumbers() {
NovaView v = new NovaView(null);
v.setKey("2");
assertEncodedEquals("key", "2", v.getQueryString());
}
@Test
public void singleArrayKeysShouldNotEnquoteNumbers() {
NovaView v1 = new NovaView(null);
NovaView v2 = new NovaView(null);
v1.setStartKeyArray("2");
v2.setEndKeyArray("2");
assertEncodedEquals("startkey", "[2]", v1.getQueryString());
assertEncodedEquals("endkey", "[2]", v2.getQueryString());
}
private void assertEncodedEquals(String key, String expected, String actual) {
try {
assertEquals(key + "=" + URLEncoder.encode(expected, "UTF-8"), actual);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment