| 1 |
#Region "Microsoft.VisualBasic::32a6b66e6cbc949c5d0aec33fb5bc4ee, Microsoft.VisualBasic.Core\Serialization\ConfigMappings\ConfigurationMappings.vb"
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#End Region
|
| 43 |
|
| 44 |
Imports System.Reflection
|
| 45 |
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
|
| 46 |
Imports Microsoft.VisualBasic.Language
|
| 47 |
Imports Microsoft.VisualBasic.Scripting.Runtime
|
| 48 |
|
| 49 |
Namespace Serialization
|
| 50 |
|
| 51 |
#If NET_40 = 0 Then
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
Public Module ConfigurationMappings
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
Public Function LoadMapping(Of T As Class, TMaps As Class)(source As TMaps) As T
|
| 68 |
Dim Mappings = GetNodeMapping(Of T, TMaps)(source)
|
| 69 |
Dim DataModel As T = Activator.CreateInstance(Of T)()
|
| 70 |
|
| 71 |
For Each Node In Mappings
|
| 72 |
Dim value As Object = Node.Source.GetValue(source)
|
| 73 |
Dim str As String = CStrSafe(value, "")
|
| 74 |
value = Node.SourceToMappingCasting(str)
|
| 75 |
Call Node.Mapping.SetValue(DataModel, value)
|
| 76 |
Next
|
| 77 |
|
| 78 |
Return DataModel
|
| 79 |
End Function
|
| 80 |
|
| 81 |
Public Function WriteMapping(Of T As Class, TMaps As Class)(model As T, ByRef WriteToSource As TMaps) As TMaps
|
| 82 |
Dim Mappings = GetNodeMapping(Of T, TMaps)(Nothing)
|
| 83 |
|
| 84 |
For Each Node In Mappings
|
| 85 |
If Node.MappingToSourceCasting Is Nothing Then
|
| 86 |
Continue For
|
| 87 |
End If
|
| 88 |
|
| 89 |
Dim value As Object = Node.Mapping.GetValue(model)
|
| 90 |
Dim str As String = Node.MappingToSourceCasting(value)
|
| 91 |
Call Node.Source.SetValue(WriteToSource, str)
|
| 92 |
Next
|
| 93 |
|
| 94 |
Return WriteToSource
|
| 95 |
End Function
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
Public Function WriteMapping(Of T As Class, TMaps As Class)(Model As T) As TMaps
|
| 106 |
Dim Source As TMaps = Activator.CreateInstance(Of TMaps)()
|
| 107 |
Return WriteMapping(Of T, TMaps)(Model, WriteToSource:=Source)
|
| 108 |
End Function
|
| 109 |
|
| 110 |
Private Function __knowsIsIgnored(p As PropertyInfo) As Boolean
|
| 111 |
Dim c_attrs As Object() = p.GetCustomAttributes(attributeType:=GetType(MappingsIgnored), inherit:=False)
|
| 112 |
Return Not c_attrs.IsNullOrEmpty
|
| 113 |
End Function
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
Public Function GetNodeMapping(Of T As Class, TMaps As Class)(obj_source As Object) As NodeMapping()
|
| 123 |
Dim LQuery As PropertyInfo() = LinqAPI.Exec(Of PropertyInfo) <=
|
| 124 |
From p As PropertyInfo
|
| 125 |
In GetType(TMaps).GetProperties(BindingFlags.Instance Or BindingFlags.Public)
|
| 126 |
Where Not __knowsIsIgnored(p) AndAlso
|
| 127 |
DataFramework.StringParsers.ContainsKey(p.PropertyType)
|
| 128 |
Select p
|
| 129 |
Dim T_EntityType As Type = GetType(T)
|
| 130 |
Dim CustomMappings As MethodInfo() = LinqAPI.Exec(Of MethodInfo) <=
|
| 131 |
From entry As MethodInfo
|
| 132 |
In GetType(TMaps).GetMethods()
|
| 133 |
Where entry.ReturnType <> GetType(System.Void) AndAlso
|
| 134 |
entry.GetParameters.Length = 1
|
| 135 |
Select entry
|
| 136 |
|
| 137 |
Dim Mappings = From p As PropertyInfo
|
| 138 |
In T_EntityType.GetProperties(BindingFlags.Instance Or BindingFlags.Public)
|
| 139 |
Let array As PropertyInfo =
|
| 140 |
LQuery.Where(Function(prop) String.Equals(prop.Name, p.Name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault
|
| 141 |
Where Not array Is Nothing AndAlso
|
| 142 |
Not __knowsIsIgnored(p)
|
| 143 |
Select source = array,
|
| 144 |
MappingToModel = p
|
| 145 |
Dim out = From mapping
|
| 146 |
In Mappings
|
| 147 |
Let sourceMapping = __getReads_MappingHandle(mapping.source, mapping.MappingToModel, CustomMappings, obj_source)
|
| 148 |
Let model2Mapping = __getWrite_MappingHandle(mapping.source, mapping.MappingToModel, CustomMappings)
|
| 149 |
Select mapping.source,
|
| 150 |
mapping.MappingToModel,
|
| 151 |
sourceMapping,
|
| 152 |
model2Mapping
|
| 153 |
|
| 154 |
Return LinqAPI.Exec(Of NodeMapping) <=
|
| 155 |
_
|
| 156 |
From map
|
| 157 |
In out
|
| 158 |
Where Not (map.sourceMapping Is Nothing)
|
| 159 |
Let nodeMap As NodeMapping = New NodeMapping With {
|
| 160 |
.Source = map.source,
|
| 161 |
.Mapping = map.MappingToModel,
|
| 162 |
.SourceToMappingCasting = map.sourceMapping,
|
| 163 |
.MappingToSourceCasting = map.model2Mapping
|
| 164 |
}
|
| 165 |
Select nodeMap
|
| 166 |
End Function
|
| 167 |
|
| 168 |
Private Function __getWrite_MappingHandle(source As PropertyInfo, Model As PropertyInfo, Methods As MethodInfo()) As IStringBuilder
|
| 169 |
If DataFramework.StringBuilders.ContainsKey(Model.PropertyType) Then
|
| 170 |
Return DataFramework.StringBuilders(Model.PropertyType)
|
| 171 |
Else
|
| 172 |
Dim Method As MethodInfo = __getCustomMapping(
|
| 173 |
p_Type:=Model.PropertyType,
|
| 174 |
ReturnedType:=source.PropertyType,
|
| 175 |
Methods:=Methods)
|
| 176 |
Return Function(obj As Object) DirectCast(Method.Invoke(Nothing, {obj}), String)
|
| 177 |
End If
|
| 178 |
End Function
|
| 179 |
|
| 180 |
Private Function __getReads_MappingHandle(source As PropertyInfo, Model As PropertyInfo, Methods As MethodInfo(), obj_source As Object) As IStringParser
|
| 181 |
If DataFramework.StringParsers.ContainsKey(Model.PropertyType) Then
|
| 182 |
Return DataFramework.StringParsers(Model.PropertyType)
|
| 183 |
Else
|
| 184 |
Dim method As MethodInfo = __getCustomMapping(
|
| 185 |
p_Type:=source.PropertyType,
|
| 186 |
ReturnedType:=Model.PropertyType,
|
| 187 |
Methods:=Methods)
|
| 188 |
#If DEBUG Then
|
| 189 |
If method Is Nothing Then
|
| 190 |
Call $"{source.Name} --> {Model.Name} is incomplete!".Warning
|
| 191 |
End If
|
| 192 |
#End If
|
| 193 |
Return Function(s$)
|
| 194 |
Return method.Invoke(obj_source, {s})
|
| 195 |
End Function
|
| 196 |
End If
|
| 197 |
End Function
|
| 198 |
|
| 199 |
Private Function __getCustomMapping(p_Type As Type, ReturnedType As Type, Methods As MethodInfo()) As MethodInfo
|
| 200 |
Dim LQuery As MethodInfo = LinqAPI.DefaultFirst(Of MethodInfo) <=
|
| 201 |
From entryPoint As MethodInfo
|
| 202 |
In Methods
|
| 203 |
Where entryPoint.GetParameters.First.ParameterType = p_Type AndAlso
|
| 204 |
entryPoint.ReturnType = ReturnedType
|
| 205 |
Select entryPoint
|
| 206 |
|
| 207 |
Return LQuery
|
| 208 |
End Function
|
| 209 |
End Module
|
| 210 |
#End If
|
| 211 |
End Namespace
|