示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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);
        }
示例#4
0
        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);
        }