-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToCodeAPI.cs
424 lines (393 loc) · 12.7 KB
/
ToCodeAPI.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
using Aerospike.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aerospike.Database.LINQPadDriver.Extensions
{
/// <summary>
/// Helpers that will translate an <see cref="ARecord"/> into Aerospike API Code template
/// </summary>
public static class ToCodeAPI
{
/// <summary>
/// Enum Options used to Generate API Code
/// </summary>
[Flags]
public enum Options
{
None = 0,
/// <summary>
/// Generates Get API Code.
/// </summary>
GetCode = 0x0001,
/// <summary>
/// Generates Put API Code.
/// </summary>
PutCode = 0x0010,
/// <summary>
/// Generates Get and Put API Code.
/// </summary>
GetPutCode = GetCode | PutCode
}
static string ConvertValue(AValue aValue, bool useAerospikeAPI = false)
{
string strValue = null;
if(useAerospikeAPI)
{
var cvtValue = Helpers.ConvertToAerospikeType(aValue);
if(cvtValue is null || cvtValue is Value.NullValue)
return "Value.AsNull";
if(cvtValue is Value cValue)
{
var dtStr = Helpers.GetRealTypeName(cvtValue.GetType());
strValue = ConvertValue(cValue.Object.ToAValue());
return $"new {dtStr}({strValue})";
}
strValue = ConvertValue(cvtValue.ToAValue());
return $"Value.Get({strValue})";
}
if(aValue is null)
return "null";
if(aValue.IsString)
strValue = Helpers.ToLiteral(aValue.Convert<string>());
else if(aValue.IsBool)
strValue = aValue.Value.ToString();
else if(aValue.IsNumeric)
{
switch(aValue.Value)
{
case long lValue:
strValue = $"{lValue}L";
break;
case float fValue:
strValue = $"{fValue}F";
break;
case double dValue:
strValue = $"{dValue}D";
break;
case decimal dValue:
strValue = $"{dValue}M";
break;
case sbyte sbValue:
strValue = $"(sbyte) {sbValue}";
break;
case byte bValue:
strValue = $"(byte) {bValue}";
break;
default:
strValue = aValue.ToString();
break;
}
}
else if(aValue.IsJson || aValue.IsGeoJson)
{
strValue = aValue.ToJson().ToString(Newtonsoft.Json.Formatting.None);
strValue = $"new JObject(\"{strValue}\")";
}
else if(aValue.IsCDT)
{
var lstValue = aValue.AsEnumerable();
strValue = String.Join(",", lstValue.Select(i => ConvertValue(i)));
var dtStr = Helpers.GetRealTypeName(aValue.UnderlyingType);
var dtParam = "()";
if(dtStr[^2..] == "[]")
dtParam = string.Empty;
if(string.IsNullOrEmpty(dtStr))
strValue = $"new {dtStr}{dtParam}";
else
strValue = $"new {dtStr}{dtParam} {{ {strValue} }}";
}
else if(aValue.IsKeyValuePair)
{
dynamic kvp = aValue.Value;
var kValue = ConvertValue(((object) kvp.Key).ToAValue());
var vValue = ConvertValue(((object) kvp.Value).ToAValue());
var dtStr = Helpers.GetRealTypeName(aValue.UnderlyingType);
strValue = $"{{{kValue},{vValue}}}";
}
else if(aValue.IsDateTime)
{
strValue = $"DateTime.Parse({Helpers.ToLiteral(aValue.ToString(Helpers.DateTimeFormat))})";
}
else if(aValue.IsDateTimeOffset)
{
strValue = $"DateTimeOffset.Parse({Helpers.ToLiteral(aValue.ToString(Helpers.DateTimeOffsetFormat))})";
}
else if(aValue.IsTimeSpan)
{
strValue = $"TimeSpan.Parse({Helpers.ToLiteral(aValue.ToString(Helpers.TimeSpanFormat))})";
}
else
{
var dtStr = Helpers.GetRealTypeName(aValue.UnderlyingType);
strValue = $"new {dtStr}({Helpers.ToLiteral(aValue.ToString())})";
}
return strValue.Replace("AValue", "Object");
}
static string CreateKey(ARecord record)
=> $@"new Key(""{record.Aerospike.Namespace}"", {Helpers.ToLiteral(record.Aerospike.SetName)}, {ConvertValue(record.GetPK(), false)})";
static string CreateBin(string binName, AValue value)
=> $"new Bin({Helpers.ToLiteral(binName)}, {ConvertValue(value, true)})";
static string CreateBin(string binName, object value)
=> CreateBin(binName, (AValue) value?.ToAValue());
static string CreateKVPBin(string binName, AValue value)
=> $"{{{Helpers.ToLiteral(binName)}, {ConvertValue(value, false)}}}";
static string CreateKVPBin(string binName, object value)
=> CreateKVPBin(binName, (AValue) value?.ToAValue());
static string GenerateCode(ARecord record,
Options optCode,
bool useAerospikeAPI,
bool useBatchStmt)
{
var strCode = new StringBuilder();
switch(optCode)
{
case Options.None:
return string.Empty;
case Options.GetCode:
if(useAerospikeAPI)
{
if(useBatchStmt)
strCode.Append($"new BatchRead(null, {CreateKey(record)}, true)");
else
{
strCode.AppendLine("ASClient.Get(null,");
strCode.Append($"{CreateKey(record)})");
}
}
else if(useBatchStmt)
{
strCode.Append(ConvertValue(record.GetPK(), false));
}
else
{
var safeNSName = Helpers.CheckName(record.Aerospike.Namespace, "Namespace");
var safeSetName = Helpers.CheckName(record.Aerospike.SetName, "Set");
strCode.Append($"{safeNSName}.{safeSetName}.Get(");
strCode.Append(ConvertValue(record.GetPK(), false));
strCode.Append(')');
}
break;
case Options.PutCode:
if(useBatchStmt)
{
if(useAerospikeAPI)
{
var binOpCode = new List<string>();
foreach(var kvp in record.ToDictionary())
{
binOpCode.Add($"Operation.Put({CreateBin(kvp.Key, kvp.Value)})");
}
strCode.AppendLine("new BatchWrite(null,");
strCode.Append(CreateKey(record));
strCode.AppendLine(",");
strCode.AppendLine("new Operation[] {");
strCode.AppendJoin(",\r\n", binOpCode);
strCode.Append("})");
}
else
{
strCode.Append("new (");
strCode.Append(ConvertValue(record.GetPK(), false));
strCode.AppendLine(", ");
strCode.Append("new Dictionary<string,object>() {");
var binCode = new List<string>();
foreach(var kvp in record.ToDictionary())
{
binCode.Add(CreateKVPBin(kvp.Key, kvp.Value));
}
strCode.AppendJoin(",\r\n", binCode);
strCode.Append("})");
}
}
else
{
if(useAerospikeAPI)
{
strCode.AppendLine("ASClient.Put(null,");
strCode.AppendLine($"{CreateKey(record)}, ");
}
else
{
var safeNSName = Helpers.CheckName(record.Aerospike.Namespace, "Namespace");
var safeSetName = Helpers.CheckName(record.Aerospike.SetName, "Set");
strCode.Append($"{safeNSName}.{safeSetName}.Put(");
strCode.Append(ConvertValue(record.GetPK(), false));
strCode.AppendLine(", ");
}
if(!useAerospikeAPI)
{
strCode.Append("new Dictionary<string,object>() {");
}
var binCode = new List<string>();
foreach(var kvp in record.ToDictionary())
{
if(useAerospikeAPI)
{
binCode.Add(CreateBin(kvp.Key, kvp.Value));
}
else
{
binCode.Add(CreateKVPBin(kvp.Key, kvp.Value));
}
}
strCode.AppendJoin(",\r\n", binCode);
if(!useAerospikeAPI)
{
strCode.Append('}');
}
strCode.Append(')');
}
break;
default:
throw new ArgumentException($"Invalid Code Option of {optCode}. must be Put or Get...");
}
return strCode.ToString();
}
/// <summary>
/// Generates either the Aerospike or LINQPad API code.
/// </summary>
/// <param name="records">
/// Collection of <see cref="ARecord"/> that will be used to generate API Code.
/// </param>
/// <param name="codeOptions">
/// Code Generation Options.
/// </param>
/// <param name="useAerospikeAPI">
/// if set to <c>true</c>, the code generated will be based on the native Aerospike API driver.
/// </param>
/// <returns>
/// Returns a collection of generated API code where each element is a separate Get or Put statement.
/// </returns>
/// <seealso cref="ToAPICodeBatch(IEnumerable{ARecord}, Options, bool)"/>
/// <seealso cref="ToAPICode(ARecord, Options, bool)"/>
public static IEnumerable<string> ToAPICode(this IEnumerable<ARecord> records,
Options codeOptions = Options.GetPutCode,
bool useAerospikeAPI = false)
{
if(Options.None == codeOptions) yield break;
foreach(var record in records)
{
if(codeOptions.HasFlag(Options.GetCode))
{
yield return GenerateCode(record, Options.GetCode, useAerospikeAPI, false);
}
if(codeOptions.HasFlag(Options.PutCode))
{
yield return GenerateCode(record, Options.PutCode, useAerospikeAPI, false);
}
}
}
/// <summary>
/// Generates Aerospike or LINQPad API batch code based on <see cref="Options"/>.
/// </summary>
/// <param name="records">
/// Collection of <see cref="ARecord"/> that will be used to generate batch API Code.
/// </param>
/// <param name="codeOptions">
/// Code Generation Options.
/// </param>
/// <param name="useAerospikeAPI">
/// if set to <c>true</c>, the code generated will be based on the native Aerospike API driver.
/// </param>
/// <returns>
/// Returns a collection of generated API code where each element is a separate Batch statement.
/// </returns>
/// <seealso cref="ToAPICodeBatch(IEnumerable{ARecord}, Options, bool)"/>
/// <seealso cref="ToAPICode(ARecord, Options, bool)"/>
public static IEnumerable<string> ToAPICodeBatch(this IEnumerable<ARecord> records,
Options codeOptions = Options.GetPutCode,
bool useAerospikeAPI = false)
{
if(Options.None == codeOptions) return Enumerable.Empty<string>();
var batchCode = new List<string>();
if(useAerospikeAPI)
{
if(codeOptions.HasFlag(Options.GetCode))
{
var strBatch = new StringBuilder();
var keys = new List<string>();
foreach(var record in records)
{
keys.Add(GenerateCode(record, Options.GetCode, true, true));
}
strBatch.AppendLine("ASClient.Get(null, new List<BatchRead>(){");
strBatch.AppendJoin(",\r\n", keys);
strBatch.Append("})");
batchCode.Add(strBatch.ToString());
}
if(codeOptions.HasFlag(Options.PutCode))
{
var strBatch = new StringBuilder();
var pairRecs = new List<string>();
foreach(var record in records)
{
pairRecs.Add(GenerateCode(record, Options.PutCode, true, true));
}
strBatch.AppendLine("ASClient.Operate(null, new List<BatchRecord>(){");
strBatch.AppendJoin(",\r\n", pairRecs);
strBatch.Append("})");
batchCode.Add(strBatch.ToString());
}
}
else
{
var grouping = records.GroupBy(s => (s.Aerospike.Namespace, s.Aerospike.SetName));
foreach(var group in grouping)
{
var safeNSName = Helpers.CheckName(group.Key.Namespace, "Namespace");
var safeSetName = Helpers.CheckName(group.Key.SetName, "Set");
if(codeOptions.HasFlag(Options.GetCode))
{
var strBatch = new StringBuilder();
var keys = new List<string>();
foreach(var record in group)
{
keys.Add(GenerateCode(record, Options.GetCode, false, true));
}
strBatch.AppendLine($"{safeNSName}.{safeSetName}.BatchRead(new object[] {{");
strBatch.AppendJoin(",\r\n", keys);
strBatch.Append("})");
batchCode.Add(strBatch.ToString());
}
if(codeOptions.HasFlag(Options.PutCode))
{
var strBatch = new StringBuilder();
var pairRecs = new List<string>();
foreach(var record in group)
{
pairRecs.Add(GenerateCode(record, Options.PutCode, false, true));
}
strBatch.AppendLine($"{safeNSName}.{safeSetName}.BatchWrite(new (object key,IDictionary<string,object> binvaluePair)[] {{");
strBatch.AppendJoin(",\r\n", pairRecs);
strBatch.Append("})");
batchCode.Add(strBatch.ToString());
}
}
}
return batchCode;
}
/// <summary>
/// Generates a Aerospike or LINQPad API code segment.
/// </summary>
/// <param name="record">
/// A record used to generate the code segment.
/// </param>
/// <param name="codeOptions">
/// Code Generation Options.
/// </param>
/// <param name="useAerospikeAPI">
/// if set to <c>true</c>, the code generated will be based on the native Aerospike API driver.
/// </param>
/// <returns>
/// Returns generated API code based on <paramref name="record"/>.
/// The returned generated code may only be a code segment.
/// </returns>
/// <seealso cref="ToAPICodeBatch(IEnumerable{ARecord}, Options, bool)"/>
/// <seealso cref="ToAPICode(IEnumerable{ARecord}, Options, bool)"/>
public static string ToAPICode(this ARecord record,
Options codeOptions = Options.PutCode,
bool useAerospikeAPI = false) => GenerateCode(record, codeOptions, useAerospikeAPI, false);
}
}