-
Notifications
You must be signed in to change notification settings - Fork 1
/
AValue.tt
349 lines (310 loc) · 15.2 KB
/
AValue.tt
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<#@ template debug="false" hostSpecific="false" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
<#
string[] nativeDataTypes = new string[]
{ "string", "bool", "Enum", "Guid",
"short", "int", "long",
"ushort", "uint", "ulong",
"decimal", "float", "double", "byte", "sbyte",
"DateTime", "DateTimeOffset", "TimeSpan" };
Type[] nativeclassDataTypesOf = new Type[]
{ typeof(string), typeof(bool), typeof(Enum), typeof(Guid),
typeof(short), typeof(int), typeof(long),
typeof(ushort), typeof(uint), typeof(ulong),
typeof(decimal), typeof(float), typeof(double), typeof(byte), typeof(sbyte),
typeof(DateTime), typeof(DateTimeOffset), typeof(TimeSpan) };
string[] jsonClassDataTypes = new string[]
{ "JObject", "JArray", "JValue", "JToken"};
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace Aerospike.Database.LINQPadDriver.Extensions
{
partial class AValue : IConvertible,
IComparable,
IEquatable<AValue>,
IEqualityComparer<AValue>,
IComparable<AValue>,
IEquatable<Aerospike.Client.Key>,
IEqualityComparer<Aerospike.Client.Key>,
IComparable<Aerospike.Client.Key>,
IEquatable<Aerospike.Client.Value>,
IEqualityComparer<Aerospike.Client.Value>,
IComparable<Aerospike.Client.Value>
<# foreach(var datatype in nativeDataTypes)
{ #>
, IEquatable< <#= datatype #> >
, IEqualityComparer< <#= datatype #> >
, IComparable< <#= datatype #> >
<# }//end of foreach data type #>
<# foreach(var datatype in jsonClassDataTypes)
{ #>
, IEquatable< <#= datatype #> >
, IEqualityComparer< <#= datatype #> >
<# }//end of foreach data type #>
{
#region To type Methods
/// <summary>
/// Tries to convert <see cref="Value"/> to a JToken.
/// </summary>
/// <returns>A <see cref="JToken"/> or an empty JToken</returns>
public JToken ToJson()
{
if (
<# foreach(var datatype in jsonClassDataTypes)
{ #>
this.UnderlyingType == typeof(<#= datatype #>) ||
<# }//end of foreach data type #>
false)
{
return (JToken)this.Value;
}
try
{
return (JToken) new JObject(this.Value);
} catch
{
return new JObject();
}
}
#pragma warning disable CS0618 // Type or member is obsolete
/// <summary>
/// Returns the string for <see cref="Value"/> using the given format, if possible.
/// Otherwise the ToString of <see cref="Value"/> is used.
/// </summary>
/// <param name="format">A composite format string.</param>
/// <param name="throwOnFormatException">
/// If true and a format exception occurs, it will be re-thrown.
/// The default is false and the ToString value is returned.
/// </param>
/// <returns>The string value of <see cref="Value"/></returns>
public string ToString(string format, bool throwOnFormatException = false)
{
try
{
switch(this.Value)
{
<# foreach(var datatype in nativeclassDataTypesOf)
{
if(datatype.GetMethod("ToString", new Type[] { typeof(string) }) != null)
{ #>
case <#= datatype #> v<#= datatype.Name #>:
return v<#= datatype.Name #>.ToString(format);
<# }//end of if
}//end of foreach data type #>
default:
break;
}
throwOnFormatException = false;
return String.Format($"{{0:{format}}}", this.Value);
}
catch(FormatException ex)
{
if(throwOnFormatException)
{
if (Client.Log.DebugEnabled())
{
Client.Log.Error($"AValue.ToString Exception {ex.GetType().Name} ({ex.Message})");
DynamicDriver.WriteToLog(ex, "AValue.ToString");
}
throw;
}
}
return this.ToString();
}
/// <summary>
/// Returns the string for <see cref="Value"/> using the given provider, if possible.
/// Otherwise the ToString of <see cref="Value"/> is used.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="throwOnFormatException">
/// If true and a format exception occurs, it will be re-thrown.
/// The default is false and the ToString value is returned.
/// </param>
/// <returns>The string value of <see cref="Value"/></returns>
public string ToString(IFormatProvider provider, bool throwOnFormatException = false)
{
try
{
switch(this.Value)
{
<# foreach(var datatype in nativeclassDataTypesOf)
{
if(datatype.GetMethod("ToString", new Type[] { typeof(IFormatProvider) }) != null)
{ #>
case <#= datatype #> v<#= datatype.Name #>:
return v<#= datatype.Name #>.ToString(provider);
<# }//end of if
}//end of foreach data type #>
default:
break;
}
return String.Format(provider, "{0}", this.Value);
}
catch(FormatException ex)
{
if(throwOnFormatException)
{
if (Client.Log.DebugEnabled())
{
Client.Log.Error($"AValue.ToString Exception {ex.GetType().Name} ({ex.Message})");
DynamicDriver.WriteToLog(ex, "AValue.ToString");
}
throw;
}
}
return this.ToString();
}
/// <summary>
/// Returns the string for <see cref="Value"/> using the given format and provider, if possible.
/// Otherwise the ToString of <see cref="Value"/> is used.
/// </summary>
/// <param name="format">A composite format string.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="throwOnFormatException">
/// If true and a format exception occurs, it will be re-thrown.
/// The default is false and the ToString value is returned.
/// </param>
/// <returns>The string value of <see cref="Value"/></returns>
public string ToString(string format, IFormatProvider provider, bool throwOnFormatException = false)
{
try
{
switch(this.Value)
{
<# foreach(var datatype in nativeclassDataTypesOf)
{
if(datatype.GetMethod("ToString", new Type[] { typeof(string), typeof(IFormatProvider) }) != null)
{ #>
case <#= datatype #> v<#= datatype.Name #>:
return v<#= datatype.Name #>.ToString(format, provider);
<# }//end of if
}//end of foreach data type #>
default:
break;
}
throwOnFormatException = false;
return String.Format(provider, $"{{0:{format}}}", this.Value);
}
catch(FormatException ex)
{
if(throwOnFormatException)
{
if (Client.Log.DebugEnabled())
{
Client.Log.Error($"AValue.ToString Exception {ex.GetType().Name} ({ex.Message})");
DynamicDriver.WriteToLog(ex, "AValue.ToString");
}
throw;
}
}
return this.ToString();
}
#pragma warning restore CS0618 // Type or member is obsolete
#endregion
#region native DataTypes operator/Methods
<# foreach(var datatype in nativeDataTypes)
{ #>
public static implicit operator <#= datatype #> (AValue v) => v.Convert< <#= datatype #> >();
//public static implicit operator <#= datatype #>[] (AValue v) => v.Convert<<#= datatype #>[] >();
public static bool operator==(AValue av, <#= datatype #> v) => av?.Equals(v) ?? false;
public static bool operator!=(AValue av, <#= datatype #> v) => !(av == v);
public static bool operator==(<#= datatype #> v, AValue av) => av == v;
public static bool operator!=(<#= datatype #> v, AValue av) => av != v;
public static bool operator==(AValue av, List< <#= datatype #> > v)
=> Helpers.SequenceEquals(v, av?.Value);
public static bool operator!=(AValue av, List< <#= datatype #> > v) => !(av == v);
public static bool operator==(List< <#= datatype #> > v, AValue av) => av == v;
public static bool operator!=(List< <#= datatype #> > v, AValue av) => av != v;
public static bool operator<(<#= datatype #> oValue, AValue aValue) => aValue is not null && (aValue.CompareTo(oValue) > 0);
public static bool operator>(<#= datatype #> oValue, AValue aValue) => aValue is null || (aValue.CompareTo(oValue) < 0);
public static bool operator<=(<#= datatype #> oValue, AValue aValue) => aValue is not null && (aValue.CompareTo(oValue) >= 0);
public static bool operator>=(<#= datatype #> oValue, AValue aValue) => aValue is null || (aValue.CompareTo(oValue) <= 0);
public static bool operator<(AValue aValue, <#= datatype #> oValue) => aValue is null || (aValue.CompareTo(oValue) < 0);
public static bool operator>(AValue aValue, <#= datatype #> oValue) => aValue is not null && (aValue.CompareTo(oValue) > 0);
public static bool operator<=(AValue aValue, <#= datatype #> oValue) => aValue is null || (aValue.CompareTo(oValue) <= 0);
public static bool operator>=(AValue aValue, <#= datatype #> oValue) => aValue is not null && (aValue.CompareTo(oValue) >= 0);
<# if(datatype != "string") { #>
public <#= datatype #> To<#= datatype #>() => (<#= datatype #>) this;
<# } #>
<# if(datatype == "string") { #>
public virtual bool Equals(<#= datatype #> value)
=> (this.DigestRequired() && this.CompareDigest(value))
|| Helpers.Equals(this.Value, (object) value);
<# } else { #>
public bool Equals(<#= datatype #> value)
=> (this.DigestRequired() && this.CompareDigest(value))
|| this.CompareTo(value) == 0;
<# } #>
public bool Equals(<#= datatype #> v1, <#= datatype #> v2) => v1 == v2;
public int GetHashCode(<#= datatype #> value) => value.GetHashCode();
public int CompareTo(<#= datatype #> value)
{
<# if(datatype == "string") { #>
if(this.Value is null) return value is null ? 0 : -1;
if(value is null) return 1;
if(this.Value is string sValue) return sValue.CompareTo(value);
if(this.Value is Guid gValue) return gValue.ToString().CompareTo(value);
return Helpers.GetStableHashCode(this.Value).CompareTo(Helpers.GetStableHashCode(value));
<# } else if(datatype == "Guid") { #>
if(this.Value is null) return -1;
if(this.Value is Guid gValue) return gValue.CompareTo(value);
if(this.Value is string sValue) return sValue.CompareTo(value.ToString());
return Helpers.GetStableHashCode(this.Value).CompareTo(Helpers.GetStableHashCode(value));
<# } else { #>
if(this.Value is null) return -1;
try {
var tValue = this.Value;
if(tValue is <#= datatype #> cValue)
return cValue.CompareTo(value);
<# if(datatype == "DateTime"
|| datatype == "DateTimeOffset"
|| datatype == "TimeSpan") { #>
if(tValue is string || tValue.GetType().IsPrimitive)
tValue = this.Convert< <#= datatype #> >();
if(tValue is IComparable vValue)
return vValue.CompareTo(value);
<# } else { #>
if(tValue is string)
return Helpers.GetStableHashCode(tValue).CompareTo(Helpers.GetStableHashCode(value));
tValue = ((IConvertible)tValue).ToType(typeof(decimal), null);
return ((decimal)tValue).CompareTo((decimal)((IConvertible)value).ToType(typeof(decimal), null));
<# } #>
} catch {}
return Helpers.GetStableHashCode(this.Value).CompareTo(Helpers.GetStableHashCode(value));
<# } #> }
<# }//end of foreach data type #>
#endregion
#region Class Data Types operator/methods
<# foreach(var datatype in jsonClassDataTypes)
{ #>
public static implicit operator <#= datatype #> (AValue key) => key is null ? null : (<#= datatype #>) key.Convert< <#= datatype #> >();
//public static implicit operator <#= datatype #>[] (AValue key) => (<#= datatype #>[]) key.Convert<<#= datatype #>[]>();
public <#= datatype #> To<#= datatype #>() => (<#= datatype #>) this;
public bool Equals(<#= datatype #> value)
{
try {
return this.DigestRequired()
? this.CompareDigest(value)
: ((<#= datatype #>) this).Equals(value);
} catch {}
return false;
}
public bool Equals(<#= datatype #> v1, <#= datatype #> v2)
{
if(ReferenceEquals(v1,v2)) return true;
if(v1 is null) return v2 is null;
return v1.Equals(v2);
}
public int GetHashCode(<#= datatype #> value) => value?.GetHashCode() ?? 0;
<# }//end of foreach data type #>
#endregion
}
}