Skip to content

Commit

Permalink
Refactor FlagMetadata class to inherit from BaseMetadata
Browse files Browse the repository at this point in the history
Signed-off-by: André Silva <[email protected]>
  • Loading branch information
askpt committed Jan 31, 2024
1 parent a33db20 commit cc91037
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
57 changes: 57 additions & 0 deletions src/OpenFeature/Model/BaseMetadata.cs
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

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata'

Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata'

Check failure on line 8 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'BaseMetadata'

Check failure on line 8 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'BaseMetadata'

Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata'

Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata'

Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'BaseMetadata'

Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'BaseMetadata'

Check warning on line 8 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'BaseMetadata'
{
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

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetBool(string)'

Check warning on line 21 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetBool(string)'

Check failure on line 21 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'BaseMetadata.GetBool(string)'

Check failure on line 21 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'BaseMetadata.GetBool(string)'

Check warning on line 21 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetBool(string)'

Check warning on line 21 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetBool(string)'

Check warning on line 21 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'BaseMetadata.GetBool(string)'
{
return this.GetValue<bool>(key);
}

public virtual int? GetInt(string key)

Check warning on line 26 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetInt(string)'

Check failure on line 26 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'BaseMetadata.GetInt(string)'

Check warning on line 26 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetInt(string)'

Check warning on line 26 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'BaseMetadata.GetInt(string)'
{
return this.GetValue<int>(key);
}

public virtual double? GetDouble(string key)

Check warning on line 31 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetDouble(string)'

Check failure on line 31 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'BaseMetadata.GetDouble(string)'

Check warning on line 31 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetDouble(string)'

Check warning on line 31 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'BaseMetadata.GetDouble(string)'
{
return this.GetValue<double>(key);
}

public virtual string? GetString(string key)

Check warning on line 36 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetString(string)'

Check failure on line 36 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'BaseMetadata.GetString(string)'

Check warning on line 36 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'BaseMetadata.GetString(string)'

Check warning on line 36 in src/OpenFeature/Model/BaseMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'BaseMetadata.GetString(string)'
{
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)}");
}
}
46 changes: 3 additions & 43 deletions src/OpenFeature/Model/FlagMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,13 @@
#nullable enable
namespace OpenFeature.Model;

public sealed class FlagMetadata
public sealed class FlagMetadata : BaseMetadata

Check warning on line 8 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'FlagMetadata'

Check failure on line 8 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'FlagMetadata'

Check warning on line 8 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'FlagMetadata'

Check warning on line 8 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'FlagMetadata'
{
private readonly ImmutableDictionary<string, object> _metadata;

public FlagMetadata(Dictionary<string, object> metadata)
{
this._metadata = metadata.ToImmutableDictionary();
}

public FlagMetadata() : this([])
{
}

public bool? GetBool(string key)
{
return this.GetValue<bool>(key);
}

public int? GetInt(string key)
public FlagMetadata()

Check warning on line 10 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'FlagMetadata.FlagMetadata()'

Check failure on line 10 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'FlagMetadata.FlagMetadata()'

Check warning on line 10 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'FlagMetadata.FlagMetadata()'

Check warning on line 10 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'FlagMetadata.FlagMetadata()'
{
return this.GetValue<int>(key);
}

public double? GetDouble(string key)
public FlagMetadata(Dictionary<string, object> metadata) : base(metadata)

Check warning on line 14 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing XML comment for publicly visible type or member 'FlagMetadata.FlagMetadata(Dictionary<string, object>)'

Check failure on line 14 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / e2e-tests

Missing XML comment for publicly visible type or member 'FlagMetadata.FlagMetadata(Dictionary<string, object>)'

Check warning on line 14 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing XML comment for publicly visible type or member 'FlagMetadata.FlagMetadata(Dictionary<string, object>)'

Check warning on line 14 in src/OpenFeature/Model/FlagMetadata.cs

View workflow job for this annotation

GitHub Actions / packaging

Missing XML comment for publicly visible type or member 'FlagMetadata.FlagMetadata(Dictionary<string, object>)'
{
return this.GetValue<double>(key);
}

public string? GetString(string key)
{
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)}");
}
}

0 comments on commit cc91037

Please sign in to comment.