private Option <ILiquidValue> FromObject(Object obj) { var newHash = new LiquidHash(); var kvps = obj.GetType() .GetProperties() .Where(property => !(property.GetCustomAttributes <LiquidIgnoreAttribute>().Any() || (property.GetCustomAttributes <LiquidIgnoreIfNullAttribute>().Any() && ReferenceEquals(property.GetGetMethod().Invoke(obj, null), null)))) .Select(property => new KeyValuePair <String, Option <ILiquidValue> > ( GetPropertyName(property), GetPropertyValue(obj, property))); foreach (var kvp in kvps) { // tolower may create a collision if (newHash.ContainsKey(kvp.Key)) { newHash[kvp.Key] = kvp.Value; } else { newHash.Add(kvp); } } return(newHash); }
public void It_Should_Add_A_Value() { var dict = new LiquidHash(); dict.Add("key", LiquidString.Create("test")); Assert.Equal(LiquidString.Create("test"), dict["key"].Value); }
public static Option <ILiquidValue> Transform(JObject obj) { var result = new LiquidHash(); var dict = obj.Properties().ToDictionary(k => k.Name, v => (Option <ILiquidValue>)Transform((dynamic)v.Value)); foreach (var kvp in dict) { result.Add(kvp); // result.Add(kvp.Key,kvp.Value != null? // (Option<ILiquidValue>) new Some<ILiquidValue>(kvp.Value) : // new None<ILiquidValue>()); } return(result); }
private Option <ILiquidValue> CreateHash(IDictionary dict) { var newHash = new LiquidHash(); foreach (var key in dict.Keys) { String keyAsStr = key.ToString(); var newValue = Convert(dict[key]); // may throw exception if (newHash.ContainsKey(keyAsStr)) { newHash[keyAsStr] = newValue; } else { newHash.Add(keyAsStr, newValue); } } return(newHash); }