Fauna .NET Driver 0.1.0-beta
 
Loading...
Searching...
No Matches
MappingInfo.cs
Go to the documentation of this file.
3using System.Collections.Immutable;
4using System.Reflection;
5
6namespace Fauna.Mapping;
7
8public sealed class MappingInfo
9{
10 public Type Type { get; }
11 public IReadOnlyList<FieldInfo> Fields { get; }
12 public IReadOnlyDictionary<string, FieldInfo> FieldsByName { get; }
13
14 internal bool ShouldEscapeObject { get; }
15 internal IClassDeserializer Deserializer { get; }
16
17 internal MappingInfo(MappingContext ctx, Type ty)
18 {
19 ctx.Add(ty, this);
20 Type = ty;
21
22 var objAttr = ty.GetCustomAttribute<ObjectAttribute>();
23 var hasAttributes = objAttr != null;
24 var fields = new List<FieldInfo>();
25 var byName = new Dictionary<string, FieldInfo>();
26
27 foreach (var prop in ty.GetProperties())
28 {
29 var attr = hasAttributes ?
30 prop.GetCustomAttribute<FieldAttribute>() :
31 new FieldAttribute();
32
33 if (attr is null) continue;
34
35 var info = new FieldInfo(ctx, attr, prop);
36
37 if (byName.ContainsKey(info.Name))
38 throw new ArgumentException($"Duplicate field name {info.Name} in {ty}");
39
40 fields.Add(info);
41 byName[info.Name] = info;
42 }
43
44 ShouldEscapeObject = Serializer.Tags.Overlaps(byName.Values.Select(i => i.Name));
45 Fields = fields.ToImmutableList();
46 FieldsByName = byName.ToImmutableDictionary();
47
48 var deserType = typeof(ClassDeserializer<>).MakeGenericType(new[] { ty });
49 Deserializer = (IClassDeserializer)Activator.CreateInstance(deserType, new[] { this })!;
50 }
51}
Attribute used to specify properties of a field in a Fauna object.
Definition Attributes.cs:30
Attribute used to indicate that a class represents a Fauna document or struct.
Definition Attributes.cs:22
IReadOnlyList< FieldInfo > Fields
IReadOnlyDictionary< string, FieldInfo > FieldsByName