/// <summary> /// Returns a <see cref="string" /> that represents the given dictionary object to be stored as a string /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="dictionary">The dictionary.</param> /// <param name="info">The information.</param> /// <param name="getKeyString">The get key string.</param> /// <param name="getValueString">The get value string.</param> /// <returns> /// A <see cref="string" /> that represents the given dictionary object to be stored as a string /// </returns> public static string ToString <TKey, TValue>(Dictionary <TKey, HashSet <TValue> > dictionary, ConfigKeyValuesSplitInfo info = null, Func <TKey, string> getKeyString = null, Func <TValue, string> getValueString = null) { info = info ?? ConfigKeyValuesSplitInfo.Default; return(string.Join(info.EntrySeparators.First().ToString(), ToStringDictionaryHashSet(dictionary, info, getKeyString, getValueString))); }
/// <summary> /// Returns a <see cref="string" /> that represents the given dictionary object to be stored as a string /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="dictionary">The dictionary.</param> /// <param name="info">The information.</param> /// <param name="getKeyString">The get key string.</param> /// <param name="getValueString">The get value string.</param> /// <param name="sort">The sort function to apply</param> /// <returns> /// A <see cref="string" /> that represents the given dictionary object to be stored as a string /// </returns> public static string ToStringSorted <TKey, TValue>(Dictionary <TKey, HashSet <TValue> > dictionary, ConfigKeyValuesSplitInfo info = null, Func <TKey, string> getKeyString = null, Func <TValue, string> getValueString = null, Func <IEnumerable <string>, IOrderedEnumerable <string> > sort = null) { info = info ?? ConfigKeyValuesSplitInfo.Default; sort = sort ?? DefaultSort(); return(string.Join(info.EntrySeparators.First().ToString(), sort(ToStringDictionaryHashSet(dictionary, info, getKeyString, getValueString)))); }
/// <summary> /// Returns a <see cref="string" /> that represents the given dictionary object to be stored as a string /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="dictionary">The dictionary.</param> /// <param name="info">The information.</param> /// <param name="getKeyString">The get key string.</param> /// <param name="getValueString">The get value string.</param> /// <returns> /// A <see cref="string" /> that represents the given dictionary object to be stored as a string /// </returns> public static string ToString <TKey, TValue>(Dictionary <TKey, HashSet <TValue> > dictionary, ConfigKeyValuesSplitInfo info = null, Func <TKey, string> getKeyString = null, Func <TValue, string> getValueString = null) { info = info ?? ConfigKeyValuesSplitInfo.Default; getKeyString = getKeyString ?? ToStringDefault; getValueString = getValueString ?? ToStringDefault; var values = (from kvp in dictionary let key = getKeyString(kvp.Key) let prefix = (info.ConvertKeysToLower ? key.ToLower() : key) + info.KeyValueSeperators.First() let items = kvp.Value.Select(v => getValueString(v)).Select(v => info.ConvertValuesToLower ? v.ToLower() : v) select prefix + string.Join(info.EntryValuesSeparators.First().ToString(), items)).ToList(); return(string.Join(info.EntrySeparators.First().ToString(), values)); }
/// <summary> /// Attempts to read the setting from the config file, and Parse into a Dictionary. /// If the Type doesn't contain a Parse, a cast is attempted. /// Any failure in the Parse will throw an exception. /// If the config value is null, then the default value will be used. /// The default setting should be in the format {Key}:{Value1},{value2}|{Key}:{Value1} /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="appSetting">The application setting.</param> /// <param name="getDefault">Function to get the default value.</param> /// <param name="info">The settings by which to split the config value.</param> /// <returns></returns> public static Dictionary <TKey, HashSet <TValue> > GetDictionaryHash <TKey, TValue>(string appSetting, Func <Dictionary <TKey, HashSet <TValue> > > getDefault, ConfigKeyValuesSplitInfo info = null) { try { var config = ConfigProvider.Instance[appSetting]; return(config == null?getDefault() : config.GetDictionaryHash <TKey, TValue>(info)); } catch (Exception ex) { throw new FormatException($"Error occured parsing dictionary Hash for app setting \"{appSetting}\"", ex); } }
/// <summary> /// Attempts to read the setting from the config file, and Parse into a Dictionary. /// If the Type doesn't contain a Parse, a cast is attempted. /// Any failure in the Parse will throw an exception. /// If the config value is null, then the default value will be used. /// The default setting should be in the format {Key}:{Value1},{value2}|{Key}:{Value1} /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="appSetting">The application setting.</param> /// <param name="defaultValue">The default value.</param> /// <param name="info">The settings by which to split the config value.</param> /// <returns></returns> public static Dictionary <TKey, List <TValue> > GetDictionaryList <TKey, TValue>(string appSetting, string defaultValue, ConfigKeyValuesSplitInfo info = null) { try { var config = ConfigProvider.Instance[appSetting] ?? defaultValue; return(config.GetDictionaryList <TKey, TValue>(info)); } catch (Exception ex) { throw new FormatException($"Error occured parsing dictionary list for app setting \"{appSetting}\", with default value \"{defaultValue}\"", ex); } }
private static Dictionary <TKey, HashSet <TValue> > GetDictionaryHash <TKey, TValue>(ConfigKeyValuesSplitInfo info, string config) { info = info ?? ConfigKeyValuesSplitInfo.Default; var dict = new Dictionary <TKey, HashSet <TValue> >(); foreach (var entry in config.Split(info.EntrySeperators, StringSplitOptions.RemoveEmptyEntries)) { var entryValues = entry.Split(info.KeyValueSeperators, StringSplitOptions.RemoveEmptyEntries); var value = entryValues.Length > 1 ? new HashSet <TValue>(entryValues[1].Split(info.EntryValuesSeperators, StringSplitOptions.RemoveEmptyEntries).Select(info.ParseValue <TValue>)) : new HashSet <TValue>(); dict.Add(info.ParseKey <TKey>(entryValues[0]), value); } return(dict); }
private static List <string> ToStringDictionaryList <TKey, TValue>(Dictionary <TKey, List <TValue> > dictionary, ConfigKeyValuesSplitInfo info, Func <TKey, string> getKeyString, Func <TValue, string> getValueString) { getKeyString = getKeyString ?? ToStringDefault; getValueString = getValueString ?? ToStringDefault; var values = (from kvp in dictionary let key = getKeyString(kvp.Key) let prefix = (info.ConvertKeysToLower ? key.ToLower() : key) + info.KeyValueSeperators.First() let items = (kvp.Value ?? new List <TValue>()).Select(v => getValueString(v)).Select(v => info.ConvertValuesToLower ? v.ToLower() : v) select prefix + string.Join(info.EntryValuesSeparators.First().ToString(), items)).ToList(); return(values); }