-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathDccConfigurationRepository.cs
92 lines (80 loc) · 3.59 KB
/
DccConfigurationRepository.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
namespace Masa.Contrib.BasicAbility.Dcc.Internal;
internal class DccConfigurationRepository : AbstractConfigurationRepository
{
private readonly IConfigurationApiClient _client;
public override SectionTypes SectionType { get; init; } = SectionTypes.ConfigurationAPI;
private readonly ConcurrentDictionary<string, IDictionary<string, string>> _dictionaries = new();
private readonly ConcurrentDictionary<string, ConfigurationTypes> _configObjectConfigurationTypeRelations = new();
public DccConfigurationRepository(
IEnumerable<DccSectionOptions> sectionOptions,
IConfigurationApiClient client,
ILoggerFactory loggerFactory)
: base(loggerFactory)
{
_client = client;
foreach (var sectionOption in sectionOptions)
{
Initialize(sectionOption).ConfigureAwait(false).GetAwaiter().GetResult();
}
}
private async Task Initialize(DccSectionOptions sectionOption)
{
foreach (var configObject in sectionOption.ConfigObjects)
{
string key = $"{sectionOption.Environment!}-{sectionOption.Cluster!}-{sectionOption.AppId}-{configObject}".ToLower();
var result = await _client.GetRawAsync(sectionOption.Environment!, sectionOption.Cluster!, sectionOption.AppId, configObject, (val) =>
{
if (_configObjectConfigurationTypeRelations.TryGetValue(key, out var configurationType))
{
_dictionaries[key] = FormatRaw(sectionOption.AppId, configObject, val, configurationType);
FireRepositoryChange(SectionType, Load());
}
});
_configObjectConfigurationTypeRelations.TryAdd(key, result.ConfigurationType);
_dictionaries[key] = FormatRaw(sectionOption.AppId, configObject, result.Raw, result.ConfigurationType);
}
}
private IDictionary<string, string> FormatRaw(string appId, string configObject, string? raw, ConfigurationTypes configurationType)
{
if (raw == null)
return new Dictionary<string, string>();
switch (configurationType)
{
case ConfigurationTypes.Json:
return SecondaryFormat(appId, configObject, JsonConfigurationParser.Parse(raw));
case ConfigurationTypes.Properties:
return SecondaryFormat(appId, configObject, JsonSerializer.Deserialize<Dictionary<string, string>>(raw)!);
case ConfigurationTypes.Text:
return new Dictionary<string, string>()
{
{ $"{appId}{ConfigurationPath.KeyDelimiter}{DATA_DICTIONARY_SECTION_NAME}{ConfigurationPath.KeyDelimiter}{configObject}" , raw ?? "" }
};
default:
throw new NotSupportedException(nameof(configurationType));
}
}
private IDictionary<string, string> SecondaryFormat(
string appId,
string configObject,
IDictionary<string, string> data)
{
var dictionary = new Dictionary<string, string>();
foreach (var item in data)
{
dictionary[$"{appId}{ConfigurationPath.KeyDelimiter}{configObject}{ConfigurationPath.KeyDelimiter}{item.Key}"] = item.Value;
}
return dictionary;
}
public override Properties Load()
{
Dictionary<string, string> properties = new();
foreach (var item in _dictionaries)
{
foreach (var key in item.Value.Keys)
{
properties[key] = item.Value[key] ?? string.Empty;
}
}
return new Properties(properties);
}
}