/
HoconValue.cs
554 lines (496 loc) · 21.3 KB
/
HoconValue.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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//-----------------------------------------------------------------------
// <copyright file="HoconValue.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
namespace Akka.Configuration.Hocon
{
/// <summary>
/// This class represents the root type for a HOCON (Human-Optimized Config Object Notation)
/// configuration object.
/// </summary>
public class HoconValue : IMightBeAHoconObject
{
private static readonly Regex EscapeRegex = new("[ \t:]{1}", RegexOptions.Compiled);
private static readonly Regex TimeSpanRegex = new(@"^(?<value>([0-9]+(\.[0-9]+)?))\s*(?<unit>(nanoseconds|nanosecond|nanos|nano|ns|microseconds|microsecond|micros|micro|us|milliseconds|millisecond|millis|milli|ms|seconds|second|s|minutes|minute|m|hours|hour|h|days|day|d))$", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="HoconValue"/> class.
/// </summary>
public HoconValue()
{
Values = new List<IHoconElement>();
}
/// <summary>
/// Initializes a new instance of the <see cref="HoconValue"/> class.
/// </summary>
/// <param name="values">The list of elements inside this HOCON value.</param>
/// <param name="adoptedFromFallback">Indicates whether this instance was constructed during association with fallback <see cref="Config"/>.</param>
public HoconValue(List<IHoconElement> values, bool adoptedFromFallback = true)
{
Values = values;
AdoptedFromFallback = adoptedFromFallback;
}
/// <summary>
/// Returns true if this HOCON value doesn't contain any elements
/// </summary>
public bool IsEmpty
{
get
{
if (Values.Count == 0)
return true;
var first = Values[0] as HoconObject;
if (first != null)
{
if (first.Items.Count == 0)
return true;
}
return false;
}
}
/// <summary>
/// The list of elements inside this HOCON value
/// </summary>
public List<IHoconElement> Values { get; private set; }
/// <summary>
/// Marker for values were merged during fallback attaching
/// serving exclusively to skip rendering such values in <see cref="HoconObject.ToString()"/>
/// </summary>
internal bool AdoptedFromFallback { get; private set; }
public Config ToConfig()
{
return new Config(new HoconRoot(this, Enumerable.Empty<HoconSubstitution>()));
}
/// <summary>
/// Wraps this <see cref="HoconValue"/> into a new <see cref="Config"/> object at the specified key.
/// </summary>
/// <param name="key">The key designated to be the new root element.</param>
/// <returns>A <see cref="Config"/> with the given key as the root element.</returns>
public Config AtKey(string key)
{
var o = new HoconObject();
o.GetOrCreateKey(key);
o.Items[key] = this;
var r = new HoconValue();
r.Values.Add(o);
return new Config(new HoconRoot(r));
}
/// <summary>
/// Retrieves the <see cref="HoconObject"/> from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The <see cref="HoconObject"/> that represents this <see cref="HoconValue"/>.</returns>
public HoconObject GetObject()
{
//TODO: merge objects?
IHoconElement raw = Values.FirstOrDefault();
var o = raw as HoconObject;
var sub = raw as IMightBeAHoconObject;
if (o != null) return o;
if (sub != null && sub.IsObject()) return sub.GetObject();
return null;
}
/// <summary>
/// Determines if this <see cref="HoconValue"/> is a <see cref="HoconObject"/>.
/// </summary>
/// <returns><c>true</c> if this value is a <see cref="HoconObject"/>, <c>false</c> otherwise.</returns>
public bool IsObject()
{
return GetObject() != null;
}
/// <summary>
/// Adds the given element to the list of elements inside this <see cref="HoconValue"/>.
/// </summary>
/// <param name="value">The element to add to the list.</param>
public void AppendValue(IHoconElement value)
{
Values.Add(value);
}
/// <summary>
/// Clears the list of elements inside this <see cref="HoconValue"/>.
/// </summary>
public void Clear()
{
Values.Clear();
}
/// <summary>
/// Creates a fresh list of elements inside this <see cref="HoconValue"/>
/// and adds the given value to the list.
/// </summary>
/// <param name="value">The element to add to the list.</param>
public void NewValue(IHoconElement value)
{
Values.Clear();
Values.Add(value);
}
/// <summary>
/// Determines whether all the elements inside this <see cref="HoconValue"/>
/// are a string.
/// </summary>
/// <returns>
/// <c>true</c>if all elements inside this <see cref="HoconValue"/> are a string; otherwise <c>false</c>.
/// </returns>
public bool IsString()
{
return Values.Count > 0 && Values.All(v => v.IsString());
}
private string ConcatString()
{
string concat = string.Join("", Values.Select(l => l.GetString())).Trim();
if (concat == "null")
return null;
return concat;
}
/// <summary>
/// Retrieves the child object located at the given key.
/// </summary>
/// <param name="key">The key used to retrieve the child object.</param>
/// <returns>The element at the given key.</returns>
public HoconValue GetChildObject(string key)
{
return GetObject()?.GetKey(key);
}
/// <summary>
/// Retrieves the boolean value from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The boolean value represented by this <see cref="HoconValue"/>.</returns>
/// <exception cref="NotSupportedException">
/// This exception is thrown if the <see cref="HoconValue"/> doesn't
/// conform to the standard boolean values: "on", "off", "true", or "false"
/// </exception>
public bool GetBoolean()
{
string v = GetString();
switch (v)
{
case "on":
return true;
case "off":
return false;
case "true":
return true;
case "false":
return false;
default:
throw new NotSupportedException($"Unknown boolean format: {v}");
}
}
/// <summary>
/// Retrieves the string value from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The string value represented by this <see cref="HoconValue"/>.</returns>
public string GetString()
{
if (IsString())
{
return ConcatString();
}
return null; //TODO: throw exception?
}
/// <summary>
/// Retrieves the decimal value from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The decimal value represented by this <see cref="HoconValue"/>.</returns>
public decimal GetDecimal()
{
return decimal.Parse(GetString(), NumberFormatInfo.InvariantInfo);
}
/// <summary>
/// Retrieves the float value from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The float value represented by this <see cref="HoconValue"/>.</returns>
public float GetFloat()
{
return float.Parse(GetString(), NumberFormatInfo.InvariantInfo);
}
/// <summary>
/// Retrieves the double value from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The double value represented by this <see cref="HoconValue"/>.</returns>
public double GetDouble()
{
return double.Parse(GetString(), NumberFormatInfo.InvariantInfo);
}
/// <summary>
/// Retrieves the long value from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The long value represented by this <see cref="HoconValue"/>.</returns>
public long GetLong()
{
return long.Parse(GetString(), NumberFormatInfo.InvariantInfo);
}
/// <summary>
/// Retrieves the integer value from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The integer value represented by this <see cref="HoconValue"/>.</returns>
public int GetInt()
{
return int.Parse(GetString(), NumberFormatInfo.InvariantInfo);
}
/// <summary>
/// Retrieves the byte value from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>The byte value represented by this <see cref="HoconValue"/>.</returns>
public byte GetByte()
{
return byte.Parse(GetString(), NumberFormatInfo.InvariantInfo);
}
/// <summary>
/// Retrieves a list of byte values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of byte values represented by this <see cref="HoconValue"/>.</returns>
public IList<byte> GetByteList()
{
return GetArray().Select(v => v.GetByte()).ToList();
}
/// <summary>
/// Retrieves a list of integer values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of integer values represented by this <see cref="HoconValue"/>.</returns>
public IList<int> GetIntList()
{
return GetArray().Select(v => v.GetInt()).ToList();
}
/// <summary>
/// Retrieves a list of long values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of long values represented by this <see cref="HoconValue"/>.</returns>
public IList<long> GetLongList()
{
return GetArray().Select(v => v.GetLong()).ToList();
}
/// <summary>
/// Retrieves a list of boolean values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of boolean values represented by this <see cref="HoconValue"/>.</returns>
public IList<bool> GetBooleanList()
{
return GetArray().Select(v => v.GetBoolean()).ToList();
}
/// <summary>
/// Retrieves a list of float values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of float values represented by this <see cref="HoconValue"/>.</returns>
public IList<float> GetFloatList()
{
return GetArray().Select(v => v.GetFloat()).ToList();
}
/// <summary>
/// Retrieves a list of double values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of double values represented by this <see cref="HoconValue"/>.</returns>
public IList<double> GetDoubleList()
{
return GetArray().Select(v => v.GetDouble()).ToList();
}
/// <summary>
/// Retrieves a list of decimal values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of decimal values represented by this <see cref="HoconValue"/>.</returns>
public IList<decimal> GetDecimalList()
{
return GetArray().Select(v => v.GetDecimal()).ToList();
}
/// <summary>
/// Retrieves a list of string values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of string values represented by this <see cref="HoconValue"/>.</returns>
public IList<string> GetStringList()
{
return GetArray().Select(v => v.GetString()).ToList();
}
/// <summary>
/// Retrieves a list of values from this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A list of values represented by this <see cref="HoconValue"/>.</returns>
public IList<HoconValue> GetArray()
{
IEnumerable<HoconValue> x = from element in Values
where element.IsArray()
from e in element.GetArray()
select e;
return x.ToList();
}
/// <summary>
/// Determines whether this <see cref="HoconValue"/> is an array.
/// </summary>
/// <returns>
/// <c>true</c> if this <see cref="HoconValue"/> is an array; otherwise <c>false</c>.
/// </returns>
public bool IsArray()
{
return GetArray().Count != 0;
}
/// <summary>
/// Retrieves the time span value from this <see cref="HoconValue"/>.
/// </summary>
/// <param name="allowInfinite">A flag used to set infinite durations.</param>
/// <exception cref="FormatException">
/// This exception is thrown if the timespan given in the <see cref="HoconValue"/> is negative.
/// </exception>
/// <returns>The time span value represented by this <see cref="HoconValue"/>.</returns>
public TimeSpan GetTimeSpan(bool allowInfinite = true)
{
string res = GetString();
var match = TimeSpanRegex.Match(res);
if (match.Success)
{
var u = match.Groups["unit"].Value;
var v = ParsePositiveValue(match.Groups["value"].Value);
switch (u)
{
case "nanoseconds":
case "nanosecond":
case "nanos":
case "nano":
case "ns":
return TimeSpan.FromTicks((long) Math.Round(TimeSpan.TicksPerMillisecond * v / 1000000.0));
case "microseconds":
case "microsecond":
case "micros":
case "micro":
return TimeSpan.FromTicks((long) Math.Round(TimeSpan.TicksPerMillisecond * v / 1000.0));
case "milliseconds":
case "millisecond":
case "millis":
case "milli":
case "ms":
return TimeSpan.FromMilliseconds(v);
case "seconds":
case "second":
case "s":
return TimeSpan.FromSeconds(v);
case "minutes":
case "minute":
case "m":
return TimeSpan.FromMinutes(v);
case "hours":
case "hour":
case "h":
return TimeSpan.FromHours(v);
case "days":
case "day":
case "d":
return TimeSpan.FromDays(v);
}
}
if (allowInfinite && res.Equals("infinite", StringComparison.OrdinalIgnoreCase)) //Not in Hocon spec
{
return Timeout.InfiniteTimeSpan;
}
return TimeSpan.FromMilliseconds(ParsePositiveValue(res));
}
private static double ParsePositiveValue(string v)
{
var value = double.Parse(v, NumberFormatInfo.InvariantInfo);
if(value < 0)
throw new FormatException($"Expected a positive value instead of {value}");
return value;
}
private struct ByteSize
{
public long Factor { get; set; }
public string[] Suffixes { get; set; }
}
private static ByteSize[] ByteSizes { get; } =
new ByteSize[]
{
new() { Factor = 1024L * 1024L * 1024L * 1024L * 1024 * 1024L, Suffixes = new string[] { "E", "e", "Ei", "EiB", "exbibyte", "exbibytes" } },
new() { Factor = 1000L * 1000L * 1000L * 1000L * 1000L * 1000L, Suffixes = new string[] { "EB", "exabyte", "exabytes" } },
new() { Factor = 1024L * 1024L * 1024L * 1024L * 1024L, Suffixes = new string[] { "P", "p", "Pi", "PiB", "pebibyte", "pebibytes" } },
new() { Factor = 1000L * 1000L * 1000L * 1000L * 1000L, Suffixes = new string[] { "PB", "petabyte", "petabytes" } },
new() { Factor = 1024L * 1024L * 1024L * 1024L, Suffixes = new string[] { "T", "t", "Ti", "TiB", "tebibyte", "tebibytes" } },
new() { Factor = 1000L * 1000L * 1000L * 1000L, Suffixes = new string[] { "TB", "terabyte", "terabytes" } },
new() { Factor = 1024L * 1024L * 1024L, Suffixes = new string[] { "G", "g", "Gi", "GiB", "gibibyte", "gibibytes" } },
new() { Factor = 1000L * 1000L * 1000L, Suffixes = new string[] { "GB", "gigabyte", "gigabytes" } },
new() { Factor = 1024L * 1024L, Suffixes = new string[] { "M", "m", "Mi", "MiB", "mebibyte", "mebibytes" } },
new() { Factor = 1000L * 1000L, Suffixes = new string[] { "MB", "megabyte", "megabytes" } },
new() { Factor = 1024L, Suffixes = new string[] { "K", "k", "Ki", "KiB", "kibibyte", "kibibytes" } },
new() { Factor = 1000L, Suffixes = new string[] { "kB", "kilobyte", "kilobytes" } },
new() { Factor = 1, Suffixes = new string[] { "b", "B", "byte", "bytes" } }
};
private static char[] Digits { get; } = "0123456789".ToCharArray();
/// <summary>
/// Retrieves the long value, optionally suffixed with a case sensitive
/// <see href="https://github.com/lightbend/config/blob/master/HOCON.md#size-in-bytes-format">byte size suffix</see>, from
/// this <see cref="HoconValue"/>. An empty value results in <see langword="null"/>.
/// </summary>
/// <returns>The long value represented by this <see cref="HoconValue"/>.</returns>
public long? GetByteSize()
{
var res = GetString();
if (string.IsNullOrEmpty(res))
return null;
res = res.Trim();
var index = res.LastIndexOfAny(Digits);
if (index == -1 || index + 1 >= res.Length)
return long.Parse(res);
var value = res.Substring(0, index + 1);
var unit = res.Substring(index + 1).Trim();
for (var byteSizeIndex = 0; byteSizeIndex < ByteSizes.Length; byteSizeIndex++)
{
var byteSize = ByteSizes[byteSizeIndex];
for (var suffixIndex = 0; suffixIndex < byteSize.Suffixes.Length; suffixIndex++)
{
var suffix = byteSize.Suffixes[suffixIndex];
if (string.Equals(unit, suffix, StringComparison.Ordinal))
return byteSize.Factor * long.Parse(value);
}
}
throw new FormatException($"{unit} is not a valid byte size suffix");
}
/// <summary>
/// Returns a HOCON string representation of this <see cref="HoconValue"/>.
/// </summary>
/// <returns>A HOCON string representation of this <see cref="HoconValue"/>.</returns>
public override string ToString()
{
return ToString(0);
}
/// <summary>
/// Returns a HOCON string representation of this <see cref="HoconValue"/>.
/// </summary>
/// <param name="indent">The number of spaces to indent the string.</param>
/// <returns>A HOCON string representation of this <see cref="HoconValue"/>.</returns>
public virtual string ToString(int indent)
{
if (IsString())
{
string text = QuoteIfNeeded(GetString());
return text;
}
if (IsObject())
{
if (indent == 0)
{
return GetObject().ToString(indent + 1);
}
else
{
var i = new string(' ', indent * 2);
return string.Format("{{\r\n{1}{0}}}", i, GetObject().ToString(indent + 1));
}
}
if (IsArray())
{
return string.Format("[{0}]", string.Join(",", GetArray().Select(e => e.ToString(indent + 1))));
}
return "<<unknown value>>";
}
private string QuoteIfNeeded(string text)
{
if (text == null) return "";
if (EscapeRegex.IsMatch(text))
{
return "\"" + text + "\"";
}
return text;
}
}
}