private bool IsFiltered(FiltrCompositeSpec spec, JToken input)
 {
     if (!spec.Filters.Any())
     {
         return(false);
     }
     // apply any filters to the object
     foreach (var filter in spec.Filters)
     {
         if (filter.Matches(input))
         {
             return(false);
         }
     }
     return(true);
 }
Example #2
0
        public Filtr(JToken spec)
        {
            if (spec == null)
            {
                throw new SpecException("Filtr expected a spec of Map type, got 'null'.");
            }
            if (!(spec is JObject dic))
            {
                throw new SpecException("Filtr expected a spec of Map type, got " + spec.GetType().Name);
            }

            _rootSpec = new FiltrCompositeSpec(null);
            foreach (var kv in dic)
            {
                List <string> pathStrs   = SpecStringParser.ParseDotNotation(new List <string>(), kv.Key.GetEnumerator(), kv.Key);
                var           parentSpec = _rootSpec;
                foreach (var pathStr in pathStrs)
                {
                    var pathElement = RemovrSpec.Parse(pathStr);
                    if (!parentSpec.Children.TryGetValue(pathElement.RawKey, out var childSpec))
                    {
                        childSpec = new FiltrCompositeSpec(pathElement);
                        parentSpec.Children.Add(pathElement.RawKey, childSpec);
                    }
                    parentSpec = childSpec;
                }
                if (!(kv.Value is JObject filterSpec))
                {
                    throw new SpecException($"Filtr object filter specification must be a JSON object (found {kv.Value.Type})");
                }
                var propFilters = new List <KeyValuePair <string, JToken> >();
                foreach (var filterKv in filterSpec)
                {
                    if (filterKv.Value.Type == JTokenType.Array || filterKv.Value.Type == JTokenType.Object)
                    {
                        throw new SpecException($"Filter object filter specification must be a simple JSON value (string, number or null)");
                    }
                    propFilters.Add(new KeyValuePair <string, JToken>(filterKv.Key, filterKv.Value));
                }
                parentSpec.Filters.Add(new FiltrLeafSpec(propFilters.AsReadOnly()));
            }
        }