-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathConfiguration.cs
More file actions
235 lines (199 loc) · 8.55 KB
/
Configuration.cs
File metadata and controls
235 lines (199 loc) · 8.55 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
using LanguageExt;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using Yaml2JsonNode;
using YamlDotNet.RepresentationModel;
namespace common;
public record ConfigurationJson
{
private static readonly JsonNodeOptions nodeOptions = new() { PropertyNameCaseInsensitive = true };
public required JsonObject Value { get; init; }
public static ConfigurationJson From(IConfiguration configuration) =>
new()
{
Value = SerializeConfiguration(configuration) is JsonObject configurationJsonObject
? configurationJsonObject
: new JsonObject(nodeOptions)
};
private static JsonNode? SerializeConfiguration(IConfiguration configuration)
{
var jsonObject = new JsonObject(nodeOptions);
foreach (var child in configuration.GetChildren())
{
if (child.Path.EndsWith(":0", StringComparison.Ordinal))
{
var jsonArray = new JsonArray(nodeOptions);
foreach (var arrayChild in configuration.GetChildren())
{
jsonArray.Add(SerializeConfiguration(arrayChild));
}
return jsonArray;
}
else
{
jsonObject.Add(child.Key, SerializeConfiguration(child));
}
}
if (jsonObject.Count == 0 && configuration is IConfigurationSection configurationSection)
{
string? sectionValue = configurationSection.Value;
if (bool.TryParse(sectionValue, out var boolValue))
{
return JsonValue.Create(boolValue);
}
else if (decimal.TryParse(sectionValue, out var decimalValue))
{
return JsonValue.Create(decimalValue);
}
else if (long.TryParse(sectionValue, out var longValue))
{
return JsonValue.Create(longValue);
}
else
{
return JsonValue.Create(sectionValue);
}
}
else
{
return jsonObject;
}
}
public static ConfigurationJson FromYaml(TextReader textReader) =>
new()
{
Value = YamlToJson(textReader)
};
private static JsonObject YamlToJson(TextReader reader)
{
var yamlStream = new YamlStream();
yamlStream.Load(reader);
return yamlStream.Documents switch
{
[] => new JsonObject(nodeOptions),
[var document] => document.ToJsonNode()?.AsObject() ?? throw new JsonException("Failed to convert YAML to JSON."),
_ => throw new JsonException("More than one YAML document was found.")
};
}
public ConfigurationJson MergeWith(ConfigurationJson other) =>
new()
{
Value = OverrideWith(Value, other.Value)
};
private static JsonObject OverrideWith(JsonObject current, JsonObject other)
{
var merged = new JsonObject(nodeOptions);
foreach (var property in current)
{
string propertyName = property.Key;
var currentPropertyValue = property.Value;
if (other.TryGetPropertyValue(propertyName, out var otherPropertyValue))
{
if (currentPropertyValue is JsonObject currentObject && otherPropertyValue is JsonObject otherObject)
{
merged[propertyName] = OverrideWith(currentObject, otherObject);
}
else
{
merged[propertyName] = otherPropertyValue?.DeepClone();
}
}
else
{
merged[propertyName] = currentPropertyValue?.DeepClone();
}
}
foreach (var property in other)
{
string propertyName = property.Key;
if (current.ContainsKey(propertyName) is false)
{
merged[propertyName] = property.Value?.DeepClone();
}
}
return merged;
}
}
public static class ConfigurationExtensions
{
public static string GetValue(this IConfiguration configuration, string key) =>
configuration.TryGetValue(key)
.IfNone(() => throw new KeyNotFoundException($"Could not find '{key}' in configuration."));
public static Option<string> TryGetValue(this IConfiguration configuration, string key) =>
configuration.TryGetSection(key)
.Where(section => section.Value is not null)
.Select(section => section.Value!);
public static Option<IConfigurationSection> TryGetSection(this IConfiguration configuration, string key)
{
ArgumentNullException.ThrowIfNull(configuration);
var section = configuration.GetSection(key);
return section.Exists()
? Option<IConfigurationSection>.Some(section)
: Option<IConfigurationSection>.None;
}
public static IConfigurationBuilder AddUserSecretsWithLowestPriority(this IConfigurationBuilder builder, Assembly assembly, bool optional = true) =>
builder.AddWithLowestPriority(b => b.AddUserSecrets(assembly, optional));
private static IConfigurationBuilder AddWithLowestPriority(this IConfigurationBuilder builder, Func<IConfigurationBuilder, IConfigurationBuilder> adder)
{
// Configuration sources added last have the highest priority. We empty existing sources,
// add the new sources, and then add the existing sources back.
var adderSources = adder(new ConfigurationBuilder()).Sources;
var existingSources = builder.Sources;
var sources = adderSources.Concat(existingSources)
.ToImmutableArray();
builder.Sources.Clear();
sources.Iter(source => builder.Add(source));
return builder;
}
}
public static class ConfigurationModule
{
public static void ConfigureConfigurationJson(IHostApplicationBuilder builder)
{
builder.Services.TryAddSingleton(GetConfigurationJson);
}
private static ConfigurationJson GetConfigurationJson(IServiceProvider provider)
{
var configuration = provider.GetRequiredService<IConfiguration>();
var logger = provider.GetRequiredService<ILogger<ConfigurationJson>>();
var configurationJson = ConfigurationJson.From(configuration);
return TryGetConfigurationJsonFromYaml(configuration, logger)
.Map(configurationJson.MergeWith)
.IfNone(configurationJson);
}
private static Option<ConfigurationJson> TryGetConfigurationJsonFromYaml(IConfiguration configuration, ILogger logger) =>
configuration.TryGetValue("CONFIGURATION_YAML_PATH")
.Map(path => new FileInfo(path))
.Where(file => file.Exists)
.Map(file =>
{
logger.LogInformation("Loading configuration from YAML file: {FilePath}", file.FullName);
// Validate the YAML configuration before loading
var validationResult = ConfigurationValidator.ValidateExtractorConfigurationFromFile(file, logger);
return validationResult.Match(
errors =>
{
var errorMessages = string.Join(Environment.NewLine, errors.Select(e => $" - {e}"));
var fullMessage = $"Configuration validation failed for file '{file.FullName}':{Environment.NewLine}{errorMessages}";
logger.LogError("Configuration validation errors: {Errors}", errorMessages);
throw new InvalidOperationException(fullMessage);
},
validConfig =>
{
logger.LogInformation("Configuration validation passed for file: {FilePath}", file.FullName);
return validConfig;
}
);
});
}