forked from akkadotnet/Akka.Management
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
56 lines (47 loc) · 2.02 KB
/
Extensions.cs
File metadata and controls
56 lines (47 loc) · 2.02 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
//-----------------------------------------------------------------------
// <copyright file="Extensions.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System.Linq;
using Akka.Configuration.Hocon;
using Akka.Util.Internal;
namespace Akka.Discovery.KubernetesApi
{
internal static class ConfigExtensions
{
public static string? GetStringIfDefined(this Configuration.Config config, string key)
{
var value = config.GetString(key);
return string.IsNullOrWhiteSpace(value) || value.Equals($"<{key}>") ? null : value;
}
internal static Configuration.Config MoveTo(this Configuration.Config config, string path)
{
var rootObj = new HoconObject();
var rootValue = new HoconValue();
rootValue.Values.Add(rootObj);
var lastObject = rootObj;
var keys = path.SplitDottedPathHonouringQuotes().ToArray();
for (var i = 0; i < keys.Length - 1; i++)
{
var key = keys[i];
var innerObject = new HoconObject();
var innerValue = new HoconValue();
innerValue.Values.Add(innerObject);
lastObject.GetOrCreateKey(key);
lastObject.Items[key] = innerValue;
lastObject = innerObject;
}
lastObject.Items[keys[keys.Length - 1]] = config.Root;
return new Configuration.Config(new HoconRoot(rootValue));
}
}
internal static class ObjectExtensions
{
public static T GetOrElse<T>(this T obj, T @default)
=> obj is null ? @default : obj;
public static string? DefaultIfNullOrWhitespace(this string? str, string? @default)
=> string.IsNullOrWhiteSpace(str) ? @default : str;
}
}