From 27e9ade4cb324720d62f45ccf145f2bf737a2302 Mon Sep 17 00:00:00 2001 From: Adam Simon Date: Fri, 15 Sep 2023 11:22:46 +0200 Subject: [PATCH] Add helper methods for converting numeric/datetime/string array values to User Object attribute values --- src/ConfigCatClient/User.cs | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/ConfigCatClient/User.cs b/src/ConfigCatClient/User.cs index 603f15cf..9cd2fc8e 100644 --- a/src/ConfigCatClient/User.cs +++ b/src/ConfigCatClient/User.cs @@ -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; @@ -13,6 +17,48 @@ namespace ConfigCat.Client; /// public class User { + /// + /// Converts the specified value to the format expected by UTC datetime comparison operators. + /// + /// The value to convert. + /// The User Object attribute value in the expected format. + public static string AttributeValueFrom(DateTime dateTime) + { + var unixTimeSeconds = DateTimeUtils.ToUnixTimeMilliseconds(dateTime) / 1000.0; + return unixTimeSeconds.ToString("0.###", CultureInfo.InvariantCulture); + } + + /// + /// Converts the specified value to the format expected by number comparison operators. + /// + /// The value to convert. + /// The User Object attribute value in the expected format. + public static string AttributeValueFrom(double number) + { + return number.ToString("g", CultureInfo.InvariantCulture); // format "g" allows scientific notation as well + } + + /// + /// Converts the specified items to the format expected by array comparison operators. + /// + /// The items to convert. + /// The User Object attribute value in the expected format. + public static string AttributeValueFrom(params string[] items) + { + return AttributeValueFrom(items.AsEnumerable()); + } + + /// + /// Converts the specified items to the format expected by array comparison operators. + /// + /// The items to convert. + /// The User Object attribute value in the expected format. + public static string AttributeValueFrom(IEnumerable items) + { + // TODO: escape the separator character in items? + return string.Join(",", items ?? throw new ArgumentNullException("items")); + } + internal const string DefaultIdentifierValue = ""; ///