Skip to content

Commit

Permalink
improving the way how SharedParameterFile objects are initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
salaros committed Oct 29, 2019
1 parent f119a77 commit 7c1385d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 77 deletions.
144 changes: 69 additions & 75 deletions src/Parameters/Shared/SharedParameterFile.CSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ static SharedParameterFile()
};

#if !NET45
// Allow the usage of ANSI encoding other than the default one
// Allow the usage of ANSI encoding other than the default one
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
}


/// <summary>
/// Initializes a new instance of the <see cref="SharedParameterFile" /> class.
/// </summary>
Expand All @@ -71,6 +70,9 @@ public SharedParameterFile(string sharedParameterFile, Encoding encoding = null)
sharedParameterFile = File.ReadAllText(sharedParameterFile, Encoding);
}

if (!SectionRegex.IsMatch(sharedParameterFile))
throw new InvalidDataException("Please make sure you supplied a correct file path or file content string.");

var sharedParamsFileLines = SectionRegex
.Split(sharedParameterFile)
.Where(line => !line.StartsWith("#")) // Exclude comment lines
Expand All @@ -92,83 +94,75 @@ public SharedParameterFile(string sharedParameterFile, Encoding encoding = null)

foreach (var section in sharedParamsFileSections)
{
using (var stringReader = new StringReader(section.Value))
using var stringReader = new StringReader(section.Value);
using var csvReader = new CsvReader(stringReader, CsvConfiguration);
csvReader.Configuration.TrimOptions = TrimOptions.Trim;
csvReader.Configuration.BadDataFound = BadDataFound;

// TODO implement
// csvReader.Configuration.AllowComments = true;
// csvReader.Configuration.Comment = '#';

var originalHeaderValidated = csvReader.Configuration.HeaderValidated;
csvReader.Configuration.HeaderValidated = (isValid, headerNames, headerIndex, context) =>
{
using (var csvReader = new CsvReader(stringReader, CsvConfiguration))
{
csvReader.Configuration.TrimOptions = TrimOptions.Trim;
csvReader.Configuration.BadDataFound = BadDataFound;

// TODO implement
// csvReader.Configuration.AllowComments = true;
// csvReader.Configuration.Comment = '#';

var originalHeaderValidated = csvReader.Configuration.HeaderValidated;
csvReader.Configuration.HeaderValidated = (isValid, headerNames, headerIndex, context) =>
{
// Everything is OK, just go out
if (isValid)
return;

// Allow DESCRIPTION header to be missing (it's actually missing in older shared parameter files)
if (nameof(ParameterDefinition.Description).Equals(headerNames?.FirstOrDefault(),
StringComparison.OrdinalIgnoreCase))
return;

// Allow USERMODIFIABLE header to be missing (it's actually missing in older shared parameter files)
if (nameof(ParameterDefinition.UserModifiable).Equals(headerNames?.FirstOrDefault(),
StringComparison.OrdinalIgnoreCase))
return;

originalHeaderValidated(false, headerNames, headerIndex, context);
};

var originalMissingFieldFound = csvReader.Configuration.MissingFieldFound;
csvReader.Configuration.MissingFieldFound = (headerNames, index, context) =>
{
// Allow DESCRIPTION header to be missing (it's actually missing in older shared parameter files)
if (nameof(ParameterDefinition.Description).Equals(headerNames?.FirstOrDefault(),
StringComparison.OrdinalIgnoreCase))
return;

// Allow USERMODIFIABLE header to be missing (it's actually missing in older shared parameter files)
if (nameof(ParameterDefinition.UserModifiable).Equals(headerNames?.FirstOrDefault(),
StringComparison.OrdinalIgnoreCase))
return;

originalMissingFieldFound(headerNames, index, context);
};

switch (section.Key)
{
// Parse *META section
case Sections.META:
csvReader.Configuration.RegisterClassMap<MetaClassMap>();
Metadata = csvReader.GetRecords<MetaData>().FirstOrDefault();
break;

// Parse *GROUP section
case Sections.GROUPS:
csvReader.Configuration.RegisterClassMap<GroupClassMap>();
_groups = csvReader.GetRecords<Group>().ToList();
break;

// Parse *PARAM section
case Sections.PARAMS:
csvReader.Configuration.RegisterClassMap<ParameterClassMap>();
Parameters = new ParameterCollection(this, csvReader.GetRecords<ParameterDefinition>().ToList());
break;

default:
Debug.WriteLine($"Unknown section type: {section.Key}");
continue;
}
}
// Everything is OK, just go out
if (isValid)
return;

// Allow DESCRIPTION header to be missing (it's actually missing in older shared parameter files)
if (nameof(ParameterDefinition.Description).Equals(headerNames?.FirstOrDefault(), StringComparison.OrdinalIgnoreCase))
return;

// Allow USERMODIFIABLE header to be missing (it's actually missing in older shared parameter files)
if (nameof(ParameterDefinition.UserModifiable).Equals(headerNames?.FirstOrDefault(), StringComparison.OrdinalIgnoreCase))
return;

originalHeaderValidated(false, headerNames, headerIndex, context);
};

var originalMissingFieldFound = csvReader.Configuration.MissingFieldFound;
csvReader.Configuration.MissingFieldFound = (headerNames, index, context) =>
{
// Allow DESCRIPTION header to be missing (it's actually missing in older shared parameter files)
if (nameof(ParameterDefinition.Description).Equals(headerNames?.FirstOrDefault(), StringComparison.OrdinalIgnoreCase))
return;

// Allow USERMODIFIABLE header to be missing (it's actually missing in older shared parameter files)
if (nameof(ParameterDefinition.UserModifiable).Equals(headerNames?.FirstOrDefault(), StringComparison.OrdinalIgnoreCase))
return;

originalMissingFieldFound(headerNames, index, context);
};

switch (section.Key)
{
// Parse *META section
case Sections.META:
csvReader.Configuration.RegisterClassMap<MetaClassMap>();
Metadata = csvReader.GetRecords<MetaData>().FirstOrDefault();
break;

// Parse *GROUP section
case Sections.GROUPS:
csvReader.Configuration.RegisterClassMap<GroupClassMap>();
_groups = csvReader.GetRecords<Group>().ToList();
break;

// Parse *PARAM section
case Sections.PARAMS:
csvReader.Configuration.RegisterClassMap<ParameterClassMap>();
Parameters = new ParameterCollection(this, csvReader.GetRecords<ParameterDefinition>().ToList());
break;

default:
Debug.WriteLine($"Unknown section type: {section.Key}");
continue;
}
}

// Post-process parameters by assigning group names using group IDs
// and recover UnitType from ParameterType
// Post-process parameters by assigning group names using group IDs
// and recover UnitType from ParameterType
Parameters = new ParameterCollection(
this, // Parent shared parameter file
Parameters
Expand Down
4 changes: 2 additions & 2 deletions tests/SharedParameterFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ public void FileIsSerializedProperly()
var simpleSharedFromDisk = File
.ReadAllText(SharedParameterFiles.FirstOrDefault(f => f.EndsWith(@"SimpleShared_1.txt", StringComparison.InvariantCulture)));

var simpleSharedFromBuilt = new SharedParameterFile(new Dictionary<string,int> { { "Identity Data", 100 } } );
var simpleSharedFromBuilt = new SharedParameterFile(new Dictionary<string, int> { { "Identity Data", 100 } });

#region Identity Data

simpleSharedFromBuilt.Parameters.Add(
new Guid("61ff3d56-09d7-4049-8c78-4abe745e4e5a"),"EquipmentName",
new Guid("61ff3d56-09d7-4049-8c78-4abe745e4e5a"), "EquipmentName",
"Identity Data", // Passing group by name
ParameterType.Text
);
Expand Down

0 comments on commit 7c1385d

Please sign in to comment.