-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
Signed-off-by: André Silva <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
|
||
#nullable enable | ||
namespace OpenFeature.Model; | ||
|
||
public abstract class BaseMetadata | ||
Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (ubuntu-latest)
Check failure on line 8 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / e2e-tests
Check failure on line 8 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / e2e-tests
Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (windows-latest)
Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (windows-latest)
Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / packaging
Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / packaging
|
||
{ | ||
private readonly ImmutableDictionary<string, object> _metadata; | ||
|
||
internal BaseMetadata(Dictionary<string, object> metadata) | ||
{ | ||
this._metadata = metadata.ToImmutableDictionary(); | ||
} | ||
|
||
internal BaseMetadata() : this([]) | ||
{ | ||
} | ||
|
||
public virtual bool? GetBool(string key) | ||
Check warning on line 21 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 21 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (ubuntu-latest)
Check failure on line 21 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / e2e-tests
Check failure on line 21 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / e2e-tests
Check warning on line 21 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (windows-latest)
Check warning on line 21 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (windows-latest)
|
||
{ | ||
return this.GetValue<bool>(key); | ||
} | ||
|
||
public virtual int? GetInt(string key) | ||
Check warning on line 26 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (ubuntu-latest)
Check failure on line 26 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / e2e-tests
Check warning on line 26 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (windows-latest)
|
||
{ | ||
return this.GetValue<int>(key); | ||
} | ||
|
||
public virtual double? GetDouble(string key) | ||
Check warning on line 31 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (ubuntu-latest)
Check failure on line 31 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / e2e-tests
Check warning on line 31 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (windows-latest)
|
||
{ | ||
return this.GetValue<double>(key); | ||
} | ||
|
||
public virtual string? GetString(string key) | ||
Check warning on line 36 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (ubuntu-latest)
Check failure on line 36 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / e2e-tests
Check warning on line 36 in src/OpenFeature/Model/BaseMetadata.cs GitHub Actions / build (windows-latest)
|
||
{ | ||
var hasValue = this._metadata.TryGetValue(key, out var value); | ||
if (!hasValue) | ||
{ | ||
return null; | ||
} | ||
|
||
return value as string ?? throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(string)}"); | ||
} | ||
|
||
private T? GetValue<T>(string key) where T : struct | ||
{ | ||
var hasValue = this._metadata.TryGetValue(key, out var value); | ||
if (!hasValue) | ||
{ | ||
return null; | ||
} | ||
|
||
return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(T)}"); | ||
} | ||
} |