Skip to content

Commit

Permalink
Merge pull request #61 from EventStore/timothycoleman/file-scoped-nam…
Browse files Browse the repository at this point in the history
…espaces

Enforce file scoped namespaces
  • Loading branch information
timothycoleman authored Oct 8, 2024
2 parents 51c1f3e + d28159e commit 49a0b01
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ csharp_new_line_within_query_expression_clauses = true
# disable hint to use primary constructor
dotnet_diagnostic.IDE0290.severity = none

# use file-scoped namespaces
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error

# use ValueTasks correctly
dotnet_diagnostic.CA2012.severity = error

Expand Down
79 changes: 40 additions & 39 deletions src/EventStore.Plugins/ConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,44 @@
namespace EventStore.Plugins;

public static class ConfigParser {
/// <summary>
/// Deserializes a section of configuration from a given config file into the provided settings type
/// </summary>
/// <param name="configPath">The path to the configuration file</param>
/// <param name="sectionName">The section to deserialize</param>
/// <typeparam name="T">The type of settings object to create from the configuration</typeparam>
public static T? ReadConfiguration<T>(string configPath, string sectionName) where T : class {
var yamlStream = new YamlStream();
var stringReader = new StringReader(File.ReadAllText(configPath));

try {
yamlStream.Load(stringReader);
}
catch (Exception ex) {
throw new(
$"An invalid configuration file has been specified. {Environment.NewLine}{ex.Message}");
}

var yamlNode = (YamlMappingNode)yamlStream.Documents[0].RootNode;
if (!string.IsNullOrEmpty(sectionName)) {
Func<KeyValuePair<YamlNode, YamlNode>, bool> predicate = x =>
x.Key.ToString() == sectionName && x.Value is YamlMappingNode;

var nodeExists = yamlNode.Children.Any(predicate);
if (nodeExists) yamlNode = (YamlMappingNode)yamlNode.Children.First(predicate).Value;
}

if (yamlNode is null) return default;

using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
using var reader = new StreamReader(stream);

new YamlStream(new YamlDocument(yamlNode)).Save(writer);
writer.Flush();
stream.Position = 0;

return new Deserializer().Deserialize<T>(reader);
}
/// <summary>
/// Deserializes a section of configuration from a given config file into the provided settings type
/// </summary>
/// <param name="configPath">The path to the configuration file</param>
/// <param name="sectionName">The section to deserialize</param>
/// <typeparam name="T">The type of settings object to create from the configuration</typeparam>
public static T? ReadConfiguration<T>(string configPath, string sectionName) where T : class {
var yamlStream = new YamlStream();
var stringReader = new StringReader(File.ReadAllText(configPath));

try {
yamlStream.Load(stringReader);
} catch (Exception ex) {
throw new(
$"An invalid configuration file has been specified. {Environment.NewLine}{ex.Message}");
}

var yamlNode = (YamlMappingNode)yamlStream.Documents[0].RootNode;
if (!string.IsNullOrEmpty(sectionName)) {
Func<KeyValuePair<YamlNode, YamlNode>, bool> predicate = x =>
x.Key.ToString() == sectionName && x.Value is YamlMappingNode;

var nodeExists = yamlNode.Children.Any(predicate);
if (nodeExists)
yamlNode = (YamlMappingNode)yamlNode.Children.First(predicate).Value;
}

if (yamlNode is null)

Check warning on line 37 in src/EventStore.Plugins/ConfigParser.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Expression is always 'true' or 'false' according to nullable reference types' annotations

Expression is always false according to nullable reference types' annotations
return default;

using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
using var reader = new StreamReader(stream);

new YamlStream(new YamlDocument(yamlNode)).Save(writer);
writer.Flush();
stream.Position = 0;

return new Deserializer().Deserialize<T>(reader);
}
}

0 comments on commit 49a0b01

Please sign in to comment.