1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Collections . Immutable ;
3
4
4
5
namespace OpenFeatureSDK . Model
5
6
{
@@ -8,9 +9,31 @@ namespace OpenFeatureSDK.Model
8
9
/// to the feature flag evaluation context.
9
10
/// </summary>
10
11
/// <seealso href="https://github.com/open-feature/spec/blob/main/specification/evaluation-context.md">Evaluation context</seealso>
11
- public class EvaluationContext
12
+ public sealed class EvaluationContext
12
13
{
13
- private readonly Structure _structure = new Structure ( ) ;
14
+ private readonly Structure _structure ;
15
+
16
+ /// <summary>
17
+ /// Internal constructor used by the builder.
18
+ /// </summary>
19
+ /// <param name="content">The content of the context.</param>
20
+ internal EvaluationContext ( Structure content )
21
+ {
22
+ this . _structure = content ;
23
+ }
24
+
25
+ /// <summary>
26
+ /// Private constructor for making an empty <see cref="EvaluationContext"/>.
27
+ /// </summary>
28
+ private EvaluationContext ( )
29
+ {
30
+ this . _structure = Structure . Empty ;
31
+ }
32
+
33
+ /// <summary>
34
+ /// An empty evaluation context.
35
+ /// </summary>
36
+ public static EvaluationContext Empty { get ; } = new EvaluationContext ( ) ;
14
37
15
38
/// <summary>
16
39
/// Gets the Value at the specified key
@@ -35,15 +58,6 @@ public class EvaluationContext
35
58
/// </exception>
36
59
public bool ContainsKey ( string key ) => this . _structure . ContainsKey ( key ) ;
37
60
38
- /// <summary>
39
- /// Removes the Value at the specified key
40
- /// </summary>
41
- /// <param name="key">The key of the value to be removed</param>
42
- /// <exception cref="ArgumentNullException">
43
- /// Thrown when the key is <see langword="null" />
44
- /// </exception>
45
- public void Remove ( string key ) => this . _structure . Remove ( key ) ;
46
-
47
61
/// <summary>
48
62
/// Gets the value associated with the specified key
49
63
/// </summary>
@@ -59,153 +73,9 @@ public class EvaluationContext
59
73
/// Gets all values as a Dictionary
60
74
/// </summary>
61
75
/// <returns>New <see cref="IDictionary{TKey,TValue}"/> representation of this Structure</returns>
62
- public IDictionary < string , Value > AsDictionary ( )
63
- {
64
- return new Dictionary < string , Value > ( this . _structure . AsDictionary ( ) ) ;
65
- }
66
-
67
- /// <summary>
68
- /// Add a new bool Value to the evaluation context
69
- /// </summary>
70
- /// <param name="key">The key of the value to be added</param>
71
- /// <param name="value">The value to be added</param>
72
- /// <returns>This <see cref="EvaluationContext"/></returns>
73
- /// <exception cref="ArgumentNullException">
74
- /// Thrown when the key is <see langword="null" />
75
- /// </exception>
76
- /// <exception cref="ArgumentException">
77
- /// Thrown when an element with the same key is already contained in the context
78
- /// </exception>
79
- public EvaluationContext Add ( string key , bool value )
76
+ public IImmutableDictionary < string , Value > AsDictionary ( )
80
77
{
81
- this . _structure . Add ( key , value ) ;
82
- return this ;
83
- }
84
-
85
- /// <summary>
86
- /// Add a new string Value to the evaluation context
87
- /// </summary>
88
- /// <param name="key">The key of the value to be added</param>
89
- /// <param name="value">The value to be added</param>
90
- /// <returns>This <see cref="EvaluationContext"/></returns>
91
- /// <exception cref="ArgumentNullException">
92
- /// Thrown when the key is <see langword="null" />
93
- /// </exception>
94
- /// <exception cref="ArgumentException">
95
- /// Thrown when an element with the same key is already contained in the context
96
- /// </exception>
97
- public EvaluationContext Add ( string key , string value )
98
- {
99
- this . _structure . Add ( key , value ) ;
100
- return this ;
101
- }
102
-
103
- /// <summary>
104
- /// Add a new int Value to the evaluation context
105
- /// </summary>
106
- /// <param name="key">The key of the value to be added</param>
107
- /// <param name="value">The value to be added</param>
108
- /// <returns>This <see cref="EvaluationContext"/></returns>
109
- /// <exception cref="ArgumentNullException">
110
- /// Thrown when the key is <see langword="null" />
111
- /// </exception>
112
- /// <exception cref="ArgumentException">
113
- /// Thrown when an element with the same key is already contained in the context
114
- /// </exception>
115
- public EvaluationContext Add ( string key , int value )
116
- {
117
- this . _structure . Add ( key , value ) ;
118
- return this ;
119
- }
120
-
121
- /// <summary>
122
- /// Add a new double Value to the evaluation context
123
- /// </summary>
124
- /// <param name="key">The key of the value to be added</param>
125
- /// <param name="value">The value to be added</param>
126
- /// <returns>This <see cref="EvaluationContext"/></returns>
127
- /// <exception cref="ArgumentNullException">
128
- /// Thrown when the key is <see langword="null" />
129
- /// </exception>
130
- /// <exception cref="ArgumentException">
131
- /// Thrown when an element with the same key is already contained in the context
132
- /// </exception>
133
- public EvaluationContext Add ( string key , double value )
134
- {
135
- this . _structure . Add ( key , value ) ;
136
- return this ;
137
- }
138
-
139
- /// <summary>
140
- /// Add a new DateTime Value to the evaluation context
141
- /// </summary>
142
- /// <param name="key">The key of the value to be added</param>
143
- /// <param name="value">The value to be added</param>
144
- /// <returns>This <see cref="EvaluationContext"/></returns>
145
- /// <exception cref="ArgumentNullException">
146
- /// Thrown when the key is <see langword="null" />
147
- /// </exception>
148
- /// <exception cref="ArgumentException">
149
- /// Thrown when an element with the same key is already contained in the context
150
- /// </exception>
151
- public EvaluationContext Add ( string key , DateTime value )
152
- {
153
- this . _structure . Add ( key , value ) ;
154
- return this ;
155
- }
156
-
157
- /// <summary>
158
- /// Add a new Structure Value to the evaluation context
159
- /// </summary>
160
- /// <param name="key">The key of the value to be added</param>
161
- /// <param name="value">The value to be added</param>
162
- /// <returns>This <see cref="EvaluationContext"/></returns>
163
- /// <exception cref="ArgumentNullException">
164
- /// Thrown when the key is <see langword="null" />
165
- /// </exception>
166
- /// <exception cref="ArgumentException">
167
- /// Thrown when an element with the same key is already contained in the context
168
- /// </exception>
169
- public EvaluationContext Add ( string key , Structure value )
170
- {
171
- this . _structure . Add ( key , value ) ;
172
- return this ;
173
- }
174
-
175
- /// <summary>
176
- /// Add a new List Value to the evaluation context
177
- /// </summary>
178
- /// <param name="key">The key of the value to be added</param>
179
- /// <param name="value">The value to be added</param>
180
- /// <returns>This <see cref="EvaluationContext"/></returns>
181
- /// <exception cref="ArgumentNullException">
182
- /// Thrown when the key is <see langword="null" />
183
- /// </exception>
184
- /// <exception cref="ArgumentException">
185
- /// Thrown when an element with the same key is already contained in the context
186
- /// </exception>
187
- public EvaluationContext Add ( string key , List < Value > value )
188
- {
189
- this . _structure . Add ( key , value ) ;
190
- return this ;
191
- }
192
-
193
- /// <summary>
194
- /// Add a new Value to the evaluation context
195
- /// </summary>
196
- /// <param name="key">The key of the value to be added</param>
197
- /// <param name="value">The value to be added</param>
198
- /// <returns>This <see cref="EvaluationContext"/></returns>
199
- /// <exception cref="ArgumentNullException">
200
- /// Thrown when the key is <see langword="null" />
201
- /// </exception>
202
- /// <exception cref="ArgumentException">
203
- /// Thrown when an element with the same key is already contained in the context
204
- /// </exception>
205
- public EvaluationContext Add ( string key , Value value )
206
- {
207
- this . _structure . Add ( key , value ) ;
208
- return this ;
78
+ return this . _structure . AsDictionary ( ) ;
209
79
}
210
80
211
81
/// <summary>
@@ -214,32 +84,21 @@ public EvaluationContext Add(string key, Value value)
214
84
public int Count => this . _structure . Count ;
215
85
216
86
/// <summary>
217
- /// Merges provided evaluation context into this one.
218
- /// Any duplicate keys will be overwritten.
87
+ /// Return an enumerator for all values
219
88
/// </summary>
220
- /// <param name="other"><see cref="EvaluationContext"/></param >
221
- public void Merge ( EvaluationContext other )
89
+ /// <returns>An enumerator for all values</returns >
90
+ public IEnumerator < KeyValuePair < string , Value > > GetEnumerator ( )
222
91
{
223
- foreach ( var key in other . _structure . Keys )
224
- {
225
- if ( this . _structure . ContainsKey ( key ) )
226
- {
227
- this . _structure [ key ] = other . _structure [ key ] ;
228
- }
229
- else
230
- {
231
- this . _structure . Add ( key , other . _structure [ key ] ) ;
232
- }
233
- }
92
+ return this . _structure . GetEnumerator ( ) ;
234
93
}
235
94
236
95
/// <summary>
237
- /// Return an enumerator for all values
96
+ /// Get a builder which can build an <see cref="EvaluationContext"/>.
238
97
/// </summary>
239
- /// <returns>An enumerator for all values </returns>
240
- public IEnumerator < KeyValuePair < string , Value > > GetEnumerator ( )
98
+ /// <returns>The builder </returns>
99
+ public static EvaluationContextBuilder Builder ( )
241
100
{
242
- return this . _structure . GetEnumerator ( ) ;
101
+ return new EvaluationContextBuilder ( ) ;
243
102
}
244
103
}
245
104
}
0 commit comments