/// <summary> /// Remove Value in Params Collection with given Key /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> /// <param name="values">Values Enumerable to be removed</param> /// <returns>True if ANY Value was removed</returns> public static bool RemoveParamValues <T>(this ICustomParamsValuable obj, string key, IEnumerable <T> values) { List <string> list; if (!obj.CustomParamsDictionary.TryGetValue(key, out list)) { list = new List <string>(); } bool removed = false; obj.ReplaceEntry(key, list.Where(val => { try { var item = (T)Convert.ChangeType(val, typeof(T)); if (values.Contains(item)) { removed = true; return(false); } return(true); } catch { return(true); } }).ToList()); return(removed); }
/// <summary> /// Search Params Collection to Extract the Values Collection identified by Key. /// From Dictionary<stringKey, List<stringValue>> (List<stringValue> Casted to List<T>) /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> /// <returns>Params Values collection or empty collection if not found</returns> public static IList <T> GetParamValues <T>(this ICustomParamsValuable obj, string key) { if (obj.CustomParamsDictionary == null || obj.CustomParamsDictionary.Count == 0) { return(new List <T>()); } List <string> list; // Is key existing ? if (obj.CustomParamsDictionary.TryGetValue(key, out list)) { return(list.Select(val => { try { return new Tuple <bool, T>(true, (T)Convert.ChangeType(val, typeof(T))); } catch { return new Tuple <bool, T>(false, default(T)); } }).Where(t => t.Item1).Select(t => t.Item2).ToList()); } return(new List <T>()); }
/// <summary> /// Parse Params String to Extract the Values identified by Key. /// Expected Format : {"key":["value"#, "value", ...#]#, "key2":[...], ...#} /// From Dictionary<stringKey, List<stringValue>> (List<stringValue> Casted to List<T>) /// </summary> /// <param name="key">Param Key</param> /// <returns>List of Values object</returns> public static IList <T> GetParamValues <T>(this ICustomParamsValuable obj, string key) { if (obj.CustomParamsDictionary == null || obj.CustomParamsDictionary.Count == 0 || Util.IsEmpty(key)) { return(new List <T>()); } var list = new List <T>(); // Is key existing ? if (obj.CustomParamsDictionary.ContainsKey(key) && obj.CustomParamsDictionary[key].Count > 0) { foreach (string val in obj.CustomParamsDictionary[key]) { T content; try { content = (T)Convert.ChangeType(val, typeof(T)); list.Add(content); } catch { } } } return(list); }
/// <summary> /// Parse Params String to Extract the Values identified by Key. /// Expected Format : {"key":["value"#, "value", ...#]#, "key2":[...], ...#} /// From Dictionary<stringKey, List<stringValue>> (List<stringValue> Casted to List<T>) /// </summary> /// <param name="key">Param Key</param> /// <returns>List of Values object</returns> public static IDictionary <K, V> GetParamValues <K, V>(this ICustomParamsValuable obj, string key) { if (obj.CustomParamsDictionary == null || obj.CustomParamsDictionary.Count == 0 || Util.IsEmpty(key)) { return(new Dictionary <K, V>()); } Regex regex = new Regex(string.Format(@"{0}\[(.+?)\]", key)); var dict = new Dictionary <K, V>(); // Browse all Dictionary foreach (var pair in obj.CustomParamsDictionary) { Match match = regex.Match(pair.Key); // this could be an entry if (match.Success) { K keyResult; V valueResult; try { keyResult = (K)Convert.ChangeType(match.Groups[1].Value, typeof(K)); valueResult = (V)Convert.ChangeType(pair.Value.FirstOrDefault(), typeof(V)); dict.Add(keyResult, valueResult); } catch { } } } return(dict); }
/// <summary> /// Init Object Params Collection from Source Collection /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="collection">Source Collection</param> /// <param name="key">Key Selector</param> /// <param name="value">Value Selector</param> public static void InitFromCollection <T>(this ICustomParamsValuable obj, IEnumerable <T> collection, Func <T, string> key, Func <T, string> value) { if (collection == null) { obj.CustomParamsDictionary = new Dictionary <string, List <string> >(); return; } obj.CustomParamsDictionary = collection.GroupBy(item => key(item)) .ToDictionary(grp => grp.Key, grp => grp.Select(item => value(item)).ToList()); }
/// <summary> /// Add Values in Params Collection with given Key /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> /// <param name="values">Values Enumerable</param> public static void AddParamValues <T>(this ICustomParamsValuable obj, string key, IEnumerable <T> values) { List <string> list; if (!obj.CustomParamsDictionary.TryGetValue(key, out list)) { list = new List <string>(); } obj.ReplaceEntry(key, list.Concat(values.Select(v => Convert.ToString(v))).ToList()); }
/// <summary> /// Add Value in Params Collection with given Key /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> /// <param name="value">Object Value</param> public static void AddParamValue <T>(this ICustomParamsValuable obj, string key, T value) { List <string> list; if (!obj.CustomParamsDictionary.TryGetValue(key, out list)) { list = new List <string>(); } obj.ReplaceEntry(key, list.Concat(new [] { Convert.ToString(value) }).ToList()); }
/// <summary> /// Parse Params String to Extract the Value identified by Key. /// Expected Format : {"key":["value"#, "value", ...#]#, "key2":[...], ...#} /// From Dictionary<stringKey, List<stringValue>> (List<stringValue>.First() Casted to T) /// </summary> /// <param name="key">Param Key</param> /// <returns>Param Value</returns> public static T GetParamValue <T>(this ICustomParamsValuable obj, string key) { if (obj.CustomParamsDictionary == null || obj.CustomParamsDictionary.Count == 0 || Util.IsEmpty(key)) { return(default(T)); } // Is key existing ? if (obj.CustomParamsDictionary.ContainsKey(key) && obj.CustomParamsDictionary[key].Count > 0) { try { return((T)Convert.ChangeType(obj.CustomParamsDictionary[key].First(), typeof(T))); } catch { return(default(T)); } } return(default(T)); }
/// <summary> /// Search Params Collection to Extract the Value identified by Key. /// From Dictionary<stringKey, List<stringValue>> (List<stringValue>.First() Casted to T) /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> /// <param name="def">Default Value</param> /// <returns>Param Value or Default if Key not found</returns> public static T GetParamValue <T>(this ICustomParamsValuable obj, string key, T def = default(T)) { if (obj.CustomParamsDictionary == null || obj.CustomParamsDictionary.Count == 0) { return(def); } // Is key existing ? List <string> list; if (obj.CustomParamsDictionary.TryGetValue(key, out list)) { try { return((T)Convert.ChangeType(obj.CustomParamsDictionary[key].First(), typeof(T))); } catch { return(def); } } return(def); }
/// <summary> /// Replace Params Collection Value at given Key /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> /// <param name="val">Values Enumerable to set</param> private static void ReplaceEntry(this ICustomParamsValuable obj, string key, List <string> val) { obj.CustomParamsDictionary = obj.CustomParamsDictionary.Select(kv => kv.Key == key ? new KeyValuePair <string, List <string> >(key, val) : kv) .ToDictionary(kv => kv.Key, kv => kv.Value); }
/// <summary> /// Remove all Values in Params Collection with given Key /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> public static void RemoveParam(this ICustomParamsValuable obj, string key) { obj.ReplaceEntry(key, new List <string>()); }
/// <summary> /// Set or Replace Values in Params Collection with given Key /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> /// <param name="values">Values Enumerable</param> public static void SetParamValues <T>(this ICustomParamsValuable obj, string key, IEnumerable <T> values) { obj.ReplaceEntry(key, values.Select(v => Convert.ToString(v)).ToList()); }
/// <summary> /// Set or Replace Value in Params Collection with given Key /// </summary> /// <param name="obj">Custom Params Object</param> /// <param name="key">Param Key</param> /// <param name="value">Object Value</param> public static void SetParamValue <T>(this ICustomParamsValuable obj, string key, T value) { obj.ReplaceEntry(key, new [] { Convert.ToString(value) }.ToList()); }