Exemple #1
0
        public TestFalcorRouter()
        {
            Get["foo[{integers:ids}].name"] = parameters =>
            {
                NumericSet ids     = parameters.ids;
                var        results = ids.Select(id => new PathValue(FalcorPath.Create("foo", id, "name"), "Jill-" + id));
                return(CompleteAsync(results));
            };

            Get["foo"] = parameters => CompleteAsync(new PathValue(FalcorPath.Create("foo"), "bar"));

            Set["todos[{integers:ids}].done"] = async parameters =>
            {
                try
                {
                    NumericSet ids       = parameters.ids;
                    dynamic    jsonGraph = parameters.jsonGraph;

                    ids.ToList().ForEach(id =>
                    {
                        todos[id].done = (bool)jsonGraph["todos"][id.ToString()]["done"];
                    });

                    var result = await Task.FromResult(ids.Select(id => Path("todos", id).Key("done").Atom(todos[id].done)));

                    return(Complete(result));
                }
                catch (Exception)
                {
                    return(null);
                }
            };
        }
Exemple #2
0
        internal static Route MatchAndBindParameters(this Route inner, IReadOnlyList <PathMatcher> pathMatchers)
        {
            return(context =>
            {
                if (pathMatchers.Count > context.Unmatched.Count)
                {
                    return context.Reject();
                }

                var matches = new List <MatchResult>();

                // Use a for loop so we can return a rejection early if there is no match
                for (var i = 0; i < pathMatchers.Count; i++)
                {
                    var matchResult = pathMatchers[i](context.Unmatched[i]);
                    if (!matchResult.IsMatched)
                    {
                        return context.Reject();
                    }
                    matches.Add(matchResult);
                }

                var unmatched = new FalcorPath(context.Unmatched.Skip(pathMatchers.Count));
                var parameters = new DynamicDictionary();
                matches.Where(m => m.HasValue && m.HasValue).ToList().ForEach(m => parameters.Add(m.Name, m.Value));
                return inner(context.WithUnmatched(unmatched, parameters))
                // Only allow partial matches if we have a ref in the results
                .Select(
                    result =>
                    result.IsComplete && result.UnmatchedPath.Any() && !result.Values.Any(pv => pv.Value is Ref)
                                ? RouteResult.Reject($"Unhandled key segments: {result.UnmatchedPath}")
                                : result);
            });
        }
 public PathValueResultHelper(FalcorPath path, IReadOnlyList <KeySegment> keys = null,
                              IReadOnlyList <PathValue> results = null)
 {
     Path        = path;
     CurrentKeys = keys ?? EmptyKeySegmentList;
     Results     = results ?? EmptyPathValueList;
 }
Exemple #4
0
 public RouteParsingTest ShouldFail(params KeySegment[] keys)
 {
     PathTests.Add(new RouteParsingSubTest {
         Path = FalcorPath.Create(keys), ShouldMatch = false
     });
     return(this);
 }
Exemple #5
0
        public TestFalcorRouter()
        {
            Get["foo[{integers:ids}].name"] = parameters =>
            {
                NumericSet ids     = parameters.ids;
                var        results = ids.Select(id => new PathValue(FalcorPath.From("foo", id, "name"), "Jill-" + id));
                return(Complete(results));
            };

            Get["foo"] = parameters => Complete(new PathValue(new FalcorPath("foo"), "bar"));
        }
Exemple #6
0
 private bool IsMatch(List <PathMatcher> matchers, FalcorPath path)
 {
     for (var index = 0; index < matchers.Count; index++)
     {
         var result = matchers[index](path[index]);
         if (!result.IsMatched)
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #7
0
        public void ShouldSerializeAtomBoolean()
        {
            "ResponseSerializer should handle bollean atom values".x(() =>
            {
                var response = new List <PathValue>
                {
                    new PathValue(FalcorPath.Create("todos", 1, "done"), new Atom(true))
                };
                var serialize  = new ResponseSerializer().Serialize(FalcorResponse.From(response.AsReadOnly()));
                dynamic result = JsonConvert.DeserializeObject(serialize);

                Assert.Equal(result["jsonGraph"]["todos"]["1"]["done"]["value"].Value, true);
            });
        }
        public void Comprehensive()
        {
            "Response builder should produce expected result".x(() =>
            {
                var results    = new List <PathValue>();
                var pathValues = new List <PathValue>
                {
                    new PathValue(FalcorPath.From("foo"), "hello"),
                    new PathValue(FalcorPath.From("bar"), "hello"),
                    new PathValue(FalcorPath.From("baz", 1, "first"), "Jessica"),
                    new PathValue(FalcorPath.From("baz", 1, "last"), "Smith"),
                    new PathValue(FalcorPath.From("baz", 1, "phone"), "111-222-1245"),
                    new PathValue(FalcorPath.From("baz", 2, "first"), "Jessica"),
                    new PathValue(FalcorPath.From("baz", 2, "last"), "Smith"),
                    new PathValue(FalcorPath.From("baz", 2, "phone"), "111-222-1245"),
                    new PathValue(FalcorPath.From("baz", 3, "first"), "Jessica"),
                    new PathValue(FalcorPath.From("baz", 3, "last"), "Smith"),
                    new PathValue(FalcorPath.From("baz", 3, "phone"), "111-222-1245")
                };
                var nameHello = new Dictionary <string, object>
                {
                    { "first", "Jessica" },
                    { "last", "Smith" },
                    { "phone", "111-222-1245" }
                };

                var expected = new Dictionary <string, object>
                {
                    { "foo", "hello" },
                    { "bar", "hello" },
                    {
                        "baz", new Dictionary <string, object>
                        {
                            { "1", nameHello },
                            { "2", nameHello },
                            { "3", nameHello }
                        }
                    }
                };
                results.AddRange(pathValues);
                var response = FalcorResponse.From(results);
                Assert.Equal(expected, response.JsonGraph);
            });
        }
        private static void AddPath(IDictionary<string, object> jsonGraphNode, FalcorPath path, object value)
        {
            var key = path.First().ToString();

            if (path.Count == 1)
            {
                jsonGraphNode[key] = value;
            }
            else
            {
                IDictionary<string, object> nextData;
                if (!jsonGraphNode.ContainsKey(key))
                {
                    nextData = new Dictionary<string, object>();
                    jsonGraphNode.Add(key, nextData);
                }
                else
                {
                    nextData = (IDictionary<string, object>) jsonGraphNode[key];
                }
                AddPath(nextData, path.Skip(1).ToList(), value);
            }
        }
Exemple #10
0
        private static void AddPath(IDictionary <string, object> jsonGraphNode, FalcorPath path, object value)
        {
            var key = path.First().ToString();

            if (path.Count == 1)
            {
                jsonGraphNode[key] = value;
            }
            else
            {
                IDictionary <string, object> nextData;
                if (!jsonGraphNode.ContainsKey(key))
                {
                    nextData = new Dictionary <string, object>();
                    jsonGraphNode.Add(key, nextData);
                }
                else
                {
                    nextData = (IDictionary <string, object>)jsonGraphNode[key];
                }
                AddPath(nextData, path.Skip(1).ToList(), value);
            }
        }
 public RequestContext WithUnmatched(FalcorPath unmatched, dynamic parameters = null) =>
     new RequestContext(Request, unmatched, parameters);
Exemple #12
0
 // Helpers
 protected static IPathValueBuilder Path(params KeySegment[] keys)
 => new PathValueResultHelper(FalcorPath.Create(keys));
 public void Add(FalcorPath path, object value) => _pathValues.Add(new PathValue(path, value));
Exemple #14
0
 public static FalcorRequest Call(params KeySegment[] keys) => Call(FalcorPath.Create(keys));
Exemple #15
0
 public static IPathValueBuilder Path(FalcorPath path) => new PathValueResultHelper(path);
 public static IPathValueBuilder Path(FalcorPath path) => new PathValueResultHelper(path);
Exemple #17
0
 public RequestContext(FalcorRequest request, FalcorPath unmatched, dynamic parameters = null)
 {
     Request    = request;
     Unmatched  = unmatched;
     Parameters = parameters;
 }
Exemple #18
0
 public IPathValueBuilder Append(params KeySegment[] keys) =>
 new PathValueResultHelper(Path.AppendAll(FalcorPath.Create(keys)));
Exemple #19
0
 public CompleteRouteResult(FalcorPath unmatchedPath, IEnumerable <PathValue> values)
 {
     UnmatchedPath = unmatchedPath;
     Values        = values.ToArray();
 }
Exemple #20
0
 public RequestContext WithUnmatched(FalcorPath unmatched, dynamic parameters = null) =>
 new RequestContext(Request, unmatched, parameters);
 public override RouteResult ToRouteResult(FalcorPath unmatchedPath) => new RejectedRouteResult(Error);
Exemple #22
0
 public IObservable <RouteResult> Complete(FalcorPath unmatched, params PathValue[] values)
 => Observable.Return(RouteResult.Complete(unmatched, values));
 public IObservable<RouteResult> Complete(FalcorPath unmatched, params PathValue[] values) => Observable.Return(RouteResult.Complete(unmatched, values));
Exemple #24
0
 IPathValueBuilderIntermediateResult IPathValueBuilderWithKey.Ref(params KeySegment[] keys)
 => WithResult(new Ref(FalcorPath.Create(keys)));
 public RequestContext(FalcorRequest request, FalcorPath unmatched, dynamic parameters = null)
 {
     Request = request;
     Unmatched = unmatched;
     Parameters = parameters;
 }
 public bool Contains(FalcorPath path) => _pathValues.Any(pv => pv.Path.Equals(path));
Exemple #27
0
 public IPathValueBuilderResult Ref(params KeySegment[] keys) => WithResult(new Ref(FalcorPath.Create(keys)));
Exemple #28
0
 private static FalcorPath Path(params KeySegment[] keys) => FalcorPath.Create(keys);
Exemple #29
0
 public PathValue(FalcorPath path, object value)
 {
     Path = path;
     Value = value;
 }
 public static RouteResult Complete(FalcorPath unmatched, params PathValue[] values) => new CompleteRouteResult(unmatched, values);
Exemple #31
0
 public static IPathValueBuilder Path(params KeySegment[] keys)
 => new PathValueResultHelper(FalcorPath.From(keys));
Exemple #32
0
 public static RouteResult Complete(FalcorPath unmatched, IEnumerable <PathValue> values)
 => new CompleteRouteResult(unmatched, values);
Exemple #33
0
 public static FalcorRequest Set(params KeySegment[] keys) => Set(FalcorPath.Create(keys));
Exemple #34
0
 public static RouteResult Complete(FalcorPath unmatched, params PathValue[] values)
 => new CompleteRouteResult(unmatched, values);
 public CompleteRouteResult(FalcorPath unmatchedPath, IEnumerable<PathValue> values)
 {
     UnmatchedPath = unmatchedPath;
     Values = values.ToArray();
 }
 public override RouteResult ToRouteResult(FalcorPath unmatchedPath) =>
     new CompleteRouteResult(unmatchedPath, _values);
Exemple #37
0
 public override RouteResult ToRouteResult(FalcorPath unmatchedPath) =>
 new CompleteRouteResult(unmatchedPath, _values);
Exemple #38
0
 public abstract RouteResult ToRouteResult(FalcorPath unmatchedPath);
 public static RouteResult Complete(FalcorPath unmatched, IEnumerable<PathValue> values) => new CompleteRouteResult(unmatched, values);
 public override RouteResult ToRouteResult(FalcorPath unmatchedPath) => new RejectedRouteResult(Error);
 private bool IsMatch(List<PathMatcher> matchers, FalcorPath path)
 {
     for (var index = 0; index < matchers.Count; index++)
     {
         var result = matchers[index](path[index]);
         if (!result.IsMatched) return false;
     }
     return true;
 }
Exemple #42
0
 public PathValue(FalcorPath path, object value)
 {
     Path  = path;
     Value = value;
 }