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