generator refactoring

This commit is contained in:
rusefillc 2021-12-11 04:42:56 -05:00
parent 2656f4b7b7
commit 09c81c6a2e
4 changed files with 44 additions and 50 deletions

Binary file not shown.

View File

@ -9,7 +9,6 @@ import org.jetbrains.annotations.NotNull;
import java.io.CharArrayWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.TreeSet;
import static com.rusefi.ConfigField.unquote;
@ -41,7 +40,8 @@ public class DataLogConsumer implements ConfigurationConsumer {
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
if (state.stack.isEmpty()) {
DataLogFieldIterator iterator = new DataLogFieldIterator(structure.tsFields, "");
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(structure.tsFields, "",
this::handle);
iterator.loop();
String content = iterator.sb.toString();
tsWriter.append(content);
@ -54,36 +54,10 @@ public class DataLogConsumer implements ConfigurationConsumer {
}
}
class DataLogFieldIterator extends FieldIterator {
private final String prefix;
StringBuilder sb = new StringBuilder();
public DataLogFieldIterator(List<ConfigField> fields, String prefix) {
super(fields);
this.prefix = prefix;
}
@Override
public void end() {
String content = handle(cf, prefix);
sb.append(content);
super.end();
}
}
private String handle(ConfigField configField, String prefix) {
if (configField.getName().contains("unused"))
return "";
ConfigStructure cs = configField.getState().structures.get(configField.getType());
if (cs != null) {
String extraPrefix = cs.withPrefix ? configField.getName() + "_" : "";
DataLogFieldIterator fieldIterator = new DataLogFieldIterator(cs.tsFields, extraPrefix);
fieldIterator.loop();
return fieldIterator.sb.toString();
}
if (configField.isArray()) {
return "";
@ -114,7 +88,7 @@ public class DataLogConsumer implements ConfigurationConsumer {
comment = (comments.length > 0) ? comments[0] : "";
if (comment.isEmpty())
comment = prefix + unquote(configField.getName());
comment = prefix + unquote(configField.getName());
if (comment.charAt(0) != '"')
comment = quote(comment);

View File

@ -35,8 +35,10 @@ public class GaugeConsumer implements ConfigurationConsumer {
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
if (state.stack.isEmpty()) {
FieldIterator iterator = new FieldIterator(structure.tsFields);
String content = handleFields(structure, iterator, "");
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(structure.tsFields, "",
this::handle);
iterator.loop();
String content = iterator.sb.toString();
charArrayWriter.append(content);
}
@ -47,26 +49,7 @@ public class GaugeConsumer implements ConfigurationConsumer {
}
}
private String handleFields(ConfigStructure structure, FieldIterator iterator, String prefix) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < structure.tsFields.size(); i++) {
iterator.start(i);
String content = handle(iterator.cf, prefix);
sb.append(content);
iterator.end();
}
return sb.toString();
}
private String handle(ConfigField configField, String prefix) {
ConfigStructure cs = configField.getState().structures.get(configField.getType());
if (cs != null) {
String extraPrefix = cs.withPrefix ? configField.getName() + "_" : "";
return handleFields(cs, new FieldIterator(cs.tsFields), extraPrefix);
}
String comment = getComment("", configField, state.variableRegistry);
comment = ConfigField.unquote(comment);
if (!prefix.isEmpty()) {

View File

@ -0,0 +1,37 @@
package com.rusefi.output;
import com.rusefi.ConfigField;
import java.util.List;
class PerFieldWithStructuresIterator extends FieldIterator {
private final String prefix;
private final Strategy strategy;
StringBuilder sb = new StringBuilder();
public PerFieldWithStructuresIterator(List<ConfigField> fields, String prefix, Strategy strategy) {
super(fields);
this.prefix = prefix;
this.strategy = strategy;
}
@Override
public void end() {
ConfigStructure cs = cf.getState().structures.get(cf.getType());
String content;
if (cs != null) {
String extraPrefix = cs.withPrefix ? cf.getName() + "_" : "";
PerFieldWithStructuresIterator fieldIterator = new PerFieldWithStructuresIterator(cs.tsFields, extraPrefix, strategy);
fieldIterator.loop();
content = fieldIterator.sb.toString();
} else {
content = strategy.process(cf, prefix);
}
sb.append(content);
super.end();
}
interface Strategy {
String process(ConfigField configField, String prefix);
}
}