Skip to content

Commit

Permalink
fix(includes): iterate airports by rule, then by airport (#168)
Browse files Browse the repository at this point in the history
* fix(includes): iterate airports by rule, then by airport

Currently, we iterate each of the airport directories and within that, iterate through the rules.
This has the effect of making the ordering of the rules essentially useless, as all the rules get
applied to the current airport before moving on. This change modifies the processing order so it
applies each rule in turn to each folder, which produces the desired effect.

fix #167

* Remove test variable
AndyTWF authored Jan 6, 2022
1 parent c407af3 commit badda79
Showing 6 changed files with 266 additions and 74 deletions.
92 changes: 68 additions & 24 deletions src/Compiler/Config/ConfigIncludeLoader.cs
Original file line number Diff line number Diff line change
@@ -16,16 +16,18 @@ public class ConfigIncludeLoader
public ConfigIncludeLoader(
FolderInclusionRuleLoaderFactory folderFactory,
FileInclusionRuleLoaderFactory fileFactory
) {
)
{
this.folderFactory = folderFactory;
this.fileFactory = fileFactory;
}

public void LoadConfig(
ConfigInclusionRules inclusionRules,
JObject jsonConfig,
string fileName
) {
)
{
// Load airport data
JToken airportData = jsonConfig.SelectToken("includes.airports");
if (airportData != null)
@@ -61,7 +63,7 @@ string fileName
);
}
}

private string GetMissingTypeMessage(string section)
{
return $"Invalid type field for section {section} - must be \"files\" or \"folders\"";
@@ -101,19 +103,54 @@ string configFilePath

// Get the airport folders
string configFileFolder = GetFolderForConfigFile(configFilePath);
string[] directories = Directory.GetDirectories(configFileFolder + Path.DirectorySeparatorChar + configItem.Key);
string[] directories =
Directory.GetDirectories(configFileFolder + Path.DirectorySeparatorChar + configItem.Key);

// For each airport, iterate the config file sections
foreach (string directory in directories)
/*
* Airports are a bit special as there are lots of them.
* Rather than cycling each airport and applying all the rules, cycle the rules and then apply the
* airports, that way we maintain any desired ordering.
*/
foreach (ConfigFileSection configSection in AirfieldConfigFileSections.ConfigFileSections)
{
IterateConfigFileSections(
configItem.Value,
AirfieldConfigFileSections.ConfigFileSections,
x => OutputGroupFactory.CreateAirport(x, Path.GetFileName(directory)),
config.AddAirportInclusionRule,
directory,
"airport"
);
JToken configObjectSection = configItem.Value.SelectToken(configSection.JsonPath);
if (configObjectSection == null)
{
continue;
}


if (configObjectSection.Type == JTokenType.Array)
{
foreach (JToken token in (JArray)configObjectSection)
{
foreach (var directory in directories)
{
ProcessConfigSectionObject(
token,
configSection,
OutputGroupFactory.CreateAirport(configSection, Path.GetFileName(directory)),
config.AddAirportInclusionRule,
directory,
"airport"
);
}
}
}
else
{
foreach (var directory in directories)
{
ProcessConfigSectionObject(
configObjectSection,
configSection,
OutputGroupFactory.CreateAirport(configSection, Path.GetFileName(directory)),
config.AddAirportInclusionRule,
directory,
"airport"
);
}
}
}
}
}
@@ -125,15 +162,16 @@ private void IterateConfigFileSections(
Action<IInclusionRule> addInclusionRule,
string configFilePath,
string sectionRootString
) {
)
{
foreach (ConfigFileSection configSection in configFileSections)
{
JToken configObjectSection = jsonConfig.SelectToken(configSection.JsonPath);
if (configObjectSection == null)
{
continue;
}

LoadConfigSection(
configObjectSection,
configSection,
@@ -155,10 +193,11 @@ private void LoadConfigSection(
Action<IInclusionRule> addInclusionRule,
string configFilePath,
string sectionRootString
) {
)
{
if (jsonConfig.Type == JTokenType.Array)
{
foreach (JToken token in (JArray) jsonConfig)
foreach (JToken token in (JArray)jsonConfig)
{
ProcessConfigSectionObject(
token,
@@ -169,7 +208,9 @@ string sectionRootString
sectionRootString
);
}
} else {
}
else
{
ProcessConfigSectionObject(
jsonConfig,
configFileSection,
@@ -188,7 +229,8 @@ private void ProcessConfigSectionObject(
Action<IInclusionRule> addInclusionRule,
string rootPath,
string sectionRootString
) {
)
{
if (jsonConfig.Type != JTokenType.Object)
{
throw new ConfigFileInvalidException(GetInvalidConfigParentSectionFormatMessage(sectionRootString));
@@ -200,8 +242,9 @@ string sectionRootString
if (
!configObject.TryGetValue("type", out var typeToken) ||
typeToken.Type != JTokenType.String ||
((string) typeToken != "files" && (string) typeToken != "folder")
) {
((string)typeToken != "files" && (string)typeToken != "folder")
)
{
throw new ConfigFileInvalidException(
GetMissingTypeMessage($"{sectionRootString}.{configFileSection.JsonPath}")
);
@@ -216,7 +259,8 @@ string sectionRootString
rootPath
).CreateRule(configObject)
);
} else
}
else
{
addInclusionRule(
folderFactory.Create(
2 changes: 1 addition & 1 deletion src/Compiler/Parser/DataParserFactory.cs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ public ISectorDataParser GetParserForFile(AbstractSectorDataFile file)
new FrequencyParser(108, 117, 50),
sectorElements,
logger
),
),
InputDataType.SCT_NDBS => new NdbParser(new FrequencyParser(108, 950, 500), sectorElements, logger),
InputDataType.SCT_ARTCC => new ArtccParser(ArtccType.REGULAR, sectorElements, logger),
InputDataType.SCT_ARTCC_LOW => new ArtccParser(ArtccType.LOW, sectorElements, logger),
209 changes: 160 additions & 49 deletions tests/CompilerTest/Config/ConfigIncludeLoaderTest.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"includes": {
"airports": {
"Airports": {
"basic": [
{
"type": "files",
"files": [
"Basic.txt",
"Basic2.txt"
],
"exclude_directory": [
"EGKK"
]
},
{
"type": "files",
"files": [
"Basic.txt",
"Basic2.txt"
]
}
]
}
},
"enroute": {},
"misc": {}
}
}

0 comments on commit badda79

Please sign in to comment.