generator refactoring
This commit is contained in:
parent
2656f4b7b7
commit
09c81c6a2e
Binary file not shown.
|
@ -9,7 +9,6 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import java.io.CharArrayWriter;
|
import java.io.CharArrayWriter;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
import static com.rusefi.ConfigField.unquote;
|
import static com.rusefi.ConfigField.unquote;
|
||||||
|
@ -41,7 +40,8 @@ public class DataLogConsumer implements ConfigurationConsumer {
|
||||||
@Override
|
@Override
|
||||||
public void handleEndStruct(ConfigStructure structure) throws IOException {
|
public void handleEndStruct(ConfigStructure structure) throws IOException {
|
||||||
if (state.stack.isEmpty()) {
|
if (state.stack.isEmpty()) {
|
||||||
DataLogFieldIterator iterator = new DataLogFieldIterator(structure.tsFields, "");
|
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(structure.tsFields, "",
|
||||||
|
this::handle);
|
||||||
iterator.loop();
|
iterator.loop();
|
||||||
String content = iterator.sb.toString();
|
String content = iterator.sb.toString();
|
||||||
tsWriter.append(content);
|
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) {
|
private String handle(ConfigField configField, String prefix) {
|
||||||
if (configField.getName().contains("unused"))
|
if (configField.getName().contains("unused"))
|
||||||
return "";
|
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()) {
|
if (configField.isArray()) {
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
|
|
|
@ -35,8 +35,10 @@ public class GaugeConsumer implements ConfigurationConsumer {
|
||||||
@Override
|
@Override
|
||||||
public void handleEndStruct(ConfigStructure structure) throws IOException {
|
public void handleEndStruct(ConfigStructure structure) throws IOException {
|
||||||
if (state.stack.isEmpty()) {
|
if (state.stack.isEmpty()) {
|
||||||
FieldIterator iterator = new FieldIterator(structure.tsFields);
|
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(structure.tsFields, "",
|
||||||
String content = handleFields(structure, iterator, "");
|
this::handle);
|
||||||
|
iterator.loop();
|
||||||
|
String content = iterator.sb.toString();
|
||||||
charArrayWriter.append(content);
|
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) {
|
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);
|
String comment = getComment("", configField, state.variableRegistry);
|
||||||
comment = ConfigField.unquote(comment);
|
comment = ConfigField.unquote(comment);
|
||||||
if (!prefix.isEmpty()) {
|
if (!prefix.isEmpty()) {
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue