99 lines
3.7 KiB
C#
Executable File
99 lines
3.7 KiB
C#
Executable File
using System.Collections.Generic;
|
|
|
|
namespace AnP.Utils{
|
|
public class Common{
|
|
|
|
public static readonly Options GET_DICTIONARY_OPTIONS = new Options(Options.NO_OVERWRITE);
|
|
public static readonly Options GET_OPTIONS = new Options(Options.ALLOW_NULLS);
|
|
|
|
public static List<string> get_strings(object? items){
|
|
|
|
List<string> strings = new List<string>();
|
|
|
|
if(items != null){
|
|
if(Check.is_string(items))
|
|
strings.Add((string)items);
|
|
else if(Check.is_array<object>(items))
|
|
foreach(object item in (IEnumerable<object>)items)
|
|
if(Check.is_string(item))
|
|
strings.Add((string)item);
|
|
}
|
|
|
|
return strings;
|
|
}
|
|
|
|
public static List<string> get_keys(object? items){
|
|
|
|
List<string> keys = new List<string>();
|
|
|
|
if(items != null){
|
|
if(Check.is_key(items))
|
|
keys.Add((string)items);
|
|
else if(Check.is_array<object>(items))
|
|
foreach(object item in (IEnumerable<object>)items)
|
|
if(Check.is_key(item)){
|
|
|
|
string key = (string)item;
|
|
|
|
if(!keys.Contains(key))
|
|
keys.Add(key);
|
|
|
|
};
|
|
}
|
|
|
|
return keys;
|
|
}
|
|
|
|
public static List<IDictionary<string, T>> get_dictionaries<T>(object? items){
|
|
|
|
List<IDictionary<string, T>> dictionaries = new List<IDictionary<string, T>>();
|
|
|
|
if(items != null){
|
|
if(Check.is_dictionary<T>(items))
|
|
dictionaries.Add((Dictionary<string, T>)items);
|
|
else if(Check.is_array<object>(items))
|
|
foreach(object item in (IEnumerable<object>)items)
|
|
if(Check.is_dictionary<T>(item))
|
|
dictionaries.Add((IDictionary<string, T>)item);
|
|
}
|
|
|
|
return dictionaries;
|
|
}
|
|
|
|
public static Dictionary<string, T> get_dictionary<T>(object? items, int custom_options = 0){
|
|
|
|
Dictionary<string, T> dictionary = new Dictionary<string, T>();
|
|
Options options = new Options(custom_options, GET_DICTIONARY_OPTIONS);
|
|
|
|
if(items != null){
|
|
if(Check.is_dictionary<T>(items))
|
|
dictionary = (Dictionary<string, T>)items;
|
|
else if(Check.is_array<object>(items))
|
|
foreach(object item in (IEnumerable<object>)items)
|
|
if(Check.is_dictionary<T>(item))
|
|
foreach(KeyValuePair<string, T> pair in (IDictionary<string, T>)item)
|
|
if(options.overwrite == true || !dictionary.ContainsKey(pair.Key))
|
|
dictionary[pair.Key] = pair.Value;
|
|
}
|
|
|
|
return dictionary;
|
|
}
|
|
|
|
public static T? get<T>(object keys, object inputs, T? _default = default(T?), int custom_options = 0){
|
|
|
|
Options options = new Options(custom_options, GET_OPTIONS);
|
|
List<string> keys_list = get_keys(keys);
|
|
|
|
if(keys_list.Count != 0)
|
|
foreach(IDictionary<string, T> subinputs in get_dictionaries<T>(inputs))
|
|
foreach(string key in keys_list)
|
|
if(subinputs.ContainsKey(key) && (
|
|
options.allow_null == true ||
|
|
subinputs[key] != null
|
|
))
|
|
return subinputs[key];
|
|
return _default;
|
|
}
|
|
|
|
}
|
|
} |