diff --git a/.editorconfig b/.editorconfig index 61026a4..d52c4a5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/src/EventStore.Plugins/ConfigParser.cs b/src/EventStore.Plugins/ConfigParser.cs index 3b71eb7..e334c2c 100644 --- a/src/EventStore.Plugins/ConfigParser.cs +++ b/src/EventStore.Plugins/ConfigParser.cs @@ -7,43 +7,44 @@ namespace EventStore.Plugins; public static class ConfigParser { - /// - /// Deserializes a section of configuration from a given config file into the provided settings type - /// - /// The path to the configuration file - /// The section to deserialize - /// The type of settings object to create from the configuration - public static T? ReadConfiguration(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, 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(reader); - } + /// + /// Deserializes a section of configuration from a given config file into the provided settings type + /// + /// The path to the configuration file + /// The section to deserialize + /// The type of settings object to create from the configuration + public static T? ReadConfiguration(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, 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(reader); + } }