new templating feature

This commit is contained in:
rusefillc 2022-11-14 22:49:51 -05:00
parent d67b89869f
commit d7c6e57eed
6 changed files with 37 additions and 14 deletions

Binary file not shown.

View File

@ -383,12 +383,7 @@ public class ConfigField {
// see testUnquote
public static String unquote(String token) {
int length = token.length();
if (length < 2)
return token;
if (token.charAt(0) == '\"' && token.charAt(token.length() - 1) == '\"')
return token.substring(1, length - 1);
return token;
return VariableRegistry.unquote(token);
}
public void setFromIterate(String iterateOriginalName, int iterateIndex) {

View File

@ -14,7 +14,7 @@ import java.util.*;
import static com.devexperts.logging.Logging.getLogging;
import static com.rusefi.ConfigField.BOOLEAN_T;
import static com.rusefi.ConfigField.unquote;
import static com.rusefi.VariableRegistry.unquote;
import static com.rusefi.output.JavaSensorsConsumer.quote;
/**

View File

@ -11,7 +11,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeSet;
import static com.rusefi.ConfigField.unquote;
import static com.rusefi.VariableRegistry.unquote;
import static org.abego.treelayout.internal.util.java.lang.string.StringUtil.quote;
public class DataLogConsumer implements ConfigurationConsumer {

View File

@ -76,6 +76,6 @@ public class VariableRegistryTest {
VariableRegistry registry = new VariableRegistry();
registry.readPrependValues(new StringReader("#define SINGLE 'L'\n" +
"#define DOUBLE \"R\""));
assertEquals("hello 'L' \"R\" 'L' \"R\"", registry.applyVariables("hello @@SINGLE@@ @@DOUBLE@@ @@SINGLE@@ @@DOUBLE@@"));
assertEquals("hello L R 'L' \"R\"", registry.applyVariables("hello @#SINGLE#@ @#DOUBLE#@ @@SINGLE@@ @@DOUBLE@@"));
}
}

View File

@ -12,6 +12,7 @@ import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -38,6 +39,7 @@ public class VariableRegistry {
// todo: smarter regex! See TsWriter.VAR which is a bit better but still not perfect
// todo: https://github.com/rusefi/rusefi/issues/3053 ?
private final Pattern VAR = Pattern.compile("(@@(.*?)@@)");
private final Pattern VAR_REMOVE_QUOTE = Pattern.compile("(@#(.*?)#@)");
public Map<String, Integer> intValues = new HashMap<>();
@ -45,6 +47,19 @@ public class VariableRegistry {
// todo: move thid logic to JavaFieldsConsumer since that's the consumer?
private final Map<String, String> javaDefinitions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
public static String unquote(String token) {
return VariableRegistry.unquote(token, '\"');
}
@NotNull
public static String unquote(String token, char quoteSymbol) {
int length = token.length();
if (length < 2)
return token;
if (token.charAt(0) == quoteSymbol && token.charAt(token.length() - 1) == quoteSymbol)
return token.substring(1, length - 1);
return token;
}
public void readPrependValues(String prependFile) throws IOException {
readPrependValues(new FileReader(prependFile));
}
@ -162,15 +177,28 @@ public class VariableRegistry {
public String applyVariables(String line) {
if (line == null)
return null;
line = process(line, VAR, key -> {
if (!data.containsKey(key))
throw new IllegalStateException("No such variable: [" + key + "]");
return data.get(key);
});
line = process(line, VAR_REMOVE_QUOTE, key -> {
if (!data.containsKey(key))
throw new IllegalStateException("No such variable: [" + key + "]");
String unquotedDouble = unquote(data.get(key));
return unquote(unquotedDouble, '\'');
});
return line;
}
@NotNull
private String process(String line, Pattern pattern, Function<String, String> function) {
Matcher m;
while ((m = VAR.matcher(line)).find()) {
while ((m = pattern.matcher(line)).find()) {
if (m.groupCount() < 2)
throw new IllegalStateException("Something broken in: [" + line + "]");
String key = m.group(2);
if (!data.containsKey(key))
throw new IllegalStateException("No such variable: [" + key + "]");
String s = data.get(key);
line = m.replaceFirst(s);
line = m.replaceFirst(function.apply(key));
}
return line;
}