Skip to content

Commit

Permalink
fix: Use Utf8Parser to convert double and int variant values
Browse files Browse the repository at this point in the history
Signed-off-by: Rogov Dmitry <[email protected]>
  • Loading branch information
dmitryrogov committed Apr 10, 2024
1 parent 8657222 commit b9fb281
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
49 changes: 43 additions & 6 deletions src/OpenFeature.Contrib.Providers.Flipt/AttachmentParser.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
using OpenFeature.Error;
using OpenFeature.Model;
using System.Buffers.Text;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;

namespace OpenFeature.Contrib.Providers.Flipt
{
internal static class AttachmentParser
{
/// <summary>
/// Try parse string attachment
/// Converts the JSON string representation of a number to its double-precision
/// floating-point number equivalent.
/// </summary>
/// <param name="attachment">Attachment</param>
/// <param name="value">Result value</param>
/// <returns><c>true</c> if parsed successfully; otherwise <c>false</c>;</returns>
/// <param name="attachment">Attachment.</param>
/// <param name="value">Double-precision floating-point number result.</param>
/// <returns>true if attachment was converted successfully; otherwise false.</returns>
public static bool TryParseDouble(string attachment, out double value)
{
if (string.IsNullOrEmpty(attachment))
{
value = default;
return false;
}

return Utf8Parser.TryParse(Encoding.UTF8.GetBytes(attachment), out value, out int _); ;
}

/// <summary>
/// Converts the JSON string representation of a number to its 32-bit signed integer equivalent.
/// </summary>
/// <param name="attachment">Attachment.</param>
/// <param name="value">32-bit signed integer result.</param>
/// <returns>true if attachment was converted successfully; otherwise false.</returns>
public static bool TryParseInteger(string attachment, out int value)
{
if (string.IsNullOrEmpty(attachment))
{
value = default;
return false;
}

return Utf8Parser.TryParse(Encoding.UTF8.GetBytes(attachment), out value, out int _);
}

/// <summary>
/// Converts the JSON string.
/// </summary>
/// <param name="attachment">Attachment.</param>
/// <param name="value">String result.</param>
/// <returns>true if attachment was converted successfully; otherwise false.</returns>
public static bool TryParseString(string attachment, out string value)
{
if (string.IsNullOrEmpty(attachment))
{
value = null;
value = default;
return false;
}

Expand All @@ -32,7 +69,7 @@ public static bool TryParseString(string attachment, out string value)
/// </summary>
/// <param name="attachment">JSON string attachment.</param>
/// <param name="value">Value result.</param>
/// <returns></returns>
/// <returns>true if attachment was converted successfully; otherwise false.</returns>
public static bool TryParseJsonValue(string attachment, out Value value)
{
value = null;
Expand Down
6 changes: 3 additions & 3 deletions src/OpenFeature.Contrib.Providers.Flipt/FliptProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace OpenFeature.Contrib.Providers.Flipt
{
/// <summary>
/// Flipt feature provider
/// Flipt feature provider.
/// </summary>
public sealed class FliptProvider : FeatureProvider
{
Expand Down Expand Up @@ -61,14 +61,14 @@ public override Task<ResolutionDetails<bool>> ResolveBooleanValue(string flagKey
public override Task<ResolutionDetails<double>> ResolveDoubleValue(string flagKey, double defaultValue,
EvaluationContext context = null)
{
return ResolveVariantAsync(flagKey, defaultValue, context, double.TryParse);
return ResolveVariantAsync(flagKey, defaultValue, context, AttachmentParser.TryParseDouble);
}

/// <inheritdoc />
public override Task<ResolutionDetails<int>> ResolveIntegerValue(string flagKey, int defaultValue,
EvaluationContext context = null)
{
return ResolveVariantAsync(flagKey, defaultValue, context, int.TryParse);
return ResolveVariantAsync(flagKey, defaultValue, context, AttachmentParser.TryParseInteger);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ public void TryParseString_ShouldBeExpectedResult(string attachment, string expe
value.Should().Be(expectedValue);
}

[Theory]
[InlineData("", 0, false)]
[InlineData("123.456", 123.456, true)]
[InlineData("-123.456", -123.456, true)]
[InlineData(".3", 0.3, true)]
[InlineData("1.2345E+2", 123.45, true)]
public void TryParseDouble_ShouldBeExpectedResult(string attachment, double expectedValue, bool expectedResult)
{
// Act
var result = AttachmentParser.TryParseDouble(attachment, out var value);

// Assert
result.Should().Be(expectedResult);
value.Should().Be(expectedValue);
}

[Theory]
[InlineData("", 0, false)]
[InlineData("-456", -456, true)]
[InlineData("-123", -123.456, true)]
[InlineData("0", 0, true)]
public void TryParseInteger_ShouldBeExpectedResult(string attachment, int expectedValue, bool expectedResult)
{
// Act
var result = AttachmentParser.TryParseInteger(attachment, out var value);

// Assert
result.Should().Be(expectedResult);
value.Should().Be(expectedValue);
}

[Theory]
[MemberData(nameof(DateTimeData))]
public void TryParseJsonValue_DateTimeValue_ShouldReturnTrue(string attachment, DateTime expectedValue)
Expand All @@ -58,7 +89,7 @@ public void TryParseJsonValue_DateTimeValue_ShouldReturnTrue(string attachment,
public void TryParseJsonValue_DoubleValue_ShouldReturnTrue()
{
// Arrange
var value = _fixture.Create<double>();
var value = 123.4;
var attachment = JsonSerializer.Serialize(value);

// Act
Expand Down

0 comments on commit b9fb281

Please sign in to comment.