Add bash style variable substitution types (#2096)

This commit is contained in:
Dave Fisher 2024-11-21 12:36:07 -08:00 committed by GitHub
parent da609d76da
commit 6e83655b57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 6 deletions

View File

@ -168,10 +168,11 @@ public class StrInterpolator implements Function<String, String> {
public static class MultiMap extends StrLookup<String> { public static class MultiMap extends StrLookup<String> {
private final List<Map<String, ?>> maps = new ArrayList<>();
private final String warnPrefix = "UNSET"; private final String warnPrefix = "UNSET";
private final Map<String,String> accesses = new LinkedHashMap<>(); private final Map<String,String> accesses = new LinkedHashMap<>();
private final Map<String,String> extractedDefaults = new LinkedHashMap<>(); private final Map<String,String> extractedDefaults = new LinkedHashMap<>();
private final Map<String,String> overrides = new LinkedHashMap<>();
private final List<Map<String, ?>> maps = new ArrayList<>();
public void add(Map<String, ?> addedMap) { public void add(Map<String, ?> addedMap) {
maps.add(addedMap); maps.add(addedMap);
@ -179,25 +180,57 @@ public class StrInterpolator implements Function<String, String> {
@Override @Override
public String lookup(String key) { public String lookup(String key) {
//String original = key;
String value = null; String value = null;
char substitution = ' ';
String[] parts = key.split("[:,]", 2); String[] parts = key.split("[:,]", 2);
if (parts.length == 2) { if (parts.length == 2) {
key = parts[0]; key = parts[0];
value = parts[1]; 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)) { 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");
}
}
} }
if ( substitution != '+' ) {
Object val = overrides.get(key);
if (val != null) {
value = val.toString();
//System.out.println("for: '"+original+"': "+key+"->"+value);
} else {
for (Map<String, ?> map : maps) { for (Map<String, ?> map : maps) {
Object val = map.get(key); val = map.get(key);
if (val != null) { if (val != null) {
value = val.toString(); value = val.toString();
break; break;
} }
} }
}
value = (value==null? extractedDefaults.get(key) : value); value = (value==null? extractedDefaults.get(key) : value);
}
value = (value != null) ? value : warnPrefix + ":" + key; value = (value != null) ? value : warnPrefix + ":" + key;

View File

@ -16,6 +16,8 @@
package io.nosqlbench.adapters.api.templating; package io.nosqlbench.adapters.api.templating;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
@ -115,6 +117,30 @@ public class StrInterpolatorTest {
assertThat(a).isEqualTo("START\n# TEMPLATE(blahblah,blah)\nUniform(0,1000000000)->int;"); 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 @Test
public void shouldReturnWarningWhenUnmatched() { public void shouldReturnWarningWhenUnmatched() {
StrInterpolator interp = new StrInterpolator(abcd); StrInterpolator interp = new StrInterpolator(abcd);