Skip to content

Commit b45cb0a

Browse files
ArkatufusAaronontheweb
authored andcommitted
Remove HoconValue.Undefined (#101)
1 parent 4eb901c commit b45cb0a

File tree

5 files changed

+63
-102
lines changed

5 files changed

+63
-102
lines changed

src/Hocon.Configuration/Config.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected override HoconValue GetNode(HoconPath path)
7474
}
7575
catch
7676
{
77-
result = Fallback?.GetNode(path) ?? HoconValue.Undefined;
77+
result = Fallback?.GetNode(path);
7878
}
7979

8080
return result;
@@ -95,10 +95,10 @@ public virtual Config GetConfig(HoconPath path)
9595
if (Fallback != null)
9696
{
9797
var f = Fallback.GetConfig(path);
98-
return ReferenceEquals(value, HoconValue.Undefined) ? f : new Config(new HoconRoot(value)).WithFallback(f);
98+
return value == null ? f : new Config(new HoconRoot(value)).WithFallback(f);
9999
}
100100

101-
return ReferenceEquals(value, HoconValue.Undefined) ? null : new Config(new HoconRoot(value));
101+
return value == null ? null : new Config(new HoconRoot(value));
102102
}
103103

104104
/// <summary>
@@ -112,9 +112,9 @@ public virtual Config WithFallback(Config fallback)
112112
if (fallback == this)
113113
throw new ArgumentException("Config can not have itself as fallback", nameof(fallback));
114114

115-
Config clone = Copy();
115+
var clone = Copy();
116116

117-
Config current = clone;
117+
var current = clone;
118118
while (current.Fallback != null)
119119
{
120120
current = current.Fallback;

src/Hocon/HoconRoot.cs

+48-78
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public string GetString(string path, string @default = null)
158158
public string GetString(HoconPath path, string @default = null)
159159
{
160160
var value = GetNode(path);
161-
return ReferenceEquals(value, HoconValue.Undefined) ? @default : value.GetString();
161+
return value == null ? @default : value.GetString();
162162
}
163163

164164
/// <summary>
@@ -173,11 +173,8 @@ public bool GetBoolean(string path, bool @default = false)
173173
/// <inheritdoc cref="GetBoolean(string,bool)"/>
174174
public bool GetBoolean(HoconPath path, bool @default = false)
175175
{
176-
HoconValue value = GetNode(path);
177-
if (ReferenceEquals(value, HoconValue.Undefined))
178-
return @default;
179-
180-
return value.GetBoolean();
176+
var value = GetNode(path);
177+
return value == null ? @default : value.GetBoolean();
181178
}
182179

183180
/// <summary>
@@ -191,10 +188,8 @@ public bool GetBoolean(HoconPath path, bool @default = false)
191188
/// <inheritdoc cref="GetByteSize(string)"/>
192189
public long? GetByteSize(HoconPath path)
193190
{
194-
HoconValue value = GetNode(path);
195-
if (ReferenceEquals(value, HoconValue.Undefined))
196-
return null;
197-
return value.GetByteSize();
191+
var value = GetNode(path);
192+
return value?.GetByteSize();
198193
}
199194

200195
/// <summary>
@@ -209,11 +204,8 @@ public int GetInt(string path, int @default = 0)
209204
/// <inheritdoc cref="GetInt(string,int)"/>
210205
public int GetInt(HoconPath path, int @default = 0)
211206
{
212-
HoconValue value = GetNode(path);
213-
if (ReferenceEquals(value, HoconValue.Undefined))
214-
return @default;
215-
216-
return value.GetInt();
207+
var value = GetNode(path);
208+
return value == null ? @default : value.GetInt();
217209
}
218210

219211
/// <summary>
@@ -228,23 +220,17 @@ public long GetLong(string path, long @default = 0)
228220
/// <inheritdoc cref="GetLong(string,long)"/>
229221
public long GetLong(HoconPath path, long @default = 0)
230222
{
231-
HoconValue value = GetNode(path);
232-
if (ReferenceEquals(value, HoconValue.Undefined))
233-
return @default;
234-
235-
return value.GetLong();
223+
var value = GetNode(path);
224+
return value == null ? @default : value.GetLong();
236225
}
237226

238227
public byte GetByte(string path, byte @default = 0)
239228
=> GetByte(HoconPath.Parse(path), @default);
240229

241230
public byte GetByte(HoconPath path, byte @default = 0)
242231
{
243-
HoconValue value = GetNode(path);
244-
if (ReferenceEquals(value, HoconValue.Undefined))
245-
return @default;
246-
247-
return value.GetByte();
232+
var value = GetNode(path);
233+
return value == null ? @default : value.GetByte();
248234
}
249235

250236
/// <summary>
@@ -259,11 +245,8 @@ public float GetFloat(string path, float @default = 0)
259245
/// <inheritdoc cref="GetFloat(string,float)"/>
260246
public float GetFloat(HoconPath path, float @default = 0)
261247
{
262-
HoconValue value = GetNode(path);
263-
if (ReferenceEquals(value, HoconValue.Undefined))
264-
return @default;
265-
266-
return value.GetFloat();
248+
var value = GetNode(path);
249+
return value == null ? @default : value.GetFloat();
267250
}
268251

269252
/// <summary>
@@ -278,11 +261,8 @@ public decimal GetDecimal(string path, decimal @default = 0)
278261
/// <inheritdoc cref="GetDecimal(string,decimal)"/>
279262
public decimal GetDecimal(HoconPath path, decimal @default = 0)
280263
{
281-
HoconValue value = GetNode(path);
282-
if (ReferenceEquals(value, HoconValue.Undefined))
283-
return @default;
284-
285-
return value.GetDecimal();
264+
var value = GetNode(path);
265+
return value == null ? @default : value.GetDecimal();
286266
}
287267

288268
/// <summary>
@@ -297,11 +277,8 @@ public double GetDouble(string path, double @default = 0)
297277
/// <inheritdoc cref="GetDouble(string,double)"/>
298278
public double GetDouble(HoconPath path, double @default = 0)
299279
{
300-
HoconValue value = GetNode(path);
301-
if (ReferenceEquals(value, HoconValue.Undefined))
302-
return @default;
303-
304-
return value.GetDouble();
280+
var value = GetNode(path);
281+
return value == null ? @default : value.GetDouble();
305282
}
306283

307284
/// <summary>
@@ -316,11 +293,8 @@ public HoconObject GetObject(string path, HoconObject @default = null)
316293
/// <inheritdoc cref="GetObject(string,HoconObject)"/>
317294
public HoconObject GetObject(HoconPath path, HoconObject @default = null)
318295
{
319-
HoconValue value = GetNode(path);
320-
if (ReferenceEquals(value, HoconValue.Undefined))
321-
return @default;
322-
323-
return value.GetObject();
296+
var value = GetNode(path);
297+
return value == null ? @default : value.GetObject();
324298
}
325299

326300

@@ -335,9 +309,9 @@ public IList<Boolean> GetBooleanList(string path)
335309
/// <inheritdoc cref="GetBooleanList(string)"/>
336310
public IList<Boolean> GetBooleanList(HoconPath path)
337311
{
338-
HoconValue value = GetNode(path);
339-
if (ReferenceEquals(value, HoconValue.Undefined))
340-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
312+
var value = GetNode(path);
313+
if (value == null)
314+
throw new HoconParserException($"Hocon path {path} does not exist.");
341315

342316
return value.GetBooleanList();
343317
}
@@ -353,9 +327,9 @@ public IList<decimal> GetDecimalList(string path)
353327
/// <inheritdoc cref="GetDecimalList(string)"/>
354328
public IList<decimal> GetDecimalList(HoconPath path)
355329
{
356-
HoconValue value = GetNode(path);
357-
if (ReferenceEquals(value, HoconValue.Undefined))
358-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
330+
var value = GetNode(path);
331+
if (value == null)
332+
throw new HoconParserException($"Hocon path {path} does not exist.");
359333

360334
return value.GetDecimalList();
361335
}
@@ -371,9 +345,9 @@ public IList<float> GetFloatList(string path)
371345
/// <inheritdoc cref="GetFloatList(string)"/>
372346
public IList<float> GetFloatList(HoconPath path)
373347
{
374-
HoconValue value = GetNode(path);
375-
if (ReferenceEquals(value, HoconValue.Undefined))
376-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
348+
var value = GetNode(path);
349+
if (value == null)
350+
throw new HoconParserException($"Hocon path {path} does not exist.");
377351

378352
return value.GetFloatList();
379353
}
@@ -389,9 +363,9 @@ public IList<double> GetDoubleList(string path)
389363
/// <inheritdoc cref="GetDoubleList(string)"/>
390364
public IList<double> GetDoubleList(HoconPath path)
391365
{
392-
HoconValue value = GetNode(path);
393-
if (ReferenceEquals(value, HoconValue.Undefined))
394-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
366+
var value = GetNode(path);
367+
if (value == null)
368+
throw new HoconParserException($"Hocon path {path} does not exist.");
395369

396370
return value.GetDoubleList();
397371
}
@@ -407,9 +381,9 @@ public IList<int> GetIntList(string path)
407381
/// <inheritdoc cref="GetIntList(string)"/>
408382
public IList<int> GetIntList(HoconPath path)
409383
{
410-
HoconValue value = GetNode(path);
411-
if (ReferenceEquals(value, HoconValue.Undefined))
412-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
384+
var value = GetNode(path);
385+
if (value == null)
386+
throw new HoconParserException($"Hocon path {path} does not exist.");
413387

414388
return value.GetIntList();
415389
}
@@ -425,9 +399,9 @@ public IList<long> GetLongList(string path)
425399
/// <inheritdoc cref="GetLongList(string)"/>
426400
public IList<long> GetLongList(HoconPath path)
427401
{
428-
HoconValue value = GetNode(path);
429-
if (ReferenceEquals(value, HoconValue.Undefined))
430-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
402+
var value = GetNode(path);
403+
if (value == null)
404+
throw new HoconParserException($"Hocon path {path} does not exist.");
431405

432406
return value.GetLongList();
433407
}
@@ -443,9 +417,9 @@ public IList<byte> GetByteList(string path)
443417
/// <inheritdoc cref="GetByteList(string)"/>
444418
public IList<byte> GetByteList(HoconPath path)
445419
{
446-
HoconValue value = GetNode(path);
447-
if (ReferenceEquals(value, HoconValue.Undefined))
448-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
420+
var value = GetNode(path);
421+
if (value == null)
422+
throw new HoconParserException($"Hocon path {path} does not exist.");
449423

450424
return value.GetByteList();
451425
}
@@ -461,9 +435,9 @@ public IList<string> GetStringList(string path)
461435
/// <inheritdoc cref="GetStringList(string)"/>
462436
public IList<string> GetStringList(HoconPath path)
463437
{
464-
HoconValue value = GetNode(path);
465-
if (ReferenceEquals(value, HoconValue.Undefined))
466-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
438+
var value = GetNode(path);
439+
if (value == null)
440+
throw new HoconParserException($"Hocon path {path} does not exist.");
467441

468442
return value.GetStringList();
469443
}
@@ -479,9 +453,8 @@ public IList<HoconObject> GetObjectList(string path)
479453
public IList<HoconObject> GetObjectList(HoconPath path)
480454
{
481455
var value = GetNode(path);
482-
483-
if (ReferenceEquals(value, HoconValue.Undefined))
484-
throw new HoconParserException($"Hocon path {path} was not an array or a numerically-indexed object.");
456+
if (value == null)
457+
throw new HoconParserException($"Hocon path {path} does not exist.");
485458

486459
return value.GetObjectList();
487460
}
@@ -497,7 +470,7 @@ public HoconValue GetValue(string path)
497470
/// <inheritdoc cref="GetValue(string)"/>
498471
public HoconValue GetValue(HoconPath path)
499472
{
500-
HoconValue value = GetNode(path);
473+
var value = GetNode(path);
501474
return value;
502475
}
503476

@@ -524,11 +497,8 @@ public TimeSpan GetTimeSpan(string path, TimeSpan? @default = null, bool allowIn
524497
/// <inheritdoc cref="GetTimeSpan(string,System.Nullable{System.TimeSpan},bool)"/>
525498
public TimeSpan GetTimeSpan(HoconPath path, TimeSpan? @default = null, bool allowInfinite = true)
526499
{
527-
HoconValue value = GetNode(path);
528-
if (value == null)
529-
return @default.GetValueOrDefault();
530-
531-
return value.GetTimeSpan(allowInfinite);
500+
var value = GetNode(path);
501+
return value == null ? @default.GetValueOrDefault() : value.GetTimeSpan(allowInfinite);
532502
}
533503
#endregion
534504

src/Hocon/Impl/HoconField.cs

+9-12
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public sealed class HoconField:IHoconElement
3131
public IHoconElement Parent { get; }
3232

3333
/// <inheritdoc/>
34-
public HoconType Type => Value.Type;
34+
public HoconType Type => Value == null ? HoconType.Empty : Value.Type;
3535

3636
/// <inheritdoc/>
3737
public string Raw => Value.Raw;
@@ -62,7 +62,7 @@ public HoconValue Value
6262
switch (_internalValues.Count)
6363
{
6464
case 0:
65-
return HoconValue.Undefined;
65+
return null;
6666
case 1:
6767
return _internalValues[0];
6868
default:
@@ -80,17 +80,14 @@ public HoconValue Value
8080
}
8181

8282
var value = new HoconValue(this);
83-
switch (objects.Count)
83+
if (objects.Count == 1)
8484
{
85-
case 0:
86-
throw new HoconException("Should never reach this line.");
87-
case 1:
88-
value.Add(objects[0]);
89-
break;
90-
default:
91-
var resultObject = new HoconMergedObject(value, objects);
92-
value.Add(resultObject);
93-
break;
85+
value.Add(objects[0]);
86+
}
87+
else
88+
{
89+
var resultObject = new HoconMergedObject(value, objects);
90+
value.Add(resultObject);
9491
}
9592
return value;
9693
}

src/Hocon/Impl/HoconObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public List<HoconValue> GetArray()
107107
var type = HoconType.Empty;
108108
foreach (var field in Values)
109109
{
110-
if (ReferenceEquals(field.Value, HoconValue.Undefined) || !int.TryParse(field.Key, out var index) || index < 0)
110+
if (field.Value == null || !int.TryParse(field.Key, out var index) || index < 0)
111111
continue;
112112
if (type == HoconType.Empty)
113113
type = field.Type;

src/Hocon/Impl/HoconValue.cs

-6
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@ namespace Hocon
2222
/// </summary>
2323
public class HoconValue : List<IHoconElement>, IHoconElement
2424
{
25-
public static readonly HoconValue Undefined;
2625
private static readonly Regex TimeSpanRegex = new Regex(@"^(?<value>([0-9]+(\.[0-9]+)?))\s*(?<unit>(nanoseconds|nanosecond|nanos|nano|ns|microseconds|microsecond|micros|micro|us|milliseconds|millisecond|millis|milli|ms|seconds|second|s|minutes|minute|m|hours|hour|h|days|day|d))$", RegexOptions.Compiled);
2726

28-
static HoconValue()
29-
{
30-
Undefined = new HoconEmptyValue(null);
31-
}
32-
3327
public IHoconElement Parent { get; }
3428

3529
public virtual HoconType Type { get; private set; } = HoconType.Empty;

0 commit comments

Comments
 (0)