Skip to content

Commit

Permalink
Code formatting and cleanup
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/DarkConfig/DocNode/YamlDocNode.cs
  • Loading branch information
grahamboree committed Aug 31, 2024
1 parent 5d3cab0 commit a59fced
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
26 changes: 13 additions & 13 deletions src/DarkConfig/DocNode/ComposedDocNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, DocNode>(size) : new Dictionary<string, DocNode>();
dictionary = size > 0 ? new(size) : new();
break;
case DocNodeType.List:
list = size > 0 ? new List<DocNode>(size) : new List<DocNode>();
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}");
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -165,21 +165,21 @@ 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);
}
return newDoc;
}

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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/DarkConfig/DocNode/DocNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public abstract class DocNode : IEquatable<DocNode> {

/// <summary>
/// Only valid for dictionaries.
/// s
/// Try to get the value for the given key.
/// </summary>
/// <param name="key">Key of the value to retrieve</param>
Expand All @@ -55,6 +54,7 @@ public abstract class DocNode : IEquatable<DocNode> {

/// 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; }

Check warning on line 58 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / build (6.0, Debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 58 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / build (6.0, Debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 58 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / test (6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 58 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / build (6.0, Release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 58 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / build (6.0, Release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public abstract YamlDotNet.RepresentationModel.YamlNode? SourceNode { get; }

Check warning on line 59 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / build (6.0, Debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 59 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / build (6.0, Debug)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 59 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / test (6.0)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 59 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / build (6.0, Release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 59 in src/DarkConfig/DocNode/DocNode.cs

View workflow job for this annotation

GitHub Actions / build (6.0, Release)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Expand Down Expand Up @@ -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}");
}
Expand All @@ -182,8 +183,7 @@ public static DocNode DeepMerge(DocNode lhs, DocNode rhs) {
case DocNodeType.List:
return Configs.CombineList(new List<DocNode> {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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/DarkConfig/DocNode/YamlDocNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public override DocNode this[int index] {
/// <summary>
/// Access the node as if it was a Dictionary
/// </summary>
/// <param name="key"></param>
/// <exception cref="NotSupportedException"></exception>
/// <param name="key">Dictionary key</param>
/// <exception cref="NotSupportedException">Thrown when attempting to set a value</exception>
public override DocNode this[string key] {
get {
AssertTypeIs(DocNodeType.Dictionary);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -133,9 +135,7 @@ internal PairsIterator(YamlNode node, string filename) {

public IEnumerator<KeyValuePair<string, DocNode>> GetEnumerator() {
foreach (var entry in ((YamlMappingNode) node).Children) {
yield return new KeyValuePair<string, DocNode>(
((YamlScalarNode) entry.Key).Value,
new YamlDocNode(entry.Value, filename));
yield return new(((YamlScalarNode) entry.Key).Value, new YamlDocNode(entry.Value, filename));
}
}

Expand Down

0 comments on commit a59fced

Please sign in to comment.