new templating feature
This commit is contained in:
parent
7ba4f98dc6
commit
830a9cbd0b
Binary file not shown.
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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@@"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue