Skip to content

Commit

Permalink
Merge pull request #5 from launchdarkly/dr/fixNumberComparison
Browse files Browse the repository at this point in the history
Fix number comparison when evaluating rules
  • Loading branch information
drichelson committed May 17, 2016
2 parents 6b493b4 + f7a6331 commit 867eed8
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 19 deletions.
11 changes: 5 additions & 6 deletions src/LaunchDarkly.Client/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public bool Evaluate(User user, bool defaultValue)
if (Variations.Any(v => v.MatchesUserTarget(user)))
return Variations.First(v => v.MatchesUserTarget(user)).Value;

if(Variations.Any(v=>v.Matches(user)))
if (Variations.Any(v => v.Matches(user)))
return Variations.First(v => v.Matches(user)).Value;

var param = user.GetParam(Key, Salt);
Expand Down Expand Up @@ -65,10 +65,10 @@ public bool Matches(User user)
{
return Targets.Any(t => t.Matches(user));
}

public bool MatchesUserTarget(User user)
{
if (UserTarget != null && UserTarget.Matches(user))
if (UserTarget != null && UserTarget.Matches(user))
{
return true;
}
Expand Down Expand Up @@ -98,7 +98,6 @@ public bool Matches(User user)
var uvs = (IEnumerable<object>)userValue;
return Values.Intersect<object>(uvs).Any();
}

return Values.Contains(userValue);
}

Expand Down Expand Up @@ -129,7 +128,7 @@ private Object GetUserValue(User user)
if (token.Type == Newtonsoft.Json.Linq.JTokenType.Array)
{
var arr = (JArray)token;
return arr.Values<JToken>().Select(i => ((JValue)i).Value);
return arr.Values<JToken>().Select(i => ((JValue)i).Value);
}
else if (token.Type == JTokenType.Object)
{
Expand All @@ -138,7 +137,7 @@ private Object GetUserValue(User user)
else
{
var val = (JValue)token;
return val.Value;
return val.Value;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/LaunchDarkly.Client/LaunchDarkly.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
Expand Down
6 changes: 3 additions & 3 deletions src/LaunchDarkly.Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyVersion("1.0.1")]
[assembly: AssemblyFileVersion("1.0.1")]
12 changes: 11 additions & 1 deletion src/LaunchDarkly.Client/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static User AndCustomAttribute(this User user, string attribute, int valu
if (attribute == string.Empty)
throw new ArgumentException("Attribute Name can not be empty");

user.Custom.Add(attribute, new JValue(value));
user.Custom.Add(attribute, new JValue((double)value));

return user;
}
Expand All @@ -175,5 +175,15 @@ public static User AndCustomAttribute(this User user, string attribute, List<str

return user;
}

public static User AndCustomAttribute(this User user, string attribute, List<int> value)
{
if (attribute == string.Empty)
throw new ArgumentException("Attribute Name can not be empty");

user.Custom.Add(attribute, new JArray(value.ToArray()));

return user;
}
}
}
2 changes: 1 addition & 1 deletion src/LaunchDarkly.Client/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
Expand Down
2 changes: 1 addition & 1 deletion src/LaunchDarkly.Client/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="NuGet.CommandLine" version="3.4.3" targetFramework="net45" developmentDependency="true" />
</packages>
40 changes: 40 additions & 0 deletions src/LaunchDarkly.Tests/FeatureTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LaunchDarkly.Client;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using RichardSzalay.MockHttp;
using System.Collections.Generic;
Expand Down Expand Up @@ -84,6 +85,45 @@ public void IfAUserHasCustomListAttributes_TargetRulesMatch()
Assert.AreEqual(true, target.Matches(user));
}

[Test]
public void IfAUserHasCustomAttributeAsFloat_TargetRulesMatch()
{
var user = User.WithKey("anyUser").AndCustomAttribute("numbers", 1362823F);
var target = new TargetRule();

target.Attribute = "numbers";
target.Op = "in";
target.Values = new List<object>() { 1362823F };

Assert.AreEqual(true, target.Matches(user));
}

[Test]
public void IfAUserHasCustomAttributeAsInteger_TargetRulesMatch()
{
var user = User.WithKey("anyUser").AndCustomAttribute("numbers", 1362823);
var target = new TargetRule();

target.Attribute = "numbers";
target.Op = "in";
target.Values = new List<object>() { 1362823.0 };

Assert.AreEqual(true, target.Matches(user));
}

[Test]
public void IfAUserHasCustomListAttributeAsIntegers_TargetRulesMatch()
{
var user = User.WithKey("anyUser").AndCustomAttribute("numbers", new List<int>() { 1362823 });
var target = new TargetRule();

target.Attribute = "numbers";
target.Op = "in";
target.Values = new List<object>() { 55, 1362823 };

Assert.AreEqual(true, target.Matches(user));
}

[Test]
public void IfAUserHasNonMatchingCustomListAttributes_TargetRulesDoNotMatch()
{
Expand Down
4 changes: 2 additions & 2 deletions src/LaunchDarkly.Tests/LaunchDarkly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
Expand Down
2 changes: 1 addition & 1 deletion src/LaunchDarkly.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
Expand Down
2 changes: 1 addition & 1 deletion src/LaunchDarkly.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net451" userInstalled="true" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net451" userInstalled="true" />
<package id="Moq" version="4.2.1510.2205" targetFramework="net451" userInstalled="true" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" userInstalled="true" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net451" userInstalled="true" />
<package id="NLog" version="4.3.1" targetFramework="net451" userInstalled="true" />
<package id="NLog.Config" version="4.3.1" targetFramework="net451" userInstalled="true" />
<package id="NLog.Schema" version="4.3.0" targetFramework="net451" userInstalled="true" />
Expand Down

0 comments on commit 867eed8

Please sign in to comment.