@@ -20,41 +20,32 @@ namespace Hocon
20
20
[ Serializable ]
21
21
public class Config : HoconRoot , ISerializable
22
22
{
23
- /// <summary>
24
- /// INTERNAL API
25
- ///
26
- /// Special case for empty configurations. Immutable and can't be added as a fallback.
27
- /// </summary>
28
- internal sealed class EmptyConfig : Config
29
- {
30
- public static EmptyConfig Instance = new EmptyConfig ( ) ;
31
-
32
- private EmptyConfig ( ) : base ( new HoconRoot ( new HoconEmptyValue ( null ) ) )
33
- {
34
- }
35
-
36
- protected override Config Copy ( Config fallback = null )
37
- {
38
- return Instance ;
39
- }
40
-
41
- public override Config WithFallback ( Config fallback )
42
- {
43
- return fallback ;
44
- }
45
- }
23
+ private static readonly HoconValue _emptyValue ;
46
24
47
25
public const string SerializedPropertyName = "_dump" ;
48
26
27
+ static Config ( )
28
+ {
29
+ _emptyValue = new HoconValue ( null ) ;
30
+ _emptyValue . Add ( new HoconObject ( _emptyValue ) ) ;
31
+ }
32
+
49
33
[ Obsolete ( "For json serialization/deserialization only" , true ) ]
50
34
private Config ( )
51
35
{
52
36
}
53
-
37
+
54
38
/// <inheritdoc />
55
39
/// <summary>
56
40
/// Initializes a new instance of the <see cref="Config" /> class.
57
41
/// </summary>
42
+ private Config ( HoconValue value )
43
+ {
44
+ Value = value ;
45
+ Root = GetRootValue ( ) ;
46
+ }
47
+
48
+ /// <inheritdoc cref="Config(HoconValue)" />
58
49
private Config ( HoconValue value , Config fallback )
59
50
{
60
51
Fallback = fallback ;
@@ -63,15 +54,16 @@ private Config(HoconValue value, Config fallback)
63
54
Root = GetRootValue ( ) ;
64
55
}
65
56
66
- /// <inheritdoc cref="Config(HoconValue, Config)" />
57
+
58
+ /// <inheritdoc cref="Config(HoconValue)" />
67
59
/// <param name="root">The root node to base this configuration.</param>
68
60
/// <exception cref="T:System.ArgumentNullException">"The root value cannot be null."</exception>
69
61
public Config ( HoconRoot root ) : base ( root ? . Value , root ? . Substitutions ?? Enumerable . Empty < HoconSubstitution > ( ) )
70
62
{
71
63
Root = GetRootValue ( ) ;
72
64
}
73
65
74
- /// <inheritdoc cref="Config(HoconValue, Config )" />
66
+ /// <inheritdoc cref="Config(HoconValue)" />
75
67
/// <param name="source">The configuration to use as the primary source.</param>
76
68
/// <param name="fallback">The configuration to use as a secondary source.</param>
77
69
/// <exception cref="ArgumentNullException">The source configuration cannot be null.</exception>
@@ -89,7 +81,7 @@ public Config(HoconRoot source, Config fallback) : base(source?.Value,
89
81
/// <remarks>
90
82
/// Added for brevity and API backwards-compatibility with Akka.Hocon.
91
83
/// </remarks>
92
- public static Config Empty => ConfigurationFactory . Empty ;
84
+ public static Config Empty => CreateEmpty ( ) ;
93
85
94
86
/// <summary>
95
87
/// The configuration used as a secondary source.
@@ -101,6 +93,11 @@ public Config(HoconRoot source, Config fallback) : base(source?.Value,
101
93
/// </summary>
102
94
public virtual HoconValue Root { get ; }
103
95
96
+ /// <summary>
97
+ /// Determines if this root node contains any values
98
+ /// </summary>
99
+ public virtual bool IsEmpty => Value == _emptyValue ;
100
+
104
101
/// <summary>
105
102
/// Returns string representation of <see cref="Config" />, allowing to include fallback values
106
103
/// </summary>
@@ -166,7 +163,7 @@ public virtual Config WithFallback(Config fallback)
166
163
if ( fallback == this )
167
164
throw new ArgumentException ( "Config can not have itself as fallback" , nameof ( fallback ) ) ;
168
165
169
- if ( fallback == Config . Empty )
166
+ if ( fallback . IsEmpty )
170
167
return this ; // no-op
171
168
172
169
// If Fallback is not set - we will set it in new copy
@@ -219,7 +216,14 @@ public override IEnumerable<KeyValuePair<string, HoconField>> AsEnumerable()
219
216
used . Add ( kvp . Key ) ;
220
217
}
221
218
}
222
-
219
+
220
+ private static Config CreateEmpty ( )
221
+ {
222
+ var value = new HoconValue ( null ) ;
223
+ value . Add ( new HoconObject ( value ) ) ;
224
+ return new Config ( value ) ;
225
+ }
226
+
223
227
/// <summary>
224
228
/// Performs aggregation of Value and all Fallbacks into single <see cref="HoconValue"/> object
225
229
/// </summary>
0 commit comments