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); } }; }
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; }
public RouteParsingTest ShouldFail(params KeySegment[] keys) { PathTests.Add(new RouteParsingSubTest { Path = FalcorPath.Create(keys), ShouldMatch = false }); return(this); }
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")); }
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); }
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); } }
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);
// 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));
public static FalcorRequest Call(params KeySegment[] keys) => Call(FalcorPath.Create(keys));
public static IPathValueBuilder Path(FalcorPath path) => new PathValueResultHelper(path);
public RequestContext(FalcorRequest request, FalcorPath unmatched, dynamic parameters = null) { Request = request; Unmatched = unmatched; Parameters = parameters; }
public IPathValueBuilder Append(params KeySegment[] keys) => new PathValueResultHelper(Path.AppendAll(FalcorPath.Create(keys)));
public CompleteRouteResult(FalcorPath unmatchedPath, IEnumerable <PathValue> values) { UnmatchedPath = unmatchedPath; Values = values.ToArray(); }
public override RouteResult ToRouteResult(FalcorPath unmatchedPath) => new RejectedRouteResult(Error);
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));
IPathValueBuilderIntermediateResult IPathValueBuilderWithKey.Ref(params KeySegment[] keys) => WithResult(new Ref(FalcorPath.Create(keys)));
public bool Contains(FalcorPath path) => _pathValues.Any(pv => pv.Path.Equals(path));
public IPathValueBuilderResult Ref(params KeySegment[] keys) => WithResult(new Ref(FalcorPath.Create(keys)));
private static FalcorPath Path(params KeySegment[] keys) => FalcorPath.Create(keys);
public PathValue(FalcorPath path, object value) { Path = path; Value = value; }
public static RouteResult Complete(FalcorPath unmatched, params PathValue[] values) => new CompleteRouteResult(unmatched, values);
public static IPathValueBuilder Path(params KeySegment[] keys) => new PathValueResultHelper(FalcorPath.From(keys));
public static RouteResult Complete(FalcorPath unmatched, IEnumerable <PathValue> values) => new CompleteRouteResult(unmatched, values);
public static FalcorRequest Set(params KeySegment[] keys) => Set(FalcorPath.Create(keys));
public CompleteRouteResult(FalcorPath unmatchedPath, IEnumerable<PathValue> values) { UnmatchedPath = unmatchedPath; Values = values.ToArray(); }
public override RouteResult ToRouteResult(FalcorPath unmatchedPath) => new CompleteRouteResult(unmatchedPath, _values);
public abstract RouteResult ToRouteResult(FalcorPath unmatchedPath);
public static RouteResult Complete(FalcorPath unmatched, IEnumerable<PathValue> values) => new CompleteRouteResult(unmatched, values);
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; }