Repository navigation
Expand file tree
/
Copy pathValueContainer.cs
More file actions
475 lines (417 loc) · 16 KB
/
Copy pathValueContainer.cs
File metadata and controls
475 lines (417 loc) · 16 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ExpressionEngine
{
[JsonConverter(typeof(ValueContainerConverter))]
public class ValueContainer : IComparable, IEquatable<ValueContainer>
{
private readonly dynamic _value; // TODO: Consider changing this to object - CLR TYPE
private readonly ValueType _type;
public ValueContainer(string value, bool tryToParse = false)
{
if (value == null)
{
_value = null;
_type = ValueType.Null;
return;
}
if (tryToParse)
{
if (int.TryParse(value, out var iValue))
{
_value = iValue;
_type = ValueType.Integer;
}
else if (float.TryParse(value, out var fValue))
{
_value = fValue;
_type = ValueType.Float;
}
else if (bool.TryParse(value, out var bValue))
{
_value = bValue;
_type = ValueType.Boolean;
}
else
{
_value = value;
_type = ValueType.String;
}
}
else
{
_value = value;
_type = ValueType.String;
}
}
public ValueContainer(string stringValue)
{
_value = stringValue;
_type = ValueType.String;
}
public ValueContainer(float floatValue)
{
_value = Convert.ToDouble(floatValue);
_type = ValueType.Float;
}
public ValueContainer(double floatValue)
{
_value = floatValue;
_type = ValueType.Float;
}
public ValueContainer(int intValue)
{
_value = intValue;
_type = ValueType.Integer;
}
public ValueContainer(bool boolValue)
{
_value = boolValue;
_type = ValueType.Boolean;
}
public ValueContainer(IEnumerable<ValueContainer> arrayValue)
{
_value = arrayValue;
_type = ValueType.Array;
}
internal ValueContainer(Dictionary<string, ValueContainer> objectValue, bool normalize)
{
_value = normalize ? objectValue.Normalize() : objectValue;
_type = ValueType.Object;
}
public ValueContainer(Dictionary<string, ValueContainer> objectValue)
{
_value = objectValue.Normalize();
_type = ValueType.Object;
}
public ValueContainer(ValueContainer valueContainer)
{
_type = valueContainer._type;
_value = valueContainer._value;
}
public ValueContainer()
{
_type = ValueType.Null;
_value = null;
}
public ValueContainer(JToken json, IExpressionEngine expressionEngine = null)
{
if (json == null)
{
_type = ValueType.Null;
_value = null;
return;
}
var v = JsonToValueContainer(json, expressionEngine);
_type = v._type;
_value = v._value;
}
public ValueType Type()
{
return _type;
}
public enum ValueType
{
Boolean,
Integer,
Float,
String,
Object,
Array,
Null
}
public T GetValue<T>()
{
return _value;
}
public ValueContainer this[int i]
{
get
{
if (_type != ValueType.Array)
{
throw new InvalidOperationException("Index operations can only be performed on arrays.");
}
return ((List<ValueContainer>) _value)[i];
}
set
{
if (_type != ValueType.Array)
{
throw new InvalidOperationException("Index operations can only be performed on arrays.");
}
((List<ValueContainer>) _value)[i] = value;
}
}
public ValueContainer this[string key]
{
get
{
if (_type != ValueType.Object)
{
throw new InvalidOperationException("Index operations can only be performed on objects.");
}
var keyPath = key.Split('/');
var current = GetValue<Dictionary<string, ValueContainer>>()[keyPath.First()];
foreach (var xKey in keyPath.Skip(1))
{
current = current.GetValue<Dictionary<string, ValueContainer>>()[xKey]; // Does not
}
return current;
}
set
{
if (_type != ValueType.Object)
{
throw new InvalidOperationException("Index operations can only be performed on objects.");
}
var keyPath = key.Split('/');
var finalKey = keyPath.Last();
var current = _value;
foreach (var xKey in keyPath.Take(keyPath.Length - 1))
{
var dict = GetValue<Dictionary<string, ValueContainer>>();
var success = dict.TryGetValue(xKey, out var temp);
if (success)
{
current = temp;
}
else
{
dict[xKey] = new ValueContainer(new Dictionary<string, ValueContainer>());
current = dict[xKey].GetValue<Dictionary<string, ValueContainer>>();
}
}
current[finalKey] = value;
}
}
public Dictionary<string, ValueContainer> AsDict()
{
if (_type == ValueType.Object)
{
return GetValue<Dictionary<string, ValueContainer>>();
}
throw new PowerAutomateMockUpException("Can't get none object value container as dict.");
}
private ValueContainer JsonToValueContainer(JToken json, IExpressionEngine expressionEngine)
{
switch (json)
{
case JObject _:
{
var dictionary = json.ToDictionary(pair => ((JProperty) pair).Name, token =>
{
if (token.Children().Count() != 1)
return JsonToValueContainer(token.Children().First(), expressionEngine);
var t = token.First;
return t.Type switch
{
JTokenType.String => expressionEngine?.ParseToValueContainer(t.Value<string>()) ??
new ValueContainer(t.Value<string>()),
JTokenType.Boolean => new ValueContainer(t.Value<bool>()),
JTokenType.Integer => new ValueContainer(t.Value<int>()),
JTokenType.Float => new ValueContainer(t.Value<float>()),
_ => JsonToValueContainer(token.Children().First(), expressionEngine)
};
});
return new ValueContainer(dictionary);
}
case JArray jArray:
return jArray.Count > 0 ? new ValueContainer() : JArrayToValueContainer(jArray);
case JValue jValue:
if (jValue.HasValues)
{
throw new PowerAutomateMockUpException(
"When parsing JToken to ValueContainer, the JToken as JValue can only contain one value.");
}
return jValue.Type switch
{
JTokenType.Boolean => new ValueContainer(jValue.Value<bool>()),
JTokenType.Integer => new ValueContainer(jValue.Value<int>()),
JTokenType.Float => new ValueContainer(jValue.Value<float>()),
JTokenType.Null => new ValueContainer(),
JTokenType.String => expressionEngine?.ParseToValueContainer(jValue.Value<string>()) ??
new ValueContainer(jValue.Value<string>()),
JTokenType.None => new ValueContainer(),
JTokenType.Guid => new ValueContainer(jValue.Value<Guid>().ToString()),
_ => throw new PowerAutomateMockUpException(
$"{jValue.Type} is not yet supported in ValueContainer conversion")
};
default:
throw new PowerAutomateMockUpException("Could not parse JToken to ValueContainer.");
}
}
private ValueContainer JArrayToValueContainer(JArray json)
{
var list = new List<ValueContainer>();
foreach (var jToken in json)
{
if (jToken.GetType() != typeof(JValue))
{
throw new PowerAutomateMockUpException("Json can only contain arrays of primitive types.");
}
var t = (JValue) jToken;
switch (t.Value)
{
case int i:
list.Add(new ValueContainer(i));
break;
case string s:
list.Add(new ValueContainer(s));
break;
case bool b:
list.Add(new ValueContainer(b));
break;
case double d:
list.Add(new ValueContainer(d));
break;
default:
throw new PowerAutomateMockUpException(
$"Type {t.Value.GetType()} is not recognized when converting Json to ValueContainer.");
}
}
return new ValueContainer(list);
}
public override string ToString()
{
/*
* This is called when debugging to display text in the variable overview.
* This is also used when parsing.
*/
return _type switch
{
ValueType.Boolean => _value.ToString(),
ValueType.Integer => _value.ToString(),
ValueType.Float => _value.ToString(),
ValueType.String => _value,
ValueType.Object => "{" + string.Join(",", GetValue<Dictionary<string, ValueContainer>>()
.Select(kv => kv.Key + "=" + kv.Value).ToArray()) + "}",
ValueType.Array => "[" + string.Join(", ", GetValue<IEnumerable<ValueContainer>>().ToList()) + "]",
ValueType.Null => "<null>",
_ => throw new ArgumentOutOfRangeException()
};
}
public bool IsNull()
{
return _type == ValueType.Null;
}
public int CompareTo(object? obj)
{
if (obj == null || obj.GetType() != GetType())
throw new InvalidOperationException("Cannot compare these two...");
var other = (ValueContainer) obj;
if (other.Type() != _type)
{
// TODO: Fix comparison
throw new InvalidOperationException("Cannot compare two different ValueContainers");
}
else
{
switch (_value)
{
case bool b:
return b.CompareTo(other._value);
case int i:
return i.CompareTo(other._value);
case float f:
return f.CompareTo(other._value);
case double f:
return f.CompareTo(other._value);
case decimal f:
return f.CompareTo(other._value);
case string s:
return s.CompareTo(other._value);
case Dictionary<string, ValueContainer> d:
var d2 = (Dictionary<string, ValueContainer>) other._value;
return d.Count - d2.Count;
case IEnumerable<ValueContainer> l:
var l2 = (IEnumerable<ValueContainer>) other._value;
return l.Count() - l2.Count();
default:
return -1;
}
}
}
public bool Equals(ValueContainer other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
switch (_type)
{
case ValueType.Array when other._type == ValueType.Array:
{
var thisArray = (IEnumerable<ValueContainer>) _value;
var otherArray = other.GetValue<IEnumerable<ValueContainer>>();
return thisArray.SequenceEqual(otherArray);
}
case ValueType.Object when other._type == ValueType.Object:
{
var thisDict = (Dictionary<string, ValueContainer>) _value;
var otherDict = other.GetValue<Dictionary<string, ValueContainer>>();
return thisDict.Count == otherDict.Count && !thisDict.Except(otherDict).Any();
}
case ValueType.Integer when other._type == ValueType.Float:
var v = (double) _value;
return Math.Abs(Math.Floor(v) - other._value) < double.Epsilon;
case ValueType.Float when other._type == ValueType.Integer:
{
return Math.Abs(Math.Floor(_value) - other._value) < double.Epsilon;
}
case ValueType.Float:
{
// TODO: Figure out how to handle comparison and in general how to handle float/double..
// assignee: thygesteffensen
return Math.Abs(_value - other._value) < 0.01;
}
default:
return Equals(_value, other._value) && _type == other._type;
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((ValueContainer) obj);
}
public override int GetHashCode()
{
return new {_type, _value}.GetHashCode();
}
public bool ContainsKey(string key)
{
if (_type != ValueType.Object) return false;
var current = AsDict();
var keys = key.Split('/');
for (var i = 0; i < keys.Length - 1; i++)
{
if (current.ContainsKey(keys[i]))
{
current = current[keys[i]].AsDict();
}
else
{
return false;
}
}
return current.ContainsKey(keys[keys.Length-1]);
}
}
public class ValueContainerComparer : EqualityComparer<ValueContainer>
{
public override bool Equals(ValueContainer x, ValueContainer y)
{
if (x == null || y == null)
{
return x == null && y == null;
}
return x.Equals(y);
}
public override int GetHashCode(ValueContainer obj)
{
return obj.GetHashCode();
}
}
}