forked from open-feature/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluationContext.cs
112 lines (100 loc) · 4.38 KB
/
EvaluationContext.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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace OpenFeature.Model
{
/// <summary>
/// A KeyValuePair with a string key and object value that is used to apply user defined properties
/// to the feature flag evaluation context.
/// </summary>
/// <seealso href="https://github.com/open-feature/spec/blob/v0.5.2/specification/sections/03-evaluation-context.md">Evaluation context</seealso>
public sealed class EvaluationContext
{
private readonly Structure _structure;
/// <summary>
/// Internal constructor used by the builder.
/// </summary>
/// <param name="targetingKey">The targeting key</param>
/// <param name="content">The content of the context.</param>
internal EvaluationContext(string targetingKey, Structure content)
{
this.TargetingKey = targetingKey;
this._structure = content;
}
/// <summary>
/// Private constructor for making an empty <see cref="EvaluationContext"/>.
/// </summary>
private EvaluationContext()
{
this._structure = Structure.Empty;
this.TargetingKey = string.Empty;
}
/// <summary>
/// An empty evaluation context.
/// </summary>
public static EvaluationContext Empty { get; } = new EvaluationContext();
/// <summary>
/// Gets the Value at the specified key
/// </summary>
/// <param name="key">The key of the value to be retrieved</param>
/// <returns>The <see cref="Value"/> associated with the key</returns>
/// <exception cref="KeyNotFoundException">
/// Thrown when the context does not contain the specified key
/// </exception>
/// <exception cref="ArgumentNullException">
/// Thrown when the key is <see langword="null" />
/// </exception>
public Value GetValue(string key) => this._structure.GetValue(key);
/// <summary>
/// Bool indicating if the specified key exists in the evaluation context
/// </summary>
/// <param name="key">The key of the value to be checked</param>
/// <returns><see cref="bool" />indicating the presence of the key</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the key is <see langword="null" />
/// </exception>
public bool ContainsKey(string key) => this._structure.ContainsKey(key);
/// <summary>
/// Gets the value associated with the specified key
/// </summary>
/// <param name="value">The <see cref="Value"/> or <see langword="null" /> if the key was not present</param>
/// <param name="key">The key of the value to be retrieved</param>
/// <returns><see cref="bool" />indicating the presence of the key</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the key is <see langword="null" />
/// </exception>
public bool TryGetValue(string key, out Value value) => this._structure.TryGetValue(key, out value);
/// <summary>
/// Gets all values as a Dictionary
/// </summary>
/// <returns>New <see cref="IDictionary{TKey,TValue}"/> representation of this Structure</returns>
public IImmutableDictionary<string, Value> AsDictionary()
{
return this._structure.AsDictionary();
}
/// <summary>
/// Return a count of all values
/// </summary>
public int Count => this._structure.Count;
/// <summary>
/// Returns the targeting key for the context.
/// </summary>
public string TargetingKey { get; }
/// <summary>
/// Return an enumerator for all values
/// </summary>
/// <returns>An enumerator for all values</returns>
public IEnumerator<KeyValuePair<string, Value>> GetEnumerator()
{
return this._structure.GetEnumerator();
}
/// <summary>
/// Get a builder which can build an <see cref="EvaluationContext"/>.
/// </summary>
/// <returns>The builder</returns>
public static EvaluationContextBuilder Builder()
{
return new EvaluationContextBuilder();
}
}
}