trim spaces in tag specs, disallows spaces as delimiter between

This commit is contained in:
Jonathan Shook 2020-04-30 17:33:07 -05:00
parent ebc53ca3a9
commit 57762f3588
2 changed files with 36 additions and 17 deletions

View File

@ -88,23 +88,31 @@ public class TagFilter {
*/
public TagFilter(String filterSpec) {
if ((filterSpec != null) && (!filterSpec.isEmpty())) {
String[] keyvalues = filterSpec.split("[, ] *");
filterSpec=unquote(filterSpec);
String[] keyvalues = filterSpec.split("[,] *");
for (String assignment : keyvalues) {
String[] keyvalue = assignment.split("[:=]", 2);
String key = keyvalue[0];
String value = keyvalue.length > 1 ? keyvalue[1] : null;
if (value != null) {
if ((value.indexOf("\'") == 0) && ((value.indexOf("\'", 1) == (value.length() - 1)))) {
value = value.substring(1, value.length() - 1);
} else {
value = value.trim();
}
value = unquote(value);
value = value.trim();
}
filter.put(key, value);
}
}
}
private static String unquote(String filterSpec) {
for (String s : new String[]{"'","\""}) {
if (filterSpec.indexOf(s)==0 && filterSpec.indexOf(s,1)==filterSpec.length()-1) {
filterSpec=filterSpec.substring(1,filterSpec.length()-1);
}
}
return filterSpec;
}
/**
* Although this method could early-exit for certain conditions, the full tag matching logic
* is allowed to complete in order to present more complete diagnostic information back

View File

@ -36,7 +36,7 @@ public class TagFilterTest {
@Test
public void testEmptyTagFilterDoesMatch() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
put("a","tag");
}};
TagFilter tf = new TagFilter("");
@ -45,7 +45,7 @@ public class TagFilterTest {
@Test
public void testSomeFilterTagsNoItemTagsDoesNotMatch() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
}};
TagFilter tf = new TagFilter("tag=foo");
assertThat(tf.matches(itemtags).matched()).isFalse();
@ -54,7 +54,7 @@ public class TagFilterTest {
@Test
public void testEmptyTagFilterValueDoesMatch() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
put("one","two");
}};
TagFilter tf = new TagFilter("");
@ -64,14 +64,14 @@ public class TagFilterTest {
@Test
public void testMatchingTagKeyValueDoesMatch() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
put("one","two");
}};
TagFilter tf = new TagFilter("one");
TagFilter.Result result = tf.matches(itemtags);
assertThat(result.matched()).isTrue();
Map<String,String> itemtags2 = new HashMap<String,String>() {{
Map<String,String> itemtags2 = new HashMap<>() {{
put("one",null);
}};
assertThat(tf.matches(itemtags2).matched()).isTrue();
@ -80,7 +80,7 @@ public class TagFilterTest {
@Test
public void testMatchingKeyMismatchingValueDoesNotMatch() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
put("one","four");
}};
TagFilter tf = new TagFilter("one:two");
@ -90,7 +90,7 @@ public class TagFilterTest {
@Test
public void testMatchingKeyAndValueDoesMatch() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
put("one","four");
}};
TagFilter tf = new TagFilter("one:four");
@ -99,7 +99,7 @@ public class TagFilterTest {
@Test
public void testMatchingKeyAndValueRegexDoesMatch() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
put("one","four-five-six");
}};
TagFilter tfLeft = new TagFilter("one:'four-.*'");
@ -114,7 +114,7 @@ public class TagFilterTest {
Tagged tagged = new Tagged() {
@Override
public Map<String, String> getTags() {
return new HashMap<String,String>() {{
return new HashMap<>() {{
put("one","four-five-six");
put("two","three-seven-nine");
put("five",null);
@ -136,7 +136,7 @@ public class TagFilterTest {
@Test
public void testRawSubstringDoesNotMatchRegex() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
put("one","four-five-six");
}};
TagFilter tf = new TagFilter("one:'five'");
@ -145,11 +145,22 @@ public class TagFilterTest {
@Test
public void testAlternation() {
Map<String,String> itemtags = new HashMap<String,String>() {{
Map<String,String> itemtags = new HashMap<>() {{
put("one","four-five-six");
}};
TagFilter tf = new TagFilter("one:'four.*|seven'");
assertThat(tf.matches(itemtags).matched()).isTrue();
}
@Test
public void testLeadingSpaceTrimmedInQuotedTag() {
Map<String,String> itemtags = new HashMap<>() {{
put("phase","main");
}};
TagFilter tf = new TagFilter("\"phase: main\"");
assertThat(tf.matches(itemtags).matched()).isTrue();
}
}