forked from open-feature/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluationContextBuilder.cs
175 lines (156 loc) · 5.89 KB
/
EvaluationContextBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
namespace OpenFeature.Model
{
/// <summary>
/// A builder which allows the specification of attributes for an <see cref="EvaluationContext"/>.
/// <para>
/// A <see cref="EvaluationContextBuilder"/> object is intended for use by a single thread and should not be used
/// from multiple threads. Once an <see cref="EvaluationContext"/> has been created it is immutable and safe for use
/// from multiple threads.
/// </para>
/// </summary>
public sealed class EvaluationContextBuilder
{
private readonly StructureBuilder _attributes = Structure.Builder();
internal string TargetingKey { get; private set; }
/// <summary>
/// Internal to only allow direct creation by <see cref="EvaluationContext.Builder()"/>.
/// </summary>
internal EvaluationContextBuilder() { }
/// <summary>
/// Set the targeting key for the context.
/// </summary>
/// <param name="targetingKey">The targeting key</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder SetTargetingKey(string targetingKey)
{
this.TargetingKey = targetingKey;
return this;
}
/// <summary>
/// Set the key to the given <see cref="Value"/>.
/// </summary>
/// <param name="key">The key for the value</param>
/// <param name="value">The value to set</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Set(string key, Value value)
{
this._attributes.Set(key, value);
return this;
}
/// <summary>
/// Set the key to the given string.
/// </summary>
/// <param name="key">The key for the value</param>
/// <param name="value">The value to set</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Set(string key, string value)
{
this._attributes.Set(key, value);
return this;
}
/// <summary>
/// Set the key to the given int.
/// </summary>
/// <param name="key">The key for the value</param>
/// <param name="value">The value to set</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Set(string key, int value)
{
this._attributes.Set(key, value);
return this;
}
/// <summary>
/// Set the key to the given double.
/// </summary>
/// <param name="key">The key for the value</param>
/// <param name="value">The value to set</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Set(string key, double value)
{
this._attributes.Set(key, value);
return this;
}
/// <summary>
/// Set the key to the given long.
/// </summary>
/// <param name="key">The key for the value</param>
/// <param name="value">The value to set</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Set(string key, long value)
{
this._attributes.Set(key, value);
return this;
}
/// <summary>
/// Set the key to the given bool.
/// </summary>
/// <param name="key">The key for the value</param>
/// <param name="value">The value to set</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Set(string key, bool value)
{
this._attributes.Set(key, value);
return this;
}
/// <summary>
/// Set the key to the given <see cref="Structure"/>.
/// </summary>
/// <param name="key">The key for the value</param>
/// <param name="value">The value to set</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Set(string key, Structure value)
{
this._attributes.Set(key, value);
return this;
}
/// <summary>
/// Set the key to the given DateTime.
/// </summary>
/// <param name="key">The key for the value</param>
/// <param name="value">The value to set</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Set(string key, DateTime value)
{
this._attributes.Set(key, value);
return this;
}
/// <summary>
/// Incorporate an existing context into the builder.
/// <para>
/// Any existing keys in the builder will be replaced by keys in the context.
/// </para>
/// </summary>
/// <param name="context">The context to add merge</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder Merge(EvaluationContext context)
{
string newTargetingKey = "";
if (TargetingKey != null && TargetingKey.Trim() != string.Empty)
{
newTargetingKey = TargetingKey;
}
if (context.TargetingKey != null && context.TargetingKey.Trim() != string.Empty)
{
newTargetingKey = context.TargetingKey;
}
if (newTargetingKey != null && newTargetingKey.Trim() != string.Empty)
{
this.TargetingKey = newTargetingKey;
}
foreach (var kvp in context)
{
this.Set(kvp.Key, kvp.Value);
}
return this;
}
/// <summary>
/// Build an immutable <see cref="EvaluationContext"/>.
/// </summary>
/// <returns>An immutable <see cref="EvaluationContext"/></returns>
public EvaluationContext Build()
{
return new EvaluationContext(this.TargetingKey, this._attributes.Build());
}
}
}