Skip to content

Commit

Permalink
Convert arg-less methods to props
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <toddbaert@gmail.com>
  • Loading branch information
toddbaert committed Sep 12, 2022
1 parent f97d022 commit 9f8bdf5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 91 deletions.
52 changes: 26 additions & 26 deletions src/OpenFeature.SDK/Model/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ public class Value
/// Creates a Value with the inner set to the object
/// </summary>
/// <param name="value"><see cref="Object">The object to set as the inner value</see></param>
public Value(Object value)
public Value(Object value)
{
// integer is a special case, convert those.
this._innerValue = value is int ? Convert.ToDouble(value) : value;
if (!(this.IsNull()
|| this.IsBoolean()
|| this.IsString()
|| this.IsNumber()
|| this.IsStructure()
|| this.IsList()
|| this.IsDateTime()))
if (!(this.IsNull
|| this.IsBoolean
|| this.IsString
|| this.IsNumber
|| this.IsStructure
|| this.IsList
|| this.IsDateTime))
{
throw new ArgumentException("Invalid value type: " + value.GetType());
}
}


}


/// <summary>
/// Creates a Value with the inner value to the inner value of the value param
/// </summary>
Expand Down Expand Up @@ -90,97 +90,97 @@ public Value(Object value)
/// Determines if inner value is null
/// </summary>
/// <returns><see cref="bool">True if value is null</see></returns>
public bool IsNull() => this._innerValue is null;
public bool IsNull => this._innerValue is null;

/// <summary>
/// Determines if inner value is bool
/// </summary>
/// <returns><see cref="bool">True if value is bool</see></returns>
public bool IsBoolean() => this._innerValue is bool;
public bool IsBoolean => this._innerValue is bool;

/// <summary>
/// Determines if inner value is numeric
/// </summary>
/// <returns><see cref="bool">True if value is double</see></returns>
public bool IsNumber() => this._innerValue is double;
public bool IsNumber => this._innerValue is double;

/// <summary>
/// Determines if inner value is string
/// </summary>
/// <returns><see cref="bool">True if value is string</see></returns>
public bool IsString() => this._innerValue is string;
public bool IsString => this._innerValue is string;

/// <summary>
/// Determines if inner value is <see cref="Structure">Structure</see>
/// </summary>
/// <returns><see cref="bool">True if value is <see cref="Structure">Structure</see></see></returns>
public bool IsStructure() => this._innerValue is Structure;
public bool IsStructure => this._innerValue is Structure;

/// <summary>
/// Determines if inner value is list
/// </summary>
/// <returns><see cref="bool">True if value is list</see></returns>
public bool IsList() => this._innerValue is IList;
public bool IsList => this._innerValue is IList;

/// <summary>
/// Determines if inner value is DateTime
/// </summary>
/// <returns><see cref="bool">True if value is DateTime</see></returns>
public bool IsDateTime() => this._innerValue is DateTime;
public bool IsDateTime => this._innerValue is DateTime;

/// <summary>
/// Returns the underlying inner value as an object. Returns null if the inner value is null.
/// </summary>
/// <returns>Value as object</returns>
public object AsObject() => this._innerValue;
public object AsObject => this._innerValue;

/// <summary>
/// Returns the underlying int value
/// Value will be null if it isn't a integer
/// </summary>
/// <returns>Value as int</returns>
public int? AsInteger() => this.IsNumber() ? (int?)Convert.ToInt32((double?)this._innerValue) : null;
public int? AsInteger => this.IsNumber ? (int?)Convert.ToInt32((double?)this._innerValue) : null;

/// <summary>
/// Returns the underlying bool value
/// Value will be null if it isn't a bool
/// </summary>
/// <returns>Value as bool</returns>
public bool? AsBoolean() => this.IsBoolean() ? (bool?)this._innerValue : null;
public bool? AsBoolean => this.IsBoolean ? (bool?)this._innerValue : null;

/// <summary>
/// Returns the underlying double value
/// Value will be null if it isn't a double
/// </summary>
/// <returns>Value as int</returns>
public double? AsDouble() => this.IsNumber() ? (double?)this._innerValue : null;
public double? AsDouble => this.IsNumber ? (double?)this._innerValue : null;

/// <summary>
/// Returns the underlying string value
/// Value will be null if it isn't a string
/// </summary>
/// <returns>Value as string</returns>
public string AsString() => this.IsString() ? (string)this._innerValue : null;
public string AsString => this.IsString ? (string)this._innerValue : null;

/// <summary>
/// Returns the underlying Structure value
/// Value will be null if it isn't a Structure
/// </summary>
/// <returns>Value as Structure</returns>
public Structure AsStructure() => this.IsStructure() ? (Structure)this._innerValue : null;
public Structure AsStructure => this.IsStructure ? (Structure)this._innerValue : null;

/// <summary>
/// Returns the underlying List value
/// Value will be null if it isn't a List
/// </summary>
/// <returns>Value as List</returns>
public IList<Value> AsList() => this.IsList() ? (IList<Value>)this._innerValue : null;
public IList<Value> AsList => this.IsList ? (IList<Value>)this._innerValue : null;

/// <summary>
/// Returns the underlying DateTime value
/// Value will be null if it isn't a DateTime
/// </summary>
/// <returns>Value as DateTime</returns>
public DateTime? AsDateTime() => this.IsDateTime() ? (DateTime?)this._innerValue : null;
public DateTime? AsDateTime => this.IsDateTime ? (DateTime?)this._innerValue : null;
}
}
2 changes: 1 addition & 1 deletion test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void Should_Get_And_Set_Context()
var VAL = 1;
FeatureClient client = OpenFeature.Instance.GetClient();
client.SetContext(new EvaluationContext().Add(KEY, VAL));
Assert.Equal(VAL, client.GetContext().GetValue(KEY).AsInteger());
Assert.Equal(VAL, client.GetContext().GetValue(KEY).AsInteger);
}
}
}
34 changes: 17 additions & 17 deletions test/OpenFeature.SDK.Tests/OpenFeatureEvaluationContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public void Should_Merge_Two_Contexts()
context1.Merge(context2);

Assert.Equal(2, context1.Count);
Assert.Equal("value1", context1.GetValue("key1").AsString());
Assert.Equal("value2", context1.GetValue("key2").AsString());
Assert.Equal("value1", context1.GetValue("key1").AsString);
Assert.Equal("value2", context1.GetValue("key2").AsString);
}

[Fact]
Expand All @@ -39,8 +39,8 @@ public void Should_Merge_TwoContexts_And_Override_Duplicates_With_RightHand_Cont
context1.Merge(context2);

Assert.Equal(2, context1.Count);
Assert.Equal("overriden_value", context1.GetValue("key1").AsString());
Assert.Equal("value2", context1.GetValue("key2").AsString());
Assert.Equal("overriden_value", context1.GetValue("key1").AsString);
Assert.Equal("value2", context1.GetValue("key2").AsString);

context1.Remove("key1");
Assert.Throws<KeyNotFoundException>(() => context1.GetValue("key1"));
Expand All @@ -63,28 +63,28 @@ public void EvaluationContext_Should_All_Types()
.Add("key6", 1.0);

var value1 = context.GetValue("key1");
value1.IsString().Should().BeTrue();
value1.AsString().Should().Be("value");
value1.IsString.Should().BeTrue();
value1.AsString.Should().Be("value");

var value2 = context.GetValue("key2");
value2.IsNumber().Should().BeTrue();
value2.AsInteger().Should().Be(1);
value2.IsNumber.Should().BeTrue();
value2.AsInteger.Should().Be(1);

var value3 = context.GetValue("key3");
value3.IsBoolean().Should().Be(true);
value3.AsBoolean().Should().Be(true);
value3.IsBoolean.Should().Be(true);
value3.AsBoolean.Should().Be(true);

var value4 = context.GetValue("key4");
value4.IsDateTime().Should().BeTrue();
value4.AsDateTime().Should().Be(now);
value4.IsDateTime.Should().BeTrue();
value4.AsDateTime.Should().Be(now);

var value5 = context.GetValue("key5");
value5.IsStructure().Should().BeTrue();
value5.AsStructure().Should().Equal(structure);
value5.IsStructure.Should().BeTrue();
value5.AsStructure.Should().Equal(structure);

var value6 = context.GetValue("key6");
value6.IsNumber().Should().BeTrue();
value6.AsDouble().Should().Be(1.0);
value6.IsNumber.Should().BeTrue();
value6.AsDouble.Should().Be(1.0);
}

[Fact]
Expand Down Expand Up @@ -112,7 +112,7 @@ public void Should_Be_Able_To_Get_All_Values()
var count = 0;
foreach (var keyValue in context)
{
context.GetValue(keyValue.Key).AsString().Should().Be(keyValue.Value.AsString());
context.GetValue(keyValue.Key).AsString.Should().Be(keyValue.Value.AsString);
count++;
}

Expand Down
16 changes: 8 additions & 8 deletions test/OpenFeature.SDK.Tests/OpenFeatureHookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public async Task Evaluation_Context_Must_Be_Mutable_Before_Hook()
new FlagEvaluationOptions(new[] { hook1.Object, hook2.Object }, new Dictionary<string, object>()));

hook1.Verify(x => x.Before(It.IsAny<HookContext<It.IsAnyType>>(), It.IsAny<Dictionary<string, object>>()), Times.Once);
hook2.Verify(x => x.Before(It.Is<HookContext<bool>>(a => a.EvaluationContext.GetValue("test").AsString() == "test"), It.IsAny<Dictionary<string, object>>()), Times.Once);
hook2.Verify(x => x.Before(It.Is<HookContext<bool>>(a => a.EvaluationContext.GetValue("test").AsString == "test"), It.IsAny<Dictionary<string, object>>()), Times.Once);
}

[Fact]
Expand Down Expand Up @@ -245,13 +245,13 @@ public async Task Evaluation_Context_Must_Be_Merged_In_Correct_Order()

// after proper merging, all properties should equal true
provider.Verify(x => x.ResolveBooleanValue(It.IsAny<string>(), It.IsAny<bool>(), It.Is<EvaluationContext>(y =>
(y.GetValue(propGlobal).AsBoolean() ?? false)
&& (y.GetValue(propClient).AsBoolean() ?? false)
&& (y.GetValue(propGlobalToOverwrite).AsBoolean() ?? false)
&& (y.GetValue(propInvocation).AsBoolean() ?? false)
&& (y.GetValue(propClientToOverwrite).AsBoolean() ?? false)
&& (y.GetValue(propHook).AsBoolean() ?? false)
&& (y.GetValue(propInvocationToOverwrite).AsBoolean() ?? false)
(y.GetValue(propGlobal).AsBoolean ?? false)
&& (y.GetValue(propClient).AsBoolean ?? false)
&& (y.GetValue(propGlobalToOverwrite).AsBoolean ?? false)
&& (y.GetValue(propInvocation).AsBoolean ?? false)
&& (y.GetValue(propClientToOverwrite).AsBoolean ?? false)
&& (y.GetValue(propHook).AsBoolean ?? false)
&& (y.GetValue(propInvocationToOverwrite).AsBoolean ?? false)
)), Times.Once);
}

Expand Down
22 changes: 11 additions & 11 deletions test/OpenFeature.SDK.Tests/StructureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Dictionary_Arg_Should_Contain_New_Dictionary()
{ KEY, new Value(KEY) }
};
Structure structure = new Structure(dictionary);
Assert.Equal(KEY, structure.AsDictionary()[KEY].AsString());
Assert.Equal(KEY, structure.AsDictionary()[KEY].AsString);
Assert.NotSame(structure.AsDictionary(), dictionary); // should be a copy
}

Expand Down Expand Up @@ -58,14 +58,14 @@ public void Add_And_Get_Add_And_Return_Values()
structure.Add(LIST_KEY, LIST_VAL);
structure.Add(VALUE_KEY, VALUE_VAL);

Assert.Equal(BOOL_VAL, structure.GetValue(BOOL_KEY).AsBoolean());
Assert.Equal(STRING_VAL, structure.GetValue(STRING_KEY).AsString());
Assert.Equal(INT_VAL, structure.GetValue(INT_KEY).AsInteger());
Assert.Equal(DOUBLE_VAL, structure.GetValue(DOUBLE_KEY).AsDouble());
Assert.Equal(DATE_VAL, structure.GetValue(DATE_KEY).AsDateTime());
Assert.Equal(STRUCT_VAL, structure.GetValue(STRUCT_KEY).AsStructure());
Assert.Equal(LIST_VAL, structure.GetValue(LIST_KEY).AsList());
Assert.True(structure.GetValue(VALUE_KEY).IsNull());
Assert.Equal(BOOL_VAL, structure.GetValue(BOOL_KEY).AsBoolean);
Assert.Equal(STRING_VAL, structure.GetValue(STRING_KEY).AsString);
Assert.Equal(INT_VAL, structure.GetValue(INT_KEY).AsInteger);
Assert.Equal(DOUBLE_VAL, structure.GetValue(DOUBLE_KEY).AsDouble);
Assert.Equal(DATE_VAL, structure.GetValue(DATE_KEY).AsDateTime);
Assert.Equal(STRUCT_VAL, structure.GetValue(STRUCT_KEY).AsStructure);
Assert.Equal(LIST_VAL, structure.GetValue(LIST_KEY).AsList);
Assert.True(structure.GetValue(VALUE_KEY).IsNull);
}

[Fact]
Expand All @@ -91,7 +91,7 @@ public void TryGetValue_Should_Return_Value()
structure.Add(KEY, VAL);
Value value;
Assert.True(structure.TryGetValue(KEY, out value));
Assert.Equal(VAL, value.AsString());
Assert.Equal(VAL, value.AsString);
}

[Fact]
Expand Down Expand Up @@ -127,7 +127,7 @@ public void GetEnumerator_Should_Return_Enumerator()
structure.Add(KEY, VAL);
IEnumerator<KeyValuePair<string, Value>> enumerator = structure.GetEnumerator();
enumerator.MoveNext();
Assert.Equal(VAL, enumerator.Current.Value.AsString());
Assert.Equal(VAL, enumerator.Current.Value.AsString);
}
}
}
Loading

0 comments on commit 9f8bdf5

Please sign in to comment.