Skip to content

Commit 5d3cab0

Browse files
committed
Combined similar type doc node type assertion methods in YamlDocNode and ComposedDocNode
1 parent 5d8a55f commit 5d3cab0

File tree

3 files changed

+28
-44
lines changed

3 files changed

+28
-44
lines changed

src/DarkConfig/DocNode/ComposedDocNode.cs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ public ComposedDocNode(DocNodeType type, int size = -1, string sourceInformation
3333
/// access the node as if it was a list
3434
public override DocNode this[int index] {
3535
get {
36-
CheckTypeIs(DocNodeType.List);
36+
AssertTypeIs(DocNodeType.List);
3737
return list[index];
3838
}
3939
set {
40-
CheckTypeIs(DocNodeType.List);
40+
AssertTypeIs(DocNodeType.List);
4141
list[index] = value;
4242
}
4343
}
4444

4545
/// access the node as if it was a Dictionary
4646
public override DocNode this[string key] {
4747
get {
48-
CheckTypeIs(DocNodeType.Dictionary);
48+
AssertTypeIs(DocNodeType.Dictionary);
4949
return dictionary[key];
5050
}
5151
set {
52-
CheckTypeIs(DocNodeType.Dictionary);
52+
AssertTypeIs(DocNodeType.Dictionary);
5353
dictionary[key] = value;
5454
}
5555
}
@@ -58,11 +58,11 @@ public override DocNode this[string key] {
5858
Type switch {
5959
DocNodeType.Dictionary => dictionary.Count,
6060
DocNodeType.List => list.Count,
61-
_ => throw new DocNodeAccessException(GenerateAccessExceptionMessage("Countable (Dictionary or List)"))
61+
_ => throw new DocNodeAccessException(GenerateAccessExceptionMessage("Countable (Dictionary or List)", Type.ToString()))
6262
};
6363

6464
public override bool ContainsKey(string key, bool ignoreCase = false) {
65-
CheckTypeIs(DocNodeType.Dictionary);
65+
AssertTypeIs(DocNodeType.Dictionary);
6666
foreach (string dictKey in dictionary.Keys) {
6767
if (string.Equals(dictKey, key, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) {
6868
return true;
@@ -72,7 +72,7 @@ public override bool ContainsKey(string key, bool ignoreCase = false) {
7272
}
7373

7474
public override bool TryGetValue(string key, bool ignoreCase, out DocNode result) {
75-
CheckTypeIs(DocNodeType.Dictionary);
75+
AssertTypeIs(DocNodeType.Dictionary);
7676
foreach (var kvp in dictionary) {
7777
if (string.Equals(kvp.Key, key, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) {
7878
result = kvp.Value;
@@ -86,25 +86,25 @@ public override bool TryGetValue(string key, bool ignoreCase, out DocNode result
8686

8787
public override IEnumerable<DocNode> Values {
8888
get {
89-
CheckTypeIs(DocNodeType.List);
89+
AssertTypeIs(DocNodeType.List);
9090
return list;
9191
}
9292
}
9393

9494
public override IEnumerable<KeyValuePair<string, DocNode>> Pairs {
9595
get {
96-
CheckTypeIs(DocNodeType.Dictionary);
96+
AssertTypeIs(DocNodeType.Dictionary);
9797
return dictionary;
9898
}
9999
}
100100

101101
public override string StringValue {
102102
get {
103-
CheckTypeIs(DocNodeType.Scalar);
103+
AssertTypeIs(DocNodeType.Scalar);
104104
return scalar;
105105
}
106106
set {
107-
CheckTypeIs(DocNodeType.Scalar);
107+
AssertTypeIs(DocNodeType.Scalar);
108108
scalar = value;
109109
}
110110
}
@@ -119,32 +119,32 @@ public override string ToString() {
119119
#endregion
120120

121121
public void Add(DocNode d) {
122-
CheckTypeIs(DocNodeType.List);
122+
AssertTypeIs(DocNodeType.List);
123123
list.Add(d);
124124
}
125125

126126
public void Add(string key, DocNode value) {
127-
CheckTypeIs(DocNodeType.Dictionary);
127+
AssertTypeIs(DocNodeType.Dictionary);
128128
dictionary.Add(key, value);
129129
}
130130

131131
public void InsertAt(int index, DocNode value) {
132-
CheckTypeIs(DocNodeType.List);
132+
AssertTypeIs(DocNodeType.List);
133133
list.Insert(index, value);
134134
}
135135

136136
public void Remove(DocNode d) {
137-
CheckTypeIs(DocNodeType.List);
137+
AssertTypeIs(DocNodeType.List);
138138
list.Remove(d);
139139
}
140140

141141
public void RemoveAt(int index) {
142-
CheckTypeIs(DocNodeType.List);
142+
AssertTypeIs(DocNodeType.List);
143143
list.RemoveAt(index);
144144
}
145145

146146
public void RemoveKey(string key) {
147-
CheckTypeIs(DocNodeType.Dictionary);
147+
AssertTypeIs(DocNodeType.Dictionary);
148148
dictionary.Remove(key);
149149
}
150150

@@ -205,17 +205,5 @@ public static ComposedDocNode MakeMutableRef(ref DocNode doc, bool recursive = t
205205
public static ComposedDocNode DeepClone(DocNode doc) {
206206
return MakeMutable(doc, recursive: true, force: true);
207207
}
208-
209-
/////////////////////////////////////////////////
210-
211-
void CheckTypeIs(DocNodeType requiredType) {
212-
if (Type != requiredType) {
213-
throw new DocNodeAccessException(GenerateAccessExceptionMessage(requiredType.ToString()));
214-
}
215-
}
216-
217-
string GenerateAccessExceptionMessage(string expectedType) {
218-
return $"Accessing ComposedDocNode as {expectedType} but is {Type}. ";
219-
}
220208
}
221209
}

src/DarkConfig/DocNode/DocNode.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public T As<T>(ReificationOptions? options = null) {
7171
/// <returns>True if the string exists in this list</returns>
7272
/// <exception cref="DocNodeAccessException">Thrown if this is not a list</exception>
7373
public bool Contains(string item) {
74-
if (Type != DocNodeType.List) {
75-
throw new DocNodeAccessException("Expected List, is " + Type);
76-
}
74+
AssertTypeIs(DocNodeType.List);
7775

7876
for (int i = 0; i < Count; i++) {
7977
if (this[i].StringValue == item) {
@@ -207,5 +205,15 @@ public static DocNode DeepMerge(DocNode lhs, DocNode rhs) {
207205
default: throw new ArgumentException($"Can't merge doc nodes of type {lhs.Type}");
208206
}
209207
}
208+
209+
////////////////////////////////////////////
210+
211+
protected void AssertTypeIs(DocNodeType type) {
212+
if (Type != type) {
213+
throw new DocNodeAccessException(GenerateAccessExceptionMessage(type.ToString(), Type.ToString()));
214+
}
215+
}
216+
217+
protected string GenerateAccessExceptionMessage(string expectedType, string actualType) => $"Accessing DocNode as {expectedType} but is {actualType}. {SourceInformation}";
210218
}
211219
}

src/DarkConfig/DocNode/YamlDocNode.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,5 @@ public override string StringValue {
164164

165165
public override YamlNode SourceNode { get; }
166166
public override string SourceFile { get; }
167-
168-
////////////////////////////////////////////
169-
170-
void AssertTypeIs(DocNodeType type) {
171-
if (Type != type) {
172-
throw new DocNodeAccessException(GenerateAccessExceptionMessage(type.ToString(), Type.ToString()));
173-
}
174-
}
175-
176-
string GenerateAccessExceptionMessage(string expectedType, string actualType) {
177-
return $"Accessing YamlDocNode as {expectedType} but is {actualType}. {SourceInformation}";
178-
}
179167
}
180168
}

0 commit comments

Comments
 (0)