Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EnvironmentSettings.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Content;
5
7{
13 public static class EnvironmentSettings
14 {
15 #region String-valued settings
16
24 public static string Get(string EnvironmentVariable, string Key, string DefaultValue)
25 {
26 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
27 }
28
36 public static Task<string> GetAsync(string EnvironmentVariable, string Key, string DefaultValue)
37 {
38 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
39 if (s is null)
40 return RuntimeSettings.GetAsync(Key, DefaultValue);
41 else
42 return Task.FromResult(s);
43 }
44
45 #endregion
46
47 #region Int64-valued settings
48
56 public static long Get(string EnvironmentVariable, string Key, long DefaultValue)
57 {
58 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
59 }
60
68 public static Task<long> GetAsync(string EnvironmentVariable, string Key, long DefaultValue)
69 {
70 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
71 if (s is null || !long.TryParse(s, out long Result))
72 return RuntimeSettings.GetAsync(Key, DefaultValue);
73 else
74 return Task.FromResult(Result);
75 }
76
77 #endregion
78
79 #region Boolean-valued settings
80
88 public static bool Get(string EnvironmentVariable, string Key, bool DefaultValue)
89 {
90 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
91 }
92
100 public static Task<bool> GetAsync(string EnvironmentVariable, string Key, bool DefaultValue)
101 {
102 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
103 if (s is null || !CommonTypes.TryParse(s, out bool Result))
104 return RuntimeSettings.GetAsync(Key, DefaultValue);
105 else
106 return Task.FromResult(Result);
107 }
108
109 #endregion
110
111 #region DateTime-valued settings
112
120 public static DateTime Get(string EnvironmentVariable, string Key, DateTime DefaultValue)
121 {
122 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
123 }
124
132 public static Task<DateTime> GetAsync(string EnvironmentVariable, string Key, DateTime DefaultValue)
133 {
134 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
135 if (s is null)
136 return RuntimeSettings.GetAsync(Key, DefaultValue);
137 else if (CommonTypes.TryParseRfc822(s, out DateTimeOffset Result))
138 return Task.FromResult(Result.LocalDateTime);
139 else if (XML.TryParse(s, out DateTime Result2))
140 return Task.FromResult(Result2);
141 else if (DateTime.TryParse(s, out Result2))
142 return Task.FromResult(Result2);
143 else
144 return RuntimeSettings.GetAsync(Key, DefaultValue);
145 }
146
147 #endregion
148
149 #region TimeSpan-valued settings
150
158 public static TimeSpan Get(string EnvironmentVariable, string Key, TimeSpan DefaultValue)
159 {
160 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
161 }
162
170 public static Task<TimeSpan> GetAsync(string EnvironmentVariable, string Key, TimeSpan DefaultValue)
171 {
172 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
173 if (s is null || !TimeSpan.TryParse(s, out TimeSpan Result))
174 return RuntimeSettings.GetAsync(Key, DefaultValue);
175 else
176 return Task.FromResult(Result);
177 }
178
179 #endregion
180
181 #region Double-valued settings
182
190 public static double Get(string EnvironmentVariable, string Key, double DefaultValue)
191 {
192 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
193 }
194
202 public static Task<double> GetAsync(string EnvironmentVariable, string Key, double DefaultValue)
203 {
204 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
205 if (s is null || !CommonTypes.TryParse(s, out double Result))
206 return RuntimeSettings.GetAsync(Key, DefaultValue);
207 else
208 return Task.FromResult(Result);
209 }
210
211 #endregion
212
213 #region Enum-valued settings
214
222 public static Enum Get(string EnvironmentVariable, string Key, Enum DefaultValue)
223 {
224 return GetAsync(EnvironmentVariable, Key, DefaultValue).Result;
225 }
226
234 public static Task<Enum> GetAsync(string EnvironmentVariable, string Key, Enum DefaultValue)
235 {
236 string s = Environment.GetEnvironmentVariable(EnvironmentVariable);
237 if (s is null)
238 return RuntimeSettings.GetAsync(Key, DefaultValue);
239
240 Type T = DefaultValue.GetType();
241 string[] Names = Enum.GetNames(T);
242 if (Array.IndexOf(Names, s) < 0)
243 return RuntimeSettings.GetAsync(Key, DefaultValue);
244
245 return Task.FromResult((Enum)Enum.Parse(T, s));
246 }
247
248 #endregion
249 }
250}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParseRfc822(string s, out DateTimeOffset Value)
Parses a date and time value encoded according to RFC 822, §5.
Definition: CommonTypes.cs:170
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:744
Static class managing persistent environment settings. Environment settings default to runtime settin...
static DateTime Get(string EnvironmentVariable, string Key, DateTime DefaultValue)
Gets a DateTime-valued setting.
static Enum Get(string EnvironmentVariable, string Key, Enum DefaultValue)
Gets a Enum-valued setting.
static Task< bool > GetAsync(string EnvironmentVariable, string Key, bool DefaultValue)
Gets a bool-valued setting.
static Task< Enum > GetAsync(string EnvironmentVariable, string Key, Enum DefaultValue)
Gets a Enum-valued setting.
static Task< TimeSpan > GetAsync(string EnvironmentVariable, string Key, TimeSpan DefaultValue)
Gets a TimeSpan-valued setting.
static long Get(string EnvironmentVariable, string Key, long DefaultValue)
Gets a long-valued setting.
static Task< double > GetAsync(string EnvironmentVariable, string Key, double DefaultValue)
Gets a double-valued setting.
static Task< string > GetAsync(string EnvironmentVariable, string Key, string DefaultValue)
Gets a string-valued setting.
static bool Get(string EnvironmentVariable, string Key, bool DefaultValue)
Gets a bool-valued setting.
static double Get(string EnvironmentVariable, string Key, double DefaultValue)
Gets a double-valued setting.
static string Get(string EnvironmentVariable, string Key, string DefaultValue)
Gets a string-valued setting.
static Task< DateTime > GetAsync(string EnvironmentVariable, string Key, DateTime DefaultValue)
Gets a DateTime-valued setting.
static TimeSpan Get(string EnvironmentVariable, string Key, TimeSpan DefaultValue)
Gets a TimeSpan-valued setting.
static Task< long > GetAsync(string EnvironmentVariable, string Key, long DefaultValue)
Gets a long-valued setting.
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.