/
HGM.GraphQL.Types.pas
378 lines (309 loc) · 9.46 KB
/
HGM.GraphQL.Types.pas
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
unit HGM.GraphQL.Types;
interface
uses
System.StrUtils, System.SysUtils, System.Generics.Collections;
type
TGraphArgList = class;
TGraphArgString = class;
TGraphArgInteger = class;
TGraphArgBoolean = class;
TGraphArgFloat = class;
TGraphArgVar = class;
TGraphArgValue = class abstract
public
function ToString: string; reintroduce; virtual; abstract;
end;
TGraphArgItem<T> = class abstract(TGraphArgValue)
private
FValue: T;
public
constructor Create(const AValue: T); reintroduce;
end;
TGraphArg = class
private
FName: string;
FValue: TGraphArgValue;
procedure SetValue(const Value: TGraphArgValue);
procedure SetName(const Value: string);
public
class function Create(const Name: string; const Value: string; IsVar: Boolean = False): TGraphArg; overload;
class function Create(const Name: string; const Value: Integer): TGraphArg; overload;
class function Create(const Name: string; const Value: Extended): TGraphArg; overload;
class function Create(const Name: string; const Value: Boolean): TGraphArg; overload;
class function Create(const Name: string; const Value: TGraphArgValue): TGraphArg; overload;
class function Create(const Name: string; const Value: TArray<Integer>): TGraphArg; overload;
public
property Name: string read FName write SetName;
property Value: TGraphArgValue read FValue write SetValue;
function ToString: string; reintroduce; virtual;
constructor Create(const AName: string = ''); reintroduce; overload;
destructor Destroy; override;
end;
TGraphArgObject = class(TGraphArgValue)
private
FItems: TObjectList<TGraphArg>;
procedure SetItems(const Value: TObjectList<TGraphArg>);
public
function AddPair(const Name: string; const Value: string; AsVar: Boolean = False): TGraphArgObject; overload;
function AddPair(const Name: string; const Value: Integer): TGraphArgObject; overload;
function AddPair(const Name: string; const Value: Extended): TGraphArgObject; overload;
function AddPair(const Name: string; const Value: Boolean): TGraphArgObject; overload;
function AddPair(const Name: string; const Value: TGraphArgValue): TGraphArgObject; overload;
function AddPair(const Name: string; const Value: TArray<Integer>): TGraphArgObject; overload;
public
function ToString: string; override;
property Items: TObjectList<TGraphArg> read FItems write SetItems;
constructor Create; overload;
constructor Create(Values: array of TGraphArg); overload;
destructor Destroy; override;
end;
TGraphArgList = class(TObjectList<TGraphArg>)
public
function AddPair(const Name: string; const Value: string; AsVar: Boolean = False): TGraphArgList; overload;
function AddPair(const Name: string; const Value: Integer): TGraphArgList; overload;
function AddPair(const Name: string; const Value: Extended): TGraphArgList; overload;
function AddPair(const Name: string; const Value: Boolean): TGraphArgList; overload;
function AddPair(const Name: string; const Value: TGraphArgValue): TGraphArgList; overload;
function AddPair(const Name: string; const Value: TArray<Integer>): TGraphArgList; overload;
public
constructor Create; reintroduce;
function ToString: string; reintroduce; virtual;
end;
TGraphArgString = class(TGraphArgItem<string>)
public
function ToString: string; override;
end;
TGraphArgInteger = class(TGraphArgItem<Integer>)
public
function ToString: string; override;
end;
TGraphArgIntegerArray = class(TGraphArgItem<TArray<Integer>>)
public
function ToString: string; override;
end;
TGraphArgBoolean = class(TGraphArgItem<Boolean>)
public
function ToString: string; override;
end;
TGraphArgFloat = class(TGraphArgItem<Extended>)
public
function ToString: string; override;
end;
TGraphArgVar = class(TGraphArgItem<string>)
public
function ToString: string; override;
end;
implementation
{ TGraphArg }
class function TGraphArg.Create(const Name, Value: string; IsVar: Boolean): TGraphArg;
begin
Result := TGraphArg.Create;
if IsVar then
Result.Value := TGraphArgVar.Create(Value)
else
Result.Value := TGraphArgString.Create(Value);
Result.Name := Name;
end;
class function TGraphArg.Create(const Name: string; const Value: Integer): TGraphArg;
begin
Result := TGraphArg.Create;
Result.Value := TGraphArgInteger.Create(Value);
Result.Name := Name;
end;
class function TGraphArg.Create(const Name: string; const Value: Extended): TGraphArg;
begin
Result := TGraphArg.Create;
Result.Value := TGraphArgFloat.Create(Value);
Result.Name := Name;
end;
class function TGraphArg.Create(const Name: string; const Value: Boolean): TGraphArg;
begin
Result := TGraphArg.Create;
Result.Value := TGraphArgBoolean.Create(Value);
Result.Name := Name;
end;
class function TGraphArg.Create(const Name: string; const Value: TGraphArgValue): TGraphArg;
begin
Result := TGraphArg.Create;
Result.Value := Value;
Result.Name := Name;
end;
class function TGraphArg.Create(const Name: string; const Value: TArray<Integer>): TGraphArg;
begin
Result := TGraphArg.Create;
Result.Value := TGraphArgIntegerArray.Create(Value);
Result.Name := Name;
end;
destructor TGraphArg.Destroy;
begin
if Assigned(FValue) then
begin
FValue.Free;
FValue := nil;
end;
inherited;
end;
procedure TGraphArg.SetName(const Value: string);
begin
FName := Value;
end;
procedure TGraphArg.SetValue(const Value: TGraphArgValue);
begin
FValue := Value;
end;
function TGraphArg.ToString: string;
begin
Result := Name + ': ' + FValue.ToString;
end;
{ TGraphArgs }
function TGraphArgObject.AddPair(const Name, Value: string; AsVar: Boolean): TGraphArgObject;
begin
FItems.Add(TGraphArg.Create(Name, Value, AsVar));
Result := Self;
end;
function TGraphArgObject.AddPair(const Name: string; const Value: Integer): TGraphArgObject;
begin
FItems.Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
function TGraphArgObject.AddPair(const Name: string; const Value: Extended): TGraphArgObject;
begin
FItems.Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
function TGraphArgObject.AddPair(const Name: string; const Value: Boolean): TGraphArgObject;
begin
FItems.Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
function TGraphArgObject.AddPair(const Name: string; const Value: TGraphArgValue): TGraphArgObject;
begin
FItems.Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
function TGraphArgObject.AddPair(const Name: string; const Value: TArray<Integer>): TGraphArgObject;
begin
FItems.Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
constructor TGraphArgObject.Create;
begin
inherited Create;
FItems := TObjectList<TGraphArg>.Create(True);
end;
constructor TGraphArgObject.Create(Values: array of TGraphArg);
begin
Create;
FItems.AddRange(Values);
end;
destructor TGraphArgObject.Destroy;
begin
FItems.Free;
inherited;
end;
procedure TGraphArgObject.SetItems(const Value: TObjectList<TGraphArg>);
begin
FItems := Value;
end;
function TGraphArgObject.ToString: string;
var
i: Integer;
begin
for i := 0 to Pred(FItems.Count) do
Result := Result + FItems[i].ToString + ', ';
Result := '{' + Result.TrimRight([',', ' ']) + '}';
end;
constructor TGraphArg.Create(const AName: string);
begin
FName := AName;
end;
{ TGraphArgString }
function TGraphArgString.ToString: string;
begin
Result := '"' + FValue + '"';
end;
{ TGraphArgList }
function TGraphArgList.AddPair(const Name, Value: string; AsVar: Boolean): TGraphArgList;
begin
Add(TGraphArg.Create(Name, Value, AsVar));
Result := Self;
end;
function TGraphArgList.AddPair(const Name: string; const Value: Integer): TGraphArgList;
begin
Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
function TGraphArgList.AddPair(const Name: string; const Value: Extended): TGraphArgList;
begin
Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
function TGraphArgList.AddPair(const Name: string; const Value: Boolean): TGraphArgList;
begin
Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
function TGraphArgList.AddPair(const Name: string; const Value: TGraphArgValue): TGraphArgList;
begin
Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
function TGraphArgList.AddPair(const Name: string; const Value: TArray<Integer>): TGraphArgList;
begin
Add(TGraphArg.Create(Name, Value));
Result := Self;
end;
constructor TGraphArgList.Create;
begin
inherited Create;
end;
function TGraphArgList.ToString: string;
var
i: Integer;
begin
for i := 0 to Pred(Count) do
Result := Result + Items[i].ToString + ', ';
Result := '(' + Result.TrimRight([',', ' ']) + ')';
end;
{ TGraphArgItem<T> }
constructor TGraphArgItem<T>.Create(const AValue: T);
begin
FValue := AValue;
end;
{ TGraphArgInteger }
function TGraphArgInteger.ToString: string;
begin
Result := FValue.ToString;
end;
{ TGraphArgBoolean }
function TGraphArgBoolean.ToString: string;
begin
if FValue then
Exit('true')
else
Exit('false');
end;
{ TGraphArgFloat }
function TGraphArgFloat.ToString: string;
var
FS: TFormatSettings;
begin
FS := FormatSettings;
FS.DecimalSeparator := '.';
Result := FValue.ToString(FS);
end;
{ TGraphArgVar }
function TGraphArgVar.ToString: string;
begin
Result := FValue;
end;
{ TGraphArgIntegerArray }
function TGraphArgIntegerArray.ToString: string;
var
i: Integer;
begin
for i := 0 to High(FValue) do
Result := Result + FValue[i].ToString + ', ';
Result := '[' + Result.TrimRight([',', ' ']) + ']';
end;
end.