Skip to content

Commit

Permalink
feat!: Use same type for flag metadata and event metadata
Browse files Browse the repository at this point in the history
The spec describes two types(flag metadata, and event metadata) that are functionally the same. This PR
makes a breaking change to bring both of the types to use a generic ImmutableMetadata type.

- Rename BaseMetadata to Immutable, make sealed class and implement a empty constructor

Fixes: #234

Signed-off-by: Benjamin Evenson <2031163+benjiro@users.noreply.github.com>
  • Loading branch information
benjiro authored and toddbaert committed Apr 22, 2024
1 parent 43f14cc commit a016f66
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 198 deletions.
120 changes: 60 additions & 60 deletions src/OpenFeature/Model/FlagEvaluationDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,74 @@

namespace OpenFeature.Model
{
/// <summary>
/// The contract returned to the caller that describes the result of the flag evaluation process.
/// </summary>
/// <typeparam name="T">Flag value type</typeparam>
/// <seealso href="https://github.com/open-feature/spec/blob/v0.7.0/specification/types.md#evaluation-details"/>
public sealed class FlagEvaluationDetails<T>
{
/// <summary>
/// Feature flag evaluated value
/// The contract returned to the caller that describes the result of the flag evaluation process.
/// </summary>
public T Value { get; }
/// <typeparam name="T">Flag value type</typeparam>
/// <seealso href="https://github.com/open-feature/spec/blob/v0.7.0/specification/types.md#evaluation-details"/>
public sealed class FlagEvaluationDetails<T>
{
/// <summary>
/// Feature flag evaluated value
/// </summary>
public T Value { get; }

/// <summary>
/// Feature flag key
/// </summary>
public string FlagKey { get; }
/// <summary>
/// Feature flag key
/// </summary>
public string FlagKey { get; }

/// <summary>
/// Error that occurred during evaluation
/// </summary>
public ErrorType ErrorType { get; }
/// <summary>
/// Error that occurred during evaluation
/// </summary>
public ErrorType ErrorType { get; }

/// <summary>
/// Message containing additional details about an error.
/// <para>
/// Will be <see langword="null" /> if there is no error or if the provider didn't provide any additional error
/// details.
/// </para>
/// </summary>
public string? ErrorMessage { get; }
/// <summary>
/// Message containing additional details about an error.
/// <para>
/// Will be <see langword="null" /> if there is no error or if the provider didn't provide any additional error
/// details.
/// </para>
/// </summary>
public string? ErrorMessage { get; }

/// <summary>
/// Describes the reason for the outcome of the evaluation process
/// </summary>
public string? Reason { get; }
/// <summary>
/// Describes the reason for the outcome of the evaluation process
/// </summary>
public string? Reason { get; }

/// <summary>
/// A variant is a semantic identifier for a value. This allows for referral to particular values without
/// necessarily including the value itself, which may be quite prohibitively large or otherwise unsuitable
/// in some cases.
/// </summary>
public string? Variant { get; }
/// <summary>
/// A variant is a semantic identifier for a value. This allows for referral to particular values without
/// necessarily including the value itself, which may be quite prohibitively large or otherwise unsuitable
/// in some cases.
/// </summary>
public string? Variant { get; }

/// <summary>
/// A structure which supports definition of arbitrary properties, with keys of type string, and values of type boolean, string, or number.
/// </summary>
public FlagMetadata? FlagMetadata { get; }
/// <summary>
/// A structure which supports definition of arbitrary properties, with keys of type string, and values of type boolean, string, or number.
/// </summary>
public ImmutableMetadata? FlagMetadata { get; }

/// <summary>
/// Initializes a new instance of the <see cref="FlagEvaluationDetails{T}"/> class.
/// </summary>
/// <param name="flagKey">Feature flag key</param>
/// <param name="value">Evaluated value</param>
/// <param name="errorType">Error</param>
/// <param name="reason">Reason</param>
/// <param name="variant">Variant</param>
/// <param name="errorMessage">Error message</param>
/// <param name="flagMetadata">Flag metadata</param>
public FlagEvaluationDetails(string flagKey, T value, ErrorType errorType, string? reason, string? variant,
string? errorMessage = null, FlagMetadata? flagMetadata = null)
{
this.Value = value;
this.FlagKey = flagKey;
this.ErrorType = errorType;
this.Reason = reason;
this.Variant = variant;
this.ErrorMessage = errorMessage;
this.FlagMetadata = flagMetadata;
/// <summary>
/// Initializes a new instance of the <see cref="FlagEvaluationDetails{T}"/> class.
/// </summary>
/// <param name="flagKey">Feature flag key</param>
/// <param name="value">Evaluated value</param>
/// <param name="errorType">Error</param>
/// <param name="reason">Reason</param>
/// <param name="variant">Variant</param>
/// <param name="errorMessage">Error message</param>
/// <param name="flagMetadata">Flag metadata</param>
public FlagEvaluationDetails(string flagKey, T value, ErrorType errorType, string? reason, string? variant,
string? errorMessage = null, ImmutableMetadata? flagMetadata = null)
{
this.Value = value;
this.FlagKey = flagKey;
this.ErrorType = errorType;
this.Reason = reason;
this.Variant = variant;
this.ErrorMessage = errorMessage;
this.FlagMetadata = flagMetadata;
}
}
}
}
25 changes: 0 additions & 25 deletions src/OpenFeature/Model/FlagMetadata.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
using System.Collections.Generic;
using System.Collections.Immutable;

#nullable enable
namespace OpenFeature.Model;

/// <summary>
/// Represents the base class for metadata objects.
/// Represents immutable metadata associated with feature flags and events.
/// </summary>
public abstract class BaseMetadata
/// <seealso href="https://github.com/open-feature/spec/blob/v0.7.0/specification/types.md#flag-metadata"/>
/// <seealso href="https://github.com/open-feature/spec/blob/v0.7.0/specification/types.md#event-metadata"/>
public sealed class ImmutableMetadata
{
private readonly ImmutableDictionary<string, object> _metadata;

internal BaseMetadata(Dictionary<string, object> metadata)
/// <summary>
/// Constructor for the <see cref="ImmutableMetadata"/> class.
/// </summary>
public ImmutableMetadata()
{
this._metadata = ImmutableDictionary<string, object>.Empty;
}

/// <summary>
/// Constructor for the <see cref="ImmutableMetadata"/> class.
/// </summary>
/// <param name="metadata">The dictionary containing the metadata.</param>
public ImmutableMetadata(Dictionary<string, object> metadata)
{
this._metadata = metadata.ToImmutableDictionary();
}
Expand All @@ -20,7 +35,7 @@ internal BaseMetadata(Dictionary<string, object> metadata)
/// </summary>
/// <param name="key">The key of the value to retrieve.</param>
/// <returns>The boolean value associated with the key, or null if the key is not found.</returns>
public virtual bool? GetBool(string key)
public bool? GetBool(string key)
{
return this.GetValue<bool>(key);
}
Expand All @@ -30,7 +45,7 @@ internal BaseMetadata(Dictionary<string, object> metadata)
/// </summary>
/// <param name="key">The key of the value to retrieve.</param>
/// <returns>The integer value associated with the key, or null if the key is not found.</returns>
public virtual int? GetInt(string key)
public int? GetInt(string key)
{
return this.GetValue<int>(key);
}
Expand All @@ -40,7 +55,7 @@ internal BaseMetadata(Dictionary<string, object> metadata)
/// </summary>
/// <param name="key">The key of the value to retrieve.</param>
/// <returns>The double value associated with the key, or null if the key is not found.</returns>
public virtual double? GetDouble(string key)
public double? GetDouble(string key)
{
return this.GetValue<double>(key);
}
Expand All @@ -50,7 +65,7 @@ internal BaseMetadata(Dictionary<string, object> metadata)
/// </summary>
/// <param name="key">The key of the value to retrieve.</param>
/// <returns>The string value associated with the key, or null if the key is not found.</returns>
public virtual string? GetString(string key)
public string? GetString(string key)
{
var hasValue = this._metadata.TryGetValue(key, out var value);
if (!hasValue)
Expand Down
55 changes: 27 additions & 28 deletions src/OpenFeature/Model/ProviderEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,39 @@

namespace OpenFeature.Model
{
/// <summary>
/// The EventHandlerDelegate is an implementation of an Event Handler
/// </summary>
public delegate void EventHandlerDelegate(ProviderEventPayload? eventDetails);

/// <summary>
/// Contains the payload of an OpenFeature Event.
/// </summary>
public class ProviderEventPayload
{
/// <summary>
/// Name of the provider.
/// The EventHandlerDelegate is an implementation of an Event Handler
/// </summary>
public string? ProviderName { get; set; }
public delegate void EventHandlerDelegate(ProviderEventPayload? eventDetails);

/// <summary>
/// Type of the event
/// Contains the payload of an OpenFeature Event.
/// </summary>
public ProviderEventTypes Type { get; set; }
public class ProviderEventPayload
{
/// <summary>
/// Name of the provider.
/// </summary>
public string? ProviderName { get; set; }

/// <summary>
/// A message providing more information about the event.
/// </summary>
public string? Message { get; set; }
/// <summary>
/// Type of the event
/// </summary>
public ProviderEventTypes Type { get; set; }

/// <summary>
/// A List of flags that have been changed.
/// </summary>
public List<string>? FlagsChanged { get; set; }
/// <summary>
/// A message providing more information about the event.
/// </summary>
public string? Message { get; set; }

/// <summary>
/// Metadata information for the event.
/// </summary>
// TODO: This needs to be changed to a EventMetadata object
public Dictionary<string, object>? EventMetadata { get; set; }
}
/// <summary>
/// A List of flags that have been changed.
/// </summary>
public List<string>? FlagsChanged { get; set; }

/// <summary>
/// Metadata information for the event.
/// </summary>
public ImmutableMetadata? EventMetadata { get; set; }
}
}
Loading

0 comments on commit a016f66

Please sign in to comment.