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
  • Loading branch information
AndyTWF authored Jan 6, 2022
1 parent c407af3 commit badda79
Show file tree
Hide file tree
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
Expand Up @@ -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)
Expand Down Expand Up @@ -61,7 +63,7 @@ string fileName
);
}
}

private string GetMissingTypeMessage(string section)
{
return $"Invalid type field for section {section} - must be \"files\" or \"folders\"";
Expand Down Expand Up @@ -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"
);
}
}
}
}
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -169,7 +208,9 @@ string sectionRootString
sectionRootString
);
}
} else {
}
else
{
ProcessConfigSectionObject(
jsonConfig,
configFileSection,
Expand All @@ -188,7 +229,8 @@ private void ProcessConfigSectionObject(
Action<IInclusionRule> addInclusionRule,
string rootPath,
string sectionRootString
) {
)
{
if (jsonConfig.Type != JTokenType.Object)
{
throw new ConfigFileInvalidException(GetInvalidConfigParentSectionFormatMessage(sectionRootString));
Expand All @@ -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}")
);
Expand All @@ -216,7 +259,8 @@ string sectionRootString
rootPath
).CreateRule(configObject)
);
} else
}
else
{
addInclusionRule(
folderFactory.Create(
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Parser/DataParserFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading

0 comments on commit badda79

Please sign in to comment.