Fauna .NET Driver 0.1.0-beta
 
Loading...
Searching...
No Matches
LookupTable.cs
Go to the documentation of this file.
1using Fauna.Mapping;
3using Fauna.Util;
4using System.Linq.Expressions;
5using System.Reflection;
6
7namespace Fauna.Linq;
8
9// TODO(matt) reconcile/merge this behavior with MappingCtx
10internal record struct LookupTable(MappingContext Ctx)
11{
12 public record class Result(string Name, IDeserializer Deserializer, Type Type);
13 private static Result R(string name, IDeserializer deser, Type ty) => new Result(name, deser, ty);
14
15 public Result? FieldLookup(PropertyInfo prop, Expression callee)
16 {
17 if (Ctx.TryGetBaseType(callee.Type, out var info))
18 {
19 var field = info.Fields.FirstOrDefault(f => f.Property == prop);
20 return field is null ? null : R(field.Name, field.Deserializer, field.Type);
21 }
22
23 return Table(prop, callee);
24 }
25
26 public Result? MethodLookup(MethodInfo method, Expression callee) =>
27 Table(method, callee);
28
29 public bool HasField(PropertyInfo prop, Expression callee) =>
30 FieldLookup(prop, callee) is not null;
31
32 public bool HasMethod(MethodInfo method, Expression callee) =>
33 MethodLookup(method, callee) is not null;
34
35
36 // built-ins
37
38 private Result? Table(MemberInfo member, Expression callee) =>
39 callee.Type.Name switch
40 {
41 "string" => StringTable(member, callee),
42 _ => null,
43 };
44
45 private Result? StringTable(MemberInfo member, Expression callee) =>
46 member.Name switch
47 {
48 "Length" => R("length", Deserializer.Generate<int>(Ctx), typeof(int)),
49 "EndsWith" => R("endsWith", Deserializer.Generate<bool>(Ctx), typeof(int)),
50 "StartsWith" => R("startsWith", Deserializer.Generate<bool>(Ctx), typeof(int)),
51 _ => null,
52 };
53}