Example #1
0
        public static bool EvalulateCondition(TemplatorParser parser, IDictionary <string, object> input, TextHolder holder, string condition, object eav)
        {
            var not = false;

            if (condition != null && condition.StartsWith("!"))
            {
                not       = true;
                condition = condition.Substring(1, condition.Length - 1);
            }
            if (!condition.IsNullOrEmptyValue())
            {
                eav = GetInputValue(parser, condition, input);
                eav = eav ?? parser.RequireValue(parser, GetHolder(input, condition, parser.Config));
            }
            var hasValue = eav == null || (parser.Config.EmptyAsNulls && eav.IsNullOrEmptyValue());

            return(not == hasValue);
        }
Example #2
0
        public static object GetValue(TemplatorParser parser, TextHolder holder, IDictionary <string, object> input, object defaultRet)
        {
            object value = null;

            if (input != null && input.ContainsKey(holder.Name))
            {
                value = input[holder.Name];
            }
            if (value == null)
            {
                value = holder.Keywords.EmptyIfNull().Where(k => k.CalculateInput && k.OnGetValue != null)
                        .Aggregate((object)null, (current, k) => KeywordPostParse(parser, holder, current, k));
            }
            value = value ?? parser.RequireValue(parser, holder, defaultRet, input);
            value = holder.Keywords.EmptyIfNull().Where(key => !key.CalculateInput && key.OnGetValue != null)
                    .Aggregate(value, (current, k) => KeywordPostParse(parser, holder, current, k));
            if (parser.Context.ChildResultBefore.Length > 0 || parser.Context.ChildResultAfter.Length > 0)
            {
                value = String.Concat(parser.Context.ChildResultBefore, value, parser.Context.ChildResultAfter);
            }
            var logger = parser.Context.ChildLogger as TemplatorLogger;

            if (logger != null)
            {
                foreach (var en in logger.Errors)
                {
                    parser.Context.Logger?.LogError(en.FileName, en.Line, en.Column, en.EndLineNumber, en.EndColumnNumber, en.Message);
                }
            }
            if (null == value)
            {
                if (defaultRet != null)
                {
                    value = defaultRet;
                }
                else
                {
                    parser.LogError("'{0}' is required", holder.Name);
                }
            }
            return(value);
        }
Example #3
0
        public static object GetUnformmatedValue(TemplatorParser parser, string fieldName, IDictionary <string, object> input, int seekUp = 0, bool requireInput = true)
        {
            var value     = GetInputValue(parser, fieldName, input, null, seekUp);
            var subHolder = GetHolder(input, fieldName, parser.Config, false);

            if (value == null)
            {
                if (subHolder != null)
                {
                    value = subHolder.Keywords.EmptyIfNull().Where(k => k.CalculateInput && k.OnGetValue != null)
                            .Aggregate((object)null, (current, k) => KeywordPostParse(parser, subHolder, current, k));
                }
                if (requireInput)
                {
                    value = value ?? parser.RequireValue(parser, subHolder ?? new TextHolder(fieldName), null, input);
                }
            }
            if (subHolder != null)
            {
                value = subHolder.Keywords.EmptyIfNull().Where(k => k.ManipulateInput && k.OnGetValue != null)
                        .Aggregate(value, (current, k) => KeywordPostParse(parser, subHolder, current, k));
            }
            return(value);
        }