refactoring - mostly reducing complexity of code flow, end goal is making class testable

This commit is contained in:
rusefillc 2023-02-11 14:42:20 -05:00
parent 6e4028f2c3
commit a0b812f47d
2 changed files with 17 additions and 12 deletions

View File

@ -176,6 +176,7 @@ public class ConfigDefinition {
new TriggerWheelTSLogic().execute(triggersInputFolder, state.getVariableRegistry());
if (pinoutLogic != null) {
pinoutLogic.readFiles();
pinoutLogic.registerBoardSpecificPinNames(state.getVariableRegistry(), state, parseState);
}

View File

@ -30,11 +30,13 @@ public class PinoutLogic {
private final ArrayList<PinState> globalList = new ArrayList<>();
private final Map</*id*/String, /*tsName*/String> tsNameById = new TreeMap<>();
private final StringBuilder header = new StringBuilder("//DO NOT EDIT MANUALLY, let automation work hard.\n\n");
private final List<String> inputFiles;
public PinoutLogic(String boardName, File[] boardYamlFiles) {
log.info(boardName + ": " + Arrays.toString(boardYamlFiles));
this.boardName = boardName;
this.boardYamlFiles = boardYamlFiles;
inputFiles = getInputFilesList(boardName, boardYamlFiles);
}
private static Map.Entry<String, Value> find(EnumsReader.EnumState enumList, String id) {
@ -120,10 +122,6 @@ public class PinoutLogic {
}
String keyValueForm = VariableRegistry.getHumanSortedTsKeyValueString(pinMap);
return new EnumPair(keyValueForm, simpleForm.toString());
// String shorterForm = smartForm.length() < simpleForm.length() ? smartForm.toString() : simpleForm.toString();
//
// return new EnumPair(shorterForm, simpleForm.toString());
}
private static void appendCommaIfNeeded(StringBuilder sb) {
@ -143,19 +141,19 @@ public class PinoutLogic {
}
@SuppressWarnings("unchecked")
private void readMetaInfo(File yamlFile) throws IOException {
private void readMetaInfo(String yamlName, Reader reader) {
Yaml yaml = new Yaml();
Map<String, Object> yamlData = yaml.load(new FileReader(yamlFile));
Map<String, Object> yamlData = yaml.load(reader);
if (yamlData == null) {
SystemOut.println("Null yaml for " + yamlFile);
SystemOut.println("Null yaml for " + yamlName);
return;
}
List<Map<String, Object>> data = (List<Map<String, Object>>) yamlData.get("pins");
if (data == null) {
SystemOut.println("Null yaml for " + yamlFile);
SystemOut.println("Null yaml for " + yamlName);
return;
}
log.info("Got from " + yamlFile + ": " + data);
log.info("Got from " + yamlName + ": " + data);
Objects.requireNonNull(data, "data");
for (Map<String, Object> pin : data) {
Object pinId = pin.get("id");
@ -211,7 +209,6 @@ public class PinoutLogic {
}
public void registerBoardSpecificPinNames(VariableRegistry registry, ReaderStateImpl state, DefinitionsState parseState) throws IOException {
readFiles();
registerPins(boardName, globalList, registry, state, parseState);
try (FileWriter getTsNameByIdFile = new FileWriter(boardName + PinoutLogic.CONNECTORS + File.separator + "generated_ts_name_by_pin.cpp")) {
@ -235,15 +232,22 @@ public class PinoutLogic {
}
}
private void readFiles() throws IOException {
public void readFiles() throws IOException {
for (File yamlFile : boardYamlFiles) {
header.append("// auto-generated by PinoutLogic.java based on " + yamlFile + "\n");
readMetaInfo(yamlFile);
readMetaInfo(yamlFile.toString(), new FileReader(yamlFile));
}
log.info("Got from " + boardYamlFiles.length + " file(s): " + this);
}
/**
* @return list of affected files so that we can generate total unique ID
*/
public List<String> getInputFiles() {
return inputFiles;
}
private static List<String> getInputFilesList(String boardName, File[] boardYamlFiles) {
List<String> result = new ArrayList<>();
for (File yamlFile : boardYamlFiles) {
result.add(boardName + PinoutLogic.CONNECTORS + File.separator + yamlFile.getName());