forked from nbuilder/nbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SequentialPropertyNamer.cs
182 lines (149 loc) · 5.72 KB
/
SequentialPropertyNamer.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FizzWare.NBuilder.Implementation;
using System.Text;
using FizzWare.NBuilder.Extensions;
namespace FizzWare.NBuilder.PropertyNaming
{
public class SequentialPropertyNamer : PropertyNamer
{
public SequentialPropertyNamer(IReflectionUtil reflectionUtil, BuilderSettings builderSettings)
: base(reflectionUtil,builderSettings)
{
}
private int _sequenceNumber;
public override void SetValuesOfAllIn<T>(IList<T> objects)
{
_sequenceNumber = 1;
var type = typeof(T);
for (int i = 0; i < objects.Count; i++)
{
foreach (var propertyInfo in type.GetProperties(FLAGS).Where(p => p.CanWrite))
{
SetMemberValue(propertyInfo, objects[i]);
}
foreach (var fieldInfo in type.GetFields().Where(f => !f.IsLiteral))
{
SetMemberValue(fieldInfo, objects[i]);
}
_sequenceNumber++;
}
}
public override void SetValuesOf<T>(T obj)
{
_sequenceNumber = 1;
base.SetValuesOf(obj);
}
/// <summary>
/// Gets the new sequence number taking into account a maximum value.
///
/// If the current sequence number is above the maximum value it will
/// reset it to one, and continue the sequence from there until the maximum
/// value is reached again.
/// </summary>
/// <param name="sequenceNumber">The sequence number.</param>
/// <param name="maxValue">The max value.</param>
/// <returns></returns>
private static int GetNewSequenceNumber(int sequenceNumber, int maxValue)
{
int newSequenceNumber = sequenceNumber % maxValue;
if (newSequenceNumber == 0)
{
newSequenceNumber = maxValue;
}
return newSequenceNumber;
}
protected override short GetInt16(MemberInfo memberInfo)
{
int newSequenceNumber = GetNewSequenceNumber(_sequenceNumber, short.MaxValue);
return Convert.ToInt16(newSequenceNumber);
}
protected override int GetInt32(MemberInfo memberInfo)
{
return _sequenceNumber;
}
protected override long GetInt64(MemberInfo memberInfo)
{
return Convert.ToInt64(_sequenceNumber);
}
protected override decimal GetDecimal(MemberInfo memberInfo)
{
return Convert.ToDecimal(_sequenceNumber);
}
protected override float GetSingle(MemberInfo memberInfo)
{
return Convert.ToSingle(_sequenceNumber);
}
protected override double GetDouble(MemberInfo memberInfo)
{
return Convert.ToDouble(_sequenceNumber);
}
protected override ushort GetUInt16(MemberInfo memberInfo)
{
return Convert.ToUInt16(_sequenceNumber);
}
protected override uint GetUInt32(MemberInfo memberInfo)
{
return Convert.ToUInt32(_sequenceNumber);
}
protected override ulong GetUInt64(MemberInfo memberInfo)
{
return Convert.ToUInt64(_sequenceNumber);
}
protected override char GetChar(MemberInfo memberInfo)
{
int newSequenceNumber = GetNewSequenceNumber(_sequenceNumber, 26);
newSequenceNumber += 64;
return Convert.ToChar(newSequenceNumber);
}
protected override byte GetByte(MemberInfo memberInfo)
{
int newSequenceNumber = GetNewSequenceNumber(_sequenceNumber, byte.MaxValue);
return Convert.ToByte(newSequenceNumber);
}
protected override sbyte GetSByte(MemberInfo memberInfo)
{
int newSequenceNumber = GetNewSequenceNumber(_sequenceNumber, sbyte.MaxValue);
return Convert.ToSByte(newSequenceNumber);
}
protected override DateTime GetDateTime(MemberInfo memberInfo, DateTimeKind kind = DateTimeKind.Unspecified)
{
var dateTime = DateTime.Now.Date.AddDays(_sequenceNumber - 1);
return DateTime.SpecifyKind(dateTime, kind);
}
protected override string GetString(MemberInfo memberInfo)
{
return memberInfo.Name + _sequenceNumber;
}
protected override bool GetBoolean(MemberInfo memberInfo)
{
return (_sequenceNumber % 2) == 0 ? true : false;
}
protected override Enum GetEnum(MemberInfo memberInfo)
{
Type enumType = GetMemberType(memberInfo);
var enumValues = GetEnumValues(enumType);
int newSequenceNumber = GetNewSequenceNumber(_sequenceNumber, enumValues.Length);
return Enum.Parse(enumType, enumValues.GetValue(newSequenceNumber - 1).ToString(), true) as Enum;
}
protected override Guid GetGuid(MemberInfo memberInfo)
{
var bytes = new byte[16];
BitConverter.GetBytes(_sequenceNumber).CopyTo(bytes, 0);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
return new Guid(bytes);
}
protected override TimeSpan GetTimeSpan(MemberInfo memberInfo)
{
return new TimeSpan(_sequenceNumber);
}
// TODO: Implement this
//public static void AddHandlerFor<T>(Func<MemberInfo, int, T> func)
//{
// throw new NotImplementedException();
//}
}
}