Skip to content

Commit

Permalink
refactor: refactor dcc configuration (#215)
Browse files Browse the repository at this point in the history
* refactor: refactor dcc configuration

* refactor: refactor dcc configuration

* chore: adjust dcc test

* refactor: refactor dcc configuration

* test: adjust dcc configuration test

* fix: fix configuration test error

* fix: adjust InitializeAppConfiguration function

* refactor(ConfigurationApi.Dcc): Refactor Dcc

* refactor: Optimize ConfigurationApi.Dcc And Perfect unit tests

* refactor(Configuration): Simplified code

* chore: Deal with code smells

* chore: deal with code smells

Co-authored-by: yanpengju <[email protected]>
Co-authored-by: 阎鹏举 <阎鹏举@DESKTOP-28NV352>
Co-authored-by: zhenlei520 <[email protected]>
  • Loading branch information
4 people authored Aug 23, 2022
1 parent 4714160 commit ee11ccd
Show file tree
Hide file tree
Showing 26 changed files with 931 additions and 842 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Configuration;

public interface IConfigurationApiClient
{
Task<(string Raw, ConfigurationTypes ConfigurationType)> GetRawAsync(string configObject, Action<string>? valueChanged = null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Configuration.Options;
Expand All @@ -11,12 +11,18 @@ public class MasaAppConfigureOptions

public string Cluster { get => GetValue(nameof(Cluster)); set => Data[nameof(Cluster)] = value; }

public Dictionary<string, string> Data { get; set; } = new();
private Dictionary<string, string> Data { get; set; } = new(StringComparer.OrdinalIgnoreCase);

private string GetValue(string key)
public int Length => Data.Count;

public string GetValue(string key) => GetValue(key, () => string.Empty);

public string GetValue(string key, Func<string> defaultFunc)
{
if (Data.ContainsKey(key)) return Data[key];

return string.Empty;
return defaultFunc.Invoke();
}

public void TryAdd(string key, string value) => Data.TryAdd(key, value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class Constants

internal const string DEFAULT_SUBSCRIBE_KEY_PREFIX = "masa.dcc:";

internal const string DEFAULT_ENVIRONMENT_NAME = "ASPNETCORE_ENVIRONMENT";

internal const string DATA_DICTIONARY_SECTION_NAME = "DataDictionary";

internal const string DEFAULT_PUBLIC_ID = "public-$Config";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public DccConfigurationRepository(
: base(loggerFactory)
{
_client = client;

foreach (var sectionOption in sectionOptions)
{
Initialize(sectionOption).ConfigureAwait(false).GetAwaiter().GetResult();
Expand All @@ -30,8 +31,8 @@ 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) =>
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))
{
Expand All @@ -45,28 +46,26 @@ private async Task Initialize(DccSectionOptions sectionOption)
}
}

private IDictionary<string, string> FormatRaw(string appId, string configObject, string? raw, ConfigurationTypes configurationType)
private static IDictionary<string, string> FormatRaw(string appId, string configObject, string? raw, ConfigurationTypes configurationType)
{
if (raw == null)
return new Dictionary<string, string>();

switch (configurationType)
return configurationType switch
{
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}{configObject}" , raw ?? "" }
};
default:
throw new NotSupportedException(nameof(configurationType));
}
ConfigurationTypes.Json => SecondaryFormat(appId, configObject, JsonConfigurationParser.Parse(raw)),
ConfigurationTypes.Properties => SecondaryFormat(appId, configObject, JsonSerializer.Deserialize<Dictionary<string, string>>(raw)!),
ConfigurationTypes.Text => new Dictionary<string, string>
{
{ $"{appId}{ConfigurationPath.KeyDelimiter}{configObject}" , raw }
},
ConfigurationTypes.Xml => SecondaryFormat(appId, configObject, JsonConfigurationParser.Parse(raw)),
ConfigurationTypes.Yaml => SecondaryFormat(appId, configObject, JsonConfigurationParser.Parse(raw)),
_ => throw new NotSupportedException(nameof(configurationType)),
};
}

private IDictionary<string, string> SecondaryFormat(
private static IDictionary<string, string> SecondaryFormat(
string appId,
string configObject,
IDictionary<string, string> data)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Utils.Caching.Core.Interfaces;

internal static class DistributedCacheClientExtensions
{
public static List<string> GetAllConfigObjects(
this IDistributedCacheClient distributedCacheClient,
string appId,
string environment,
string cluster)
{
var defaultConfigObjects = new List<string>();

string partialKey =
$"{environment}-{cluster}-{appId}".ToLower();
List<string> keys = distributedCacheClient.GetKeys($"{partialKey}*");
foreach (var key in keys)
{
var configObject = key.Split($"{partialKey}-", StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
if (configObject == null) continue;

defaultConfigObjects.Add(configObject);
}

return defaultConfigObjects;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ internal static class StaticConfig
{
public static string AppId { get; set; }

public static string PublicId => "public-$Config";
public static string PublicId { get; set; }
}

This file was deleted.

Loading

0 comments on commit ee11ccd

Please sign in to comment.