diff --git a/nb-apis/adapters-api/src/main/java/io/nosqlbench/adapters/api/templating/StrInterpolator.java b/nb-apis/adapters-api/src/main/java/io/nosqlbench/adapters/api/templating/StrInterpolator.java index 08271ada2..f78de1a25 100644 --- a/nb-apis/adapters-api/src/main/java/io/nosqlbench/adapters/api/templating/StrInterpolator.java +++ b/nb-apis/adapters-api/src/main/java/io/nosqlbench/adapters/api/templating/StrInterpolator.java @@ -168,10 +168,11 @@ public class StrInterpolator implements Function { public static class MultiMap extends StrLookup { - private final List> maps = new ArrayList<>(); private final String warnPrefix = "UNSET"; private final Map accesses = new LinkedHashMap<>(); private final Map extractedDefaults = new LinkedHashMap<>(); + private final Map overrides = new LinkedHashMap<>(); + private final List> maps = new ArrayList<>(); public void add(Map addedMap) { maps.add(addedMap); @@ -179,25 +180,57 @@ public class StrInterpolator implements Function { @Override public String lookup(String key) { + //String original = key; String value = null; + char substitution = ' '; String[] parts = key.split("[:,]", 2); if (parts.length == 2) { key = parts[0]; value = parts[1]; + if ( value.length() > 0 ) { + substitution = value.charAt(0); + } else { + substitution = ' '; + } + if ( substitution == '-' || substitution == '=' || substitution == '?' || substitution == '+') { + if ( value.length() < 2 ) { + value = null; + } else { + value = value.substring(1); + } + } else { + substitution = ' '; + } if (!extractedDefaults.containsKey(key)) { - extractedDefaults.put(key,value); + extractedDefaults.put(key, value); + } + if (!overrides.containsKey(key)) { + if ( substitution == '=' ) { + overrides.put(key, value); + //System.out.println(key+"="+value); + } else if ( substitution == '?' ) { + throw new NullPointerException("Parameter "+key+" is not set"); + } } } - for (Map map : maps) { - Object val = map.get(key); + if ( substitution != '+' ) { + Object val = overrides.get(key); if (val != null) { value = val.toString(); - break; + //System.out.println("for: '"+original+"': "+key+"->"+value); + } else { + for (Map map : maps) { + val = map.get(key); + if (val != null) { + value = val.toString(); + break; + } + } } + value = (value==null? extractedDefaults.get(key) : value); } - value = (value==null? extractedDefaults.get(key) : value); value = (value != null) ? value : warnPrefix + ":" + key; diff --git a/nb-apis/adapters-api/src/test/java/io/nosqlbench/adapters/api/templating/StrInterpolatorTest.java b/nb-apis/adapters-api/src/test/java/io/nosqlbench/adapters/api/templating/StrInterpolatorTest.java index 0e59edbc6..17cf95ce8 100644 --- a/nb-apis/adapters-api/src/test/java/io/nosqlbench/adapters/api/templating/StrInterpolatorTest.java +++ b/nb-apis/adapters-api/src/test/java/io/nosqlbench/adapters/api/templating/StrInterpolatorTest.java @@ -16,6 +16,8 @@ package io.nosqlbench.adapters.api.templating; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -115,6 +117,30 @@ public class StrInterpolatorTest { assertThat(a).isEqualTo("START\n# TEMPLATE(blahblah,blah)\nUniform(0,1000000000)->int;"); } + @Test + public void sustitutionTests() { + StrInterpolator interp = new StrInterpolator(abcd); + String a = interp.apply("-${setkey:=setme}-${setkey}-"); + assertThat(a).isEqualTo("-setme-setme-"); + String b = interp.apply("-${setkey2:-setme}-${setkey2:-setyou}-"); + assertThat(b).isEqualTo("-setme-setyou-"); + String c = interp.apply("-${setkey:=setme}-${setkey3:+setme}-${setkey:+setyou}-"); + assertThat(c).isEqualTo("-setme-setme-setyou-"); + } + + @Test + public void shouldThrowException() { + StrInterpolator interp = new StrInterpolator(abcd); + + // Test for missing variable or invalid syntax + Exception exception = assertThrows(NullPointerException.class, () -> { + interp.apply("-${unsetKey:?unset exception}-"); + }); + + // Verify the exception message if necessary + assertThat(exception.getMessage()).contains("unsetKey"); + } + @Test public void shouldReturnWarningWhenUnmatched() { StrInterpolator interp = new StrInterpolator(abcd);