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.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 = FalcorPath.Create(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)); parameters.Add("jsonGraph", context.Request.JsonGraph); 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 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.Create("foo"), "hello"), new PathValue(FalcorPath.Create("bar"), "hello"), new PathValue(FalcorPath.Create("baz", 1, "first"), "Jessica"), new PathValue(FalcorPath.Create("baz", 1, "last"), "Smith"), new PathValue(FalcorPath.Create("baz", 1, "phone"), "111-222-1245"), new PathValue(FalcorPath.Create("baz", 2, "first"), "Jessica"), new PathValue(FalcorPath.Create("baz", 2, "last"), "Smith"), new PathValue(FalcorPath.Create("baz", 2, "phone"), "111-222-1245"), new PathValue(FalcorPath.Create("baz", 3, "first"), "Jessica"), new PathValue(FalcorPath.Create("baz", 3, "last"), "Smith"), new PathValue(FalcorPath.Create("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); }); }
public static FalcorRequest Call(params KeySegment[] keys) => Call(FalcorPath.Create(keys));
public static FalcorRequest Set(params KeySegment[] keys) => Set(FalcorPath.Create(keys));
IPathValueBuilderIntermediateResult IPathValueBuilderWithKey.Ref(params KeySegment[] keys) => WithResult(new Ref(FalcorPath.Create(keys)));
public IPathValueBuilderResult Ref(params KeySegment[] keys) => WithResult(new Ref(FalcorPath.Create(keys)));
public IPathValueBuilder Append(params KeySegment[] keys) => new PathValueResultHelper(Path.AppendAll(FalcorPath.Create(keys)));
// Helpers protected static IPathValueBuilder Path(params KeySegment[] keys) => new PathValueResultHelper(FalcorPath.Create(keys));
private static FalcorPath Path(params KeySegment[] keys) => FalcorPath.Create(keys);