Exemplo n.º 1
0
        private static Rule ParseFieldRule(JObject json, RuleOptions options)
        {
            // parse operator
            string operatorStr = json[TokenName.Operator]?.ToString() ?? throw new ArgumentException("Missing 'operator'");

            operatorStr = Regex.Replace(operatorStr.ToLowerInvariant(), @"[^a-z]", "");
            Operator op = Enum.Parse <Operator>(operatorStr, true);

            // parse field type
            string    fieldTypeStr = json[TokenName.Type]?.ToString() ?? throw new ArgumentException("Missing 'type'");
            FieldType fieldType    = Enum.Parse <FieldType>(fieldTypeStr, true);

            FieldRule ruleObj = fieldType switch
            {
                FieldType.String => FieldRule.Create <string>(op),
                FieldType.Integer => FieldRule.Create <long>(op),
                FieldType.Double => FieldRule.Create <double>(op),
                FieldType.Boolean => FieldRule.Create <bool>(op),
                FieldType.DateTime => FieldRule.Create <DateTimeOffset>(op),
                FieldType.Date => FieldRule.Create <DateTime>(op),
                FieldType.Duration => FieldRule.Create <TimeSpan>(op),
                _ => throw new ArgumentOutOfRangeException()
            };

            ruleObj.Options = options;

            ruleObj.Field = json[TokenName.Field]?.ToString() ?? throw new ArgumentException("Missing 'field'");
            ruleObj.Value = json[TokenName.Value];

            return(ruleObj);
        }
    }
Exemplo n.º 2
0
        private static Rule ParseCondition(JObject json, JToken conditionToken, RuleOptions options)
        {
            // create condition
            var conditionObj = Condition.Create((string)conditionToken);

            conditionObj.Options = options;

            // process rules
            if (json.TryGetValue(TokenName.Rules, out JToken rulesToken) && rulesToken is JArray rulesArray)
            {
                foreach (JToken subRule in rulesArray)
                {
                    if (subRule is JObject subRuleObj)
                    {
                        conditionObj.Rules.Add(Rule.Parse(subRuleObj, options));
                    }
                }
            }

            // handle NOT
            if (json.TryGetValue(TokenName.Not, out JToken notToken) && notToken.Type == JTokenType.Boolean)
            {
                conditionObj.Not = notToken.Value <bool>();
            }

            return(conditionObj);
        }
Exemplo n.º 3
0
        public static Rule Parse(JObject json, RuleOptions options)
        {
            // condition (rule set)
            if (json.TryGetValue(TokenName.Condition, out JToken conditionToken))
            {
                return(ParseCondition(json, conditionToken, options));
            }

            return(ParseFieldRule(json, options));
        }
Exemplo n.º 4
0
            public HighlightWordsRule(XElement rule)
            {
                Words   = new List <string>();
                Options = new RuleOptions(rule);

                var wordsStr = rule.Element("Words").Value;
                var words    = Regex.Split(wordsStr, "\\s+");

                foreach (var word in words)
                {
                    if (!string.IsNullOrWhiteSpace(word))
                    {
                        Words.Add(word.Trim());
                    }
                }
            }
Exemplo n.º 5
0
        public static RuleOptions BuildOptions(string conf)
        {
            var optionStrings = conf.Trim('[', ']').Split(',').Select(o => o.Trim());

            RuleOptions options = RuleOptions.None;

            foreach (var o in optionStrings)
            {
                if (OptionLookup.ContainsKey(o))
                {
                    options |= OptionLookup[o];
                }
                else
                {
                    //conf.Dump();
                    throw new Exception("UNKNOWN OPTION: " + o);
                }
            }

            return(options);
        }
Exemplo n.º 6
0
 public AdvancedHighlightRule(XElement rule)
 {
     Expression = rule.Element("Expression").Value.Trim();
     Options    = new RuleOptions(rule);
 }
Exemplo n.º 7
0
 public HighlightLineRule(XElement rule)
 {
     LineStart = rule.Element("LineStart").Value.Trim();
     Options   = new RuleOptions(rule);
 }
Exemplo n.º 8
0
 public HighlightLineRule(XElement rule)
 {
     LineStart = rule.Element("LineStart").Value.Trim();
     Options = new RuleOptions(rule);
 }
Exemplo n.º 9
0
 public AdvancedHighlightRule(XElement rule)
 {
     Expression = rule.Element("Expression").Value.Trim();
     Options = new RuleOptions(rule);
 }
Exemplo n.º 10
0
            public HighlightWordsRule(XElement rule)
            {
                Words = new List<string>();
                Options = new RuleOptions(rule);

                string wordsStr = rule.Element("Words").Value;
                string[] words = Regex.Split(wordsStr, "\\s+");

                foreach (string word in words)
                  if (!string.IsNullOrWhiteSpace(word))
                Words.Add(word.Trim());
            }
 /// <summary>
 /// Creates a new expression tree serializer instance, with the specified configuration parameters.
 /// Additional rules may be added using the Rules property.
 /// </summary>
 /// <param name="options">Rule set configuration flags.</param>
 /// <param name="typeResolutionService">Type resolution service. Can be null.</param>
 public ExpressionJsonSerializer(RuleOptions options, ITypeResolutionService typeResolutionService)
     : this(RuleConfiguration.Get(options), typeResolutionService)
 {
 }
Exemplo n.º 12
0
        /// <summary>
        /// Constructs a rule table with the specified configuration optiosn.
        /// </summary>
        /// <param name="options">Configuration options for the resulting rule table.</param>
        /// <returns>Rule table with the specified configuration options.</returns>
        private static RuleTableBase <object, Json.Expression, ExpressionJsonSerializationContext> CreateRuleTable(RuleOptions options)
        {
            var baseTable = ExpressionTreeRuleTable.Instance;

            var res = baseTable;

            if ((options & RuleOptions.CaptureClosures) != RuleOptions.None)
            {
                res = res.Concat(ClosureRuleTable.Instance);
            }

            if ((options & RuleOptions.CSharpDynamic) != RuleOptions.None)
            {
                res = res.Concat(CSharpDynamicRuleTable.Instance);
            }

            if ((options & RuleOptions.StatementTrees) != RuleOptions.None)
            {
                res = res.Concat(StatementTreeRuleTable.Instance);
            }

            if ((options & RuleOptions.ReadOnly) != RuleOptions.None)
            {
                res = res.AsReadOnly();
            }
            else
            {
                res = res.Extend();
            }

            return(res);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Gets a rule table with the specified configuration optiosn.
 /// </summary>
 /// <param name="options">Configuration options for the resulting rule table.</param>
 /// <returns>Rule table with the specified configuration options.</returns>
 public static RuleTableBase <object, Json.Expression, ExpressionJsonSerializationContext> Get(RuleOptions options)
 {
     return(CreateRuleTable(options));
 }
Exemplo n.º 14
0
 public AdvancedHighlightRule(XElement rule)
 {
     Expression = rule.Element("Expression").Value.Trim();
     HighlightExpressionIndex = int.Parse(rule.Element("HighlightExpressionIndex").Value.Trim());
     Options = new RuleOptions(rule);
 }