From a59fced95b9bb4aeafbce3e13fe36c8fc4c4a7a9 Mon Sep 17 00:00:00 2001 From: Graham Pentheny Date: Sun, 18 Aug 2024 22:36:57 -0400 Subject: [PATCH] Code formatting and cleanup # Conflicts: # src/DarkConfig/DocNode/YamlDocNode.cs --- src/DarkConfig/DocNode/ComposedDocNode.cs | 26 +++++++++++------------ src/DarkConfig/DocNode/DocNode.cs | 6 +++--- src/DarkConfig/DocNode/YamlDocNode.cs | 16 +++++++------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/DarkConfig/DocNode/ComposedDocNode.cs b/src/DarkConfig/DocNode/ComposedDocNode.cs index 01961f8..b5b8a4e 100644 --- a/src/DarkConfig/DocNode/ComposedDocNode.cs +++ b/src/DarkConfig/DocNode/ComposedDocNode.cs @@ -13,17 +13,17 @@ public ComposedDocNode(DocNodeType type, int size = -1, string sourceInformation SourceNode = sourceDocNode?.SourceNode; switch (type) { case DocNodeType.Dictionary: - dictionary = size > 0 ? new Dictionary(size) : new Dictionary(); + dictionary = size > 0 ? new(size) : new(); break; case DocNodeType.List: - list = size > 0 ? new List(size) : new List(); + list = size > 0 ? new(size) : new(); break; case DocNodeType.Scalar: scalar = ""; break; case DocNodeType.Invalid: default: - throw new Exception($"Can't make a ComposedDocNode instance with Type {type} at {sourceInformation}"); + throw new($"Can't make a ComposedDocNode instance with Type {type} at {sourceInformation}"); } } @@ -74,10 +74,12 @@ public override bool ContainsKey(string key, bool ignoreCase = false) { public override bool TryGetValue(string key, bool ignoreCase, out DocNode result) { AssertTypeIs(DocNodeType.Dictionary); foreach (var kvp in dictionary) { - if (string.Equals(kvp.Key, key, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) { - result = kvp.Value; - return true; + if (!string.Equals(kvp.Key, key, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) { + continue; } + + result = kvp.Value; + return true; } result = null; @@ -109,13 +111,11 @@ public override string StringValue { } } - public override string SourceInformation => sourceInfo ?? "ComposedDocNode " + Type; + public override string SourceInformation => sourceInfo ?? $"ComposedDocNode {Type}"; public override string SourceFile { get; } public override YamlNode SourceNode { get; } - public override string ToString() { - return $"ComposedDocNode({Type}, {(Type == DocNodeType.Scalar ? scalar : Count.ToString())})"; - } + public override string ToString() => $"ComposedDocNode({Type}, {(Type == DocNodeType.Scalar ? scalar : Count.ToString())})"; #endregion public void Add(DocNode d) { @@ -165,13 +165,13 @@ public static ComposedDocNode MakeMutable(DocNode doc, bool recursive = true, bo switch (doc.Type) { case DocNodeType.Scalar: { - return new ComposedDocNode(doc.Type, sourceDocNode: doc) { + return new(doc.Type, sourceDocNode: doc) { StringValue = doc.StringValue }; } case DocNodeType.List: { - var newDoc = new ComposedDocNode(doc.Type, doc.Count, sourceDocNode: doc); + ComposedDocNode newDoc = new(doc.Type, doc.Count, sourceDocNode: doc); foreach (var value in doc.Values) { newDoc.Add(recursive ? MakeMutable(value, recursive: true, force: force) : value); } @@ -179,7 +179,7 @@ public static ComposedDocNode MakeMutable(DocNode doc, bool recursive = true, bo } case DocNodeType.Dictionary: { - var newDoc = new ComposedDocNode(doc.Type, doc.Count, sourceDocNode: doc); + ComposedDocNode newDoc = new(doc.Type, doc.Count, sourceDocNode: doc); foreach ((string key, var value) in doc.Pairs) { newDoc.Add(key, recursive ? MakeMutable(value, recursive: true, force: force) : value); } diff --git a/src/DarkConfig/DocNode/DocNode.cs b/src/DarkConfig/DocNode/DocNode.cs index e6395a0..87628d7 100644 --- a/src/DarkConfig/DocNode/DocNode.cs +++ b/src/DarkConfig/DocNode/DocNode.cs @@ -38,7 +38,6 @@ public abstract class DocNode : IEquatable { /// /// Only valid for dictionaries. - /// s /// Try to get the value for the given key. /// /// Key of the value to retrieve @@ -55,6 +54,7 @@ public abstract class DocNode : IEquatable { /// String describing the position and context in the source format (e.g. line number). public abstract string SourceInformation { get; } + /// The file that this doc node originated from public abstract string? SourceFile { get; } public abstract YamlDotNet.RepresentationModel.YamlNode? SourceNode { get; } @@ -157,6 +157,7 @@ public int GetDeepHashCode() { } return mapHash; + case DocNodeType.Invalid: default: throw new ParseException(this, $"Cannot calculate hash code for DocNode type {Type}"); } @@ -182,8 +183,7 @@ public static DocNode DeepMerge(DocNode lhs, DocNode rhs) { case DocNodeType.List: return Configs.CombineList(new List {lhs, rhs}); case DocNodeType.Dictionary: { - var mergedDict = new ComposedDocNode(DocNodeType.Dictionary, - sourceInformation: "Merging of: [" + lhs.SourceInformation + ", " + rhs.SourceInformation + "]"); + ComposedDocNode mergedDict = new(DocNodeType.Dictionary, sourceInformation: $"Merging of: [{lhs.SourceInformation}, {rhs.SourceInformation}]"); foreach (var lhsPair in lhs.Pairs) { mergedDict[lhsPair.Key] = lhsPair.Value; } diff --git a/src/DarkConfig/DocNode/YamlDocNode.cs b/src/DarkConfig/DocNode/YamlDocNode.cs index 5d2a186..d0bf863 100644 --- a/src/DarkConfig/DocNode/YamlDocNode.cs +++ b/src/DarkConfig/DocNode/YamlDocNode.cs @@ -46,8 +46,8 @@ public override DocNode this[int index] { /// /// Access the node as if it was a Dictionary /// - /// - /// + /// Dictionary key + /// Thrown when attempting to set a value public override DocNode this[string key] { get { AssertTypeIs(DocNodeType.Dictionary); @@ -88,10 +88,12 @@ public override bool TryGetValue(string key, bool ignoreCase, out DocNode result var children = ((YamlMappingNode) SourceNode).Children; var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; foreach (var kvp in children) { - if (kvp.Key is YamlScalarNode scalarKey && string.Equals(scalarKey.Value, key, comparison)) { - result = new YamlDocNode(kvp.Value, SourceFile); - return true; + if (kvp.Key is not YamlScalarNode scalarKey || !string.Equals(scalarKey.Value, key, comparison)) { + continue; } + + result = new YamlDocNode(kvp.Value, SourceFile); + return true; } result = null; @@ -133,9 +135,7 @@ internal PairsIterator(YamlNode node, string filename) { public IEnumerator> GetEnumerator() { foreach (var entry in ((YamlMappingNode) node).Children) { - yield return new KeyValuePair( - ((YamlScalarNode) entry.Key).Value, - new YamlDocNode(entry.Value, filename)); + yield return new(((YamlScalarNode) entry.Key).Value, new YamlDocNode(entry.Value, filename)); } }