Skip to content

Commit

Permalink
Add helper methods for converting numeric/datetime/string array value…
Browse files Browse the repository at this point in the history
…s to User Object attribute values
  • Loading branch information
adams85 committed Sep 15, 2023
1 parent 6d387f4 commit 27e9ade
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/ConfigCatClient/User.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using ConfigCat.Client.Utils;
using System.Linq;

#if USE_NEWTONSOFT_JSON
using Newtonsoft.Json;
Expand All @@ -13,6 +17,48 @@ namespace ConfigCat.Client;
/// </summary>
public class User
{
/// <summary>
/// Converts the specified <see cref="DateTime"/> value to the format expected by UTC datetime comparison operators.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime"/> value to convert.</param>
/// <returns>The User Object attribute value in the expected format.</returns>
public static string AttributeValueFrom(DateTime dateTime)
{
var unixTimeSeconds = DateTimeUtils.ToUnixTimeMilliseconds(dateTime) / 1000.0;
return unixTimeSeconds.ToString("0.###", CultureInfo.InvariantCulture);
}

/// <summary>
/// Converts the specified <see cref="double"/> value to the format expected by number comparison operators.
/// </summary>
/// <param name="number">The <see cref="double"/> value to convert.</param>
/// <returns>The User Object attribute value in the expected format.</returns>
public static string AttributeValueFrom(double number)
{
return number.ToString("g", CultureInfo.InvariantCulture); // format "g" allows scientific notation as well
}

/// <summary>
/// Converts the specified <see cref="string"/> items to the format expected by array comparison operators.
/// </summary>
/// <param name="items">The <see cref="string"/> items to convert.</param>
/// <returns>The User Object attribute value in the expected format.</returns>
public static string AttributeValueFrom(params string[] items)
{
return AttributeValueFrom(items.AsEnumerable());
}

/// <summary>
/// Converts the specified <see cref="string"/> items to the format expected by array comparison operators.
/// </summary>
/// <param name="items">The <see cref="string"/> items to convert.</param>
/// <returns>The User Object attribute value in the expected format.</returns>
public static string AttributeValueFrom(IEnumerable<string> items)
{
// TODO: escape the separator character in items?
return string.Join(",", items ?? throw new ArgumentNullException("items"));
}

internal const string DefaultIdentifierValue = "";

/// <summary>
Expand Down

0 comments on commit 27e9ade

Please sign in to comment.