Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
MappingInfo.cs
Go to the documentation of this file.
1using System.Collections.Immutable;
2using System.Reflection;
6
7namespace Fauna.Mapping;
8
12public sealed class MappingInfo
13{
17 public Type Type { get; }
21 public IReadOnlyList<FieldInfo> Fields { get; }
25 public IReadOnlyDictionary<string, FieldInfo> FieldsByName { get; }
26
27 internal bool ShouldEscapeObject { get; }
28 internal ISerializer ClassSerializer { get; }
29 internal Module? Collection { get; }
30
31 internal MappingInfo(MappingContext ctx, Type ty, string? colName = null)
32 {
33 ctx.Add(ty, this);
34 Type = ty;
35 Collection = colName != null ? new Module(colName) : null;
36
37 var objAttr = ty.GetCustomAttribute<ObjectAttribute>();
38 bool hasAttributes = objAttr != null;
39 var fields = new List<FieldInfo>();
40 var byName = new Dictionary<string, FieldInfo>();
41
42 foreach (var prop in ty.GetProperties())
43 {
44 var attr = hasAttributes ?
45 prop.GetCustomAttribute<FieldAttribute>() :
46 new FieldAttribute();
47
48 if (attr is null) continue;
49
50 var info = new FieldInfo(ctx, attr, prop);
51
52 if (byName.ContainsKey(info.Name))
53 throw new ArgumentException($"Duplicate field name {info.Name} in {ty}");
54
55 fields.Add(info);
56 byName[info.Name] = info;
57 }
58
59 ShouldEscapeObject = Serializer.Tags.Overlaps(byName.Values.Select(i => i.Name));
60 Fields = fields.ToImmutableList();
61 FieldsByName = byName.ToImmutableDictionary();
62
63 var serType = typeof(ClassSerializer<>).MakeGenericType(new[] { ty });
64 ClassSerializer = (ISerializer)Activator.CreateInstance(serType, this, Collection != null)!;
65 }
66}
Fauna.Types.Module Module
Definition MappingInfo.cs:5
Attribute used to specify properties of a field in a Fauna object.
Definition Attributes.cs:17
Attribute used to indicate that a class represents a Fauna document or struct.
Definition Attributes.cs:9
A class that encapsulates the field mapping, serialization, and deserialization of a particular field...
Definition FieldInfo.cs:11
A class representing the mapping context to be used during serialization and deserialization.
A class that encapsulates the class mapping, serialization, and deserialization of a Fauna object,...
Type Type
The associated type.
IReadOnlyList< FieldInfo > Fields
A read-only list of FieldInfo representing the object.
IReadOnlyDictionary< string, FieldInfo > FieldsByName
A read-only dictionary of FieldInfo representing the object.
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
Definition Module.cs:8
Definition Client.cs:8