Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
276 lines (237 loc) · 9.51 KB
/
Copy pathProgram.cs
File metadata and controls
276 lines (237 loc) · 9.51 KB
1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System;
using System.Collections.Generic;
using Mammoth.LiteMapper;
namespace Mammoth.LiteMapper.Samples.Basic
{
internal static class Program
{
private static void Main()
{
LazyCyclePathExample.Run();
ConfiguredUpdaterOverloadExample.Run();
EnumPathExample.Run();
PatchUpdaterExample.Run();
var staticTarget = StaticMapper.Map(new StaticSource
{
Name = "Ada",
Children = new[]
{
new ChildSource { Value = 1 },
new ChildSource { Value = 2 },
},
});
if (staticTarget.Name != "Ada" || staticTarget.DisplayName != "Name: Ada" || staticTarget.Children.Count != 2 || staticTarget.Children[1].Value != 2)
{
throw new InvalidOperationException("Static mapping failed.");
}
var instanceSource = new InstanceSource
{
Id = 42,
Child = new ChildSource { Value = 7 },
Children = new[] { new ChildSource { Value = 8 } },
};
var instanceTarget = new InstanceMapper("first:").Map(instanceSource);
var otherTarget = new InstanceMapper("second:").Map(instanceSource);
if (instanceTarget.Id != 42 || instanceTarget.Child.Value != "first:7" ||
instanceTarget.Children[0].Value != "first:8" || otherTarget.Child.Value != "second:7" ||
otherTarget.Children[0].Value != "second:8")
{
throw new InvalidOperationException("Instance mapping failed.");
}
var sharedNode = new NodeSource();
var wrapped = new WrappedSource { Envelope = new EnvelopeSource { Nodes = new[] { sharedNode, sharedNode } } };
var wrappedTarget = WrappedCycleMapper.Map(wrapped);
if (wrappedTarget.Envelope.Nodes.Count != 2 ||
ReferenceEquals(wrappedTarget.Envelope.Nodes[0], wrappedTarget.Envelope.Nodes[1]))
{
throw new InvalidOperationException("Wrapped mapping failed.");
}
sharedNode.Next = sharedNode;
try
{
WrappedCycleMapper.Map(wrapped);
throw new InvalidOperationException("Wrapped cycle detection failed.");
}
catch (LiteMapperCycleException error) when (
error.MappingMethod == "Map" && error.MemberPath == "Envelope.Nodes.Next")
{
}
var node = new NodeSource();
node.Next = node;
try
{
CycleMapper.Map(node);
throw new InvalidOperationException("Cycle detection failed.");
}
catch (LiteMapperCycleException)
{
}
}
}
[LiteMapper]
public static partial class StaticMapper
{
[MapProperty(Target = nameof(StaticTarget.DisplayName), Use = nameof(BuildDisplayName))]
public static partial StaticTarget Map(StaticSource source);
private static string BuildDisplayName(StaticSource source) => "Name: " + source.Name;
}
[System.Diagnostics.DebuggerDisplay("Static mapper")]
public static partial class StaticMapper
{
}
[LiteMapper]
public sealed partial class InstanceMapper
{
private readonly string prefix;
public InstanceMapper(string prefix = "") => this.prefix = prefix;
public partial InstanceTarget Map(InstanceSource source);
[MappingConverter]
private string Format(int value) => prefix + value;
}
[LiteMapper(ReferenceHandling = ReferenceHandling.ThrowOnCycle)]
public static partial class CycleMapper
{
public static partial NodeTarget Map(NodeSource source);
}
public sealed class StaticSource
{
public string? Name { get; set; }
public ChildSource[] Children { get; set; } = new ChildSource[0];
}
public sealed class StaticTarget
{
public string DisplayName { get; set; } = string.Empty;
public string? Name { get; set; }
public List<ChildTarget> Children { get; set; } = new List<ChildTarget>();
}
public sealed class ChildSource
{
public int Value { get; set; }
}
public sealed class ChildTarget
{
public int Value { get; set; }
}
public sealed class InstanceSource
{
public int Id { get; set; }
public ChildSource Child { get; set; } = new ChildSource();
public ChildSource[] Children { get; set; } = new ChildSource[0];
}
public sealed class InstanceTarget
{
public int Id { get; set; }
public FormattedChildTarget Child { get; set; } = new FormattedChildTarget();
public List<FormattedChildTarget> Children { get; set; } = new List<FormattedChildTarget>();
}
public sealed class FormattedChildTarget
{
public string Value { get; set; } = string.Empty;
}
[LiteMapper(ReferenceHandling = ReferenceHandling.ThrowOnCycle)]
public static partial class WrappedCycleMapper
{
public static partial WrappedTarget Map(WrappedSource source);
}
public sealed class WrappedSource
{
public EnvelopeSource Envelope { get; set; } = new EnvelopeSource();
}
public sealed class WrappedTarget
{
public EnvelopeTarget Envelope { get; set; } = new EnvelopeTarget();
}
public sealed class EnvelopeSource
{
public NodeSource[] Nodes { get; set; } = new NodeSource[0];
}
public sealed class EnvelopeTarget
{
public List<NodeTarget> Nodes { get; set; } = new List<NodeTarget>();
}
public sealed class NodeSource
{
public NodeSource? Next { get; set; }
}
public sealed class NodeTarget
{
public NodeTarget? Next { get; set; }
}
}
namespace Mammoth.LiteMapper.Samples.Basic
{
public static class EnumPathExample
{
public static void Run()
{
var source = new EnumPathSource { Child = new EnumPathChild { State = InputState.Ready } };
if (EnumPathMapper.Map(source).State != OutputState.Ready)
throw new System.InvalidOperationException("Enum source-path mapping failed.");
source.Child.State = (InputState)99;
try
{
EnumPathMapper.Map(source);
throw new System.InvalidOperationException("Unknown enum value was accepted.");
}
catch (System.ArgumentOutOfRangeException error) when (
error.ParamName == "Child.State" && error.ActualValue is InputState value && value == (InputState)99)
{
}
source.Child = null;
try
{
EnumPathMapper.Map(source);
throw new System.InvalidOperationException("Missing enum source path was accepted.");
}
catch (System.InvalidOperationException error) when (error.Message.Contains("Child.State"))
{
}
}
}
[LiteMapper(NullableMismatch = NullableMismatchPolicy.Throw)]
public static partial class EnumPathMapper
{
[MapProperty(Source = "Child.State", Target = nameof(EnumPathTarget.State))]
public static partial EnumPathTarget Map(EnumPathSource source);
}
public enum InputState { Ready = 1 }
public enum OutputState { Ready = 10 }
public sealed class EnumPathSource { public EnumPathChild? Child { get; set; } }
public sealed class EnumPathChild { public InputState State { get; set; } }
public sealed class EnumPathTarget { public OutputState State { get; set; } }
}
namespace Mammoth.LiteMapper.Samples.Basic
{
public static class PatchUpdaterExample
{
public static void Run()
{
var source = new PatchUpdaterSource(new PatchUpdaterChild { Value = 7 });
var target = new PatchUpdaterTarget();
var original = target.Child;
PatchUpdaterMapper.Apply(source, target);
if (source.Reads != 1 || target.Child.Value != 7 || !object.ReferenceEquals(original, target.Child))
throw new System.InvalidOperationException("Patch updater must use its captured child once.");
var missing = new PatchUpdaterSource(null);
PatchUpdaterMapper.Apply(missing, target);
if (missing.Reads != 1 || target.Child.Value != 7 || !object.ReferenceEquals(original, target.Child))
throw new System.InvalidOperationException("A null patch child must preserve the existing destination.");
}
}
[LiteMapper(IgnoreNullSourceMembers = true)]
public static partial class PatchUpdaterMapper
{
public static partial void Apply(PatchUpdaterSource source, PatchUpdaterTarget target);
public static partial void ApplyChild(PatchUpdaterChild source, PatchUpdaterChildTarget target);
}
public sealed class PatchUpdaterSource
{
private readonly PatchUpdaterChild? first;
public PatchUpdaterSource(PatchUpdaterChild? first) { this.first = first; }
public int Reads;
public PatchUpdaterChild? Child { get { Reads++; return Reads == 1 ? first : null; } }
}
public sealed class PatchUpdaterChild { public int Value { get; set; } }
public sealed class PatchUpdaterChildTarget { public int Value { get; set; } }
public sealed class PatchUpdaterTarget { public PatchUpdaterChildTarget Child { get; } = new PatchUpdaterChildTarget(); }
}