Skip to content

Commit

Permalink
Merge pull request #178 from pengweiqhca/master
Browse files Browse the repository at this point in the history
添加CommonSectionBuilder
  • Loading branch information
pengweiqhca authored Nov 19, 2021
2 parents f12e8b8 + 29b0188 commit 4f87e23
Show file tree
Hide file tree
Showing 21 changed files with 474 additions and 199 deletions.
2 changes: 1 addition & 1 deletion Apollo.AspNetCoreHosting/Apollo.AspNetCoreHosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $(PackageReleaseNotes)</Description>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<RootNamespace>Microsoft.AspNetCore.Hosting</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>$(ApolloVersion)</Version>
<Version>$(ApolloVersion).0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Apollo.ConfigAdapter.Yaml/Apollo.ConfigAdapter.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $(PackageReleaseNotes)</Description>
<PackageTags>$(PackageTags) yml yaml</PackageTags>
<RootNamespace>Com.Ctrip.Framework.Apollo.ConfigAdapter</RootNamespace>
<TargetFrameworks>net40;net45;netstandard2.0</TargetFrameworks>
<Version>$(ApolloVersion)</Version>
<Version>$(ApolloVersion).0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Apollo.Configuration/Apollo.Configuration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $(PackageReleaseNotes)</Description>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<RootNamespace>Com.Ctrip.Framework.Apollo</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>$(ApolloVersion)</Version>
<Version>$(ApolloVersion).0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions Apollo.ConfigurationManager.Tests/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@
<configuration>
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<section name="test" type="Apollo.ConfigurationManager.Tests.TestConfigurationSection, Apollo.ConfigurationManager.Tests" />
</configSections>

<configBuilders>
<builders>
<add name="AppSettingsSectionBuilder" type="Com.Ctrip.Framework.Apollo.AppSettingsSectionBuilder, Com.Ctrip.Framework.Apollo.ConfigurationManager" namespace="application.json;application.xml;application.yml;application.yaml;application" />
<add name="ConnectionStringsSectionBuilder" type="Com.Ctrip.Framework.Apollo.ConnectionStringsSectionBuilder, Com.Ctrip.Framework.Apollo.ConfigurationManager" namespace="application.json;application.xml;application.yml;application.yaml;application" />
<add name="ClientSectionBuilder" type="Com.Ctrip.Framework.Apollo.NodeReplaceSectionBuilder, Com.Ctrip.Framework.Apollo.ConfigurationManager" namespace="application.json;application.xml;application.yml;application.yaml;application" key="system.serviceModel:client" />
<add name="CommonSectionBuilder" type="Com.Ctrip.Framework.Apollo.CommonSectionBuilder, Com.Ctrip.Framework.Apollo.ConfigurationManager" keyPrefix="" namespace="application.json;application.xml;application.yml;application.yaml;application" />
</builders>
</configBuilders>

<connectionStrings configBuilders="ConnectionStringsSectionBuilder" />

<appSettings configBuilders="AppSettingsSectionBuilder">
<add key="Apollo:AppId" value="apollo-client" />
<add key="Apollo:Secret" value="ffd9d01130ee4329875ac3441c0bedda" />
<add key="Apollo:MetaServer" value="http://106.54.227.205:8080/" />
</appSettings>

<system.serviceModel>
<client configBuilders="ClientSectionBuilder" />
</system.serviceModel>

<test configBuilders="CommonSectionBuilder">
<map>
<add key="abc" value="321" />
</map>
</test>

</configuration>
88 changes: 88 additions & 0 deletions Apollo.ConfigurationManager.Tests/ConfigExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Com.Ctrip.Framework.Apollo;
using Xunit;

namespace Apollo.ConfigurationManager.Tests;

public class ConfigExtensionsTest
{
[Fact]
public void GetChildren()
{
var config = new TestConfig(Create("a:B", "1"), Create("A:c", "2"), Create("A:C:d", "4"), Create("b", "3"));

Assert.Equal(new[] { "a", "b" }, config.GetChildren("").Select(x => x.Name));
Assert.Equal(new[] { "B", "c" }, config.GetChildren("a").Select(x => x.Name));
Assert.Equal(new[] { "a:B", "A:c" }, config.GetChildren("a").Select(x => x.FullName));
}

[Fact]
public void GetConnectionStrings_ConnectionString_Prior()
{
var config = new TestConfig(Create("a:b", "123"), Create("a:b:ConnectionString", "abc"));

var connectionString = config.GetConnectionStrings("a", "sql").Single();

Assert.Equal("b", connectionString.Name);
Assert.Equal("abc", connectionString.ConnectionString);
Assert.Equal("sql", connectionString.ProviderName);
}

[Fact]
public void GetConnectionStrings_ProviderName()
{
var config = new TestConfig(Create("a:b", "123"), Create("a:b:ProviderName", "abc"));

var connectionString = config.GetConnectionStrings("a", "sql").Single();

Assert.Equal("123", connectionString.ConnectionString);
Assert.Equal("abc", connectionString.ProviderName);
}

[Fact]
public void GetConnectionStrings_No_Prefix()
{
var config = new TestConfig(Create("a", "123"));

var connectionString = config.GetConnectionStrings("", "sql").Single();

Assert.Equal("a", connectionString.Name);
Assert.Equal("123", connectionString.ConnectionString);
}

[Fact]
public void WithPrefix()
{
IConfig config = new TestConfig(Create("a:B", "1"), Create("A:c", "2"), Create("A:C:d", "4"), Create("b", "3"));

Assert.Same(config, config.WithPrefix(" "));

config = config.WithPrefix("a");

Assert.Equal(new[] { "B", "c", "C:d" }, config.GetPropertyNames());
Assert.Equal(new[] { "1", "2", "4" }, config.GetPropertyNames().Select(p => config.GetProperty(p, "")));
}

private static KeyValuePair<string, string> Create(string key, string value) => new(key, value);

private class TestConfig : IConfig
{
private readonly IReadOnlyDictionary<string, string> _dict;

public TestConfig(params KeyValuePair<string, string>[] keyValues)
{
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

foreach (var kv in keyValues) dict[kv.Key] = kv.Value;

_dict = dict;
}

public bool TryGetProperty(string key, [NotNullWhen(true)] out string? value) =>
_dict.TryGetValue(key, out value);

public IEnumerable<string> GetPropertyNames() => _dict.Keys;

public event ConfigChangeEvent ConfigChanged = default!;
}
}

39 changes: 37 additions & 2 deletions Apollo.ConfigurationManager.Tests/ConfigurationBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ConfigurationBuilderTest
#if NET471_OR_GREATER
[Fact]
public void AppSettingsSectionBuilderTest() =>
Assert.Equal("560", System.Configuration.ConfigurationManager.AppSettings["Timeout"]);
Assert.Equal("00:00:30", System.Configuration.ConfigurationManager.AppSettings["Timeout"]);

[Fact]
public void ConnectionStringsSectionBuilderTest() =>
Expand All @@ -26,5 +26,40 @@ public void NodeReplaceSectionBuilderTest()
Assert.Equal(new Uri("http://localhost:1234"), endpoint.Address);
Assert.Equal("test", endpoint.Name);
}

[Fact]
public void CommonConfigurationBuilderTest()
{
var test = (TestConfigurationSection)System.Configuration.ConfigurationManager.GetSection("test");

Assert.Equal(3, test.DefaultValue);

Assert.Equal(TimeSpan.FromSeconds(30), test.Timeout);

Assert.Equal(100, test.MaxValue);

Assert.NotNull(test.Map);

var element = test.Map["abc"];

Assert.NotNull(element);

Assert.Equal("abc", element.Key);
Assert.Equal("123", element.Value);

Assert.Null(test.Map["def"]);

element = test.Map["defg"];

Assert.NotNull(element);

Assert.Equal("defg", element.Key);
Assert.Equal("456", element.Value);

Assert.NotNull(test.Element);

Assert.Equal("jkl", test.Element.Name);
Assert.Equal("789", test.Element.Value);
}
#endif
}
}
35 changes: 0 additions & 35 deletions Apollo.ConfigurationManager.Tests/GetChildrenTest.cs

This file was deleted.

21 changes: 21 additions & 0 deletions Apollo.ConfigurationManager.Tests/TestConfigurationSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Configuration;

namespace Apollo.ConfigurationManager.Tests;

public class TestConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("timeout")]
public TimeSpan Timeout => (TimeSpan)this["timeout"];

[ConfigurationProperty("maxValue")]
public int MaxValue => (int)this["maxValue"];

[ConfigurationProperty("defaultValue", DefaultValue = 3L)]
public long DefaultValue => (long)this["defaultValue"];

[ConfigurationProperty("element")]
public NameValueConfigurationElement Element => (NameValueConfigurationElement)this["element"];

[ConfigurationProperty("map")]
public KeyValueConfigurationCollection Map => (KeyValueConfigurationCollection)this["map"];
}
41 changes: 0 additions & 41 deletions Apollo.ConfigurationManager.Tests/XmlExtensionsTest.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
  直接使用ApolloConfigurationManager请使用Com.Ctrip.Framework.Apollo.Configuration或者Com.Ctrip.Framework.Apollo.ConfigurationManager

$(PackageReleaseNotes)</Description>
<PackageIconUrl>https://raw.githubusercontent.com/apolloconfig/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(MSBuildProjectName)</PackageProjectUrl>
<PackageTags>$(PackageTags) ConfigurationBuilder ConfigurationManager</PackageTags>
<RootNamespace>Com.Ctrip.Framework.Apollo</RootNamespace>
<TargetFrameworks>net471;net45;net40;netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>$(ApolloVersion)</Version>
<Version>$(ApolloVersion).1</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -36,7 +35,6 @@ $(PackageReleaseNotes)</Description>

<ItemGroup Condition="'$(TargetFramework)' != 'net471'">
<Compile Remove="*Builder.cs" />
<Compile Remove="XmlExtensions.cs" />
</ItemGroup>

</Project>
17 changes: 15 additions & 2 deletions Apollo.ConfigurationManager/AppSettingsSectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ namespace Com.Ctrip.Framework.Apollo;

public class AppSettingsSectionBuilder : ApolloConfigurationBuilder
{
private string? _keyPrefix;

public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name, config);

_keyPrefix = config["keyPrefix"];

if (!string.IsNullOrWhiteSpace(_keyPrefix) && !_keyPrefix.EndsWith(":"))
_keyPrefix += ":";
}

public override ConfigurationSection ProcessConfigurationSection(ConfigurationSection configSection)
{
if (configSection is not AppSettingsSection section) return base.ProcessConfigurationSection(configSection);
Expand All @@ -15,7 +27,8 @@ public override ConfigurationSection ProcessConfigurationSection(ConfigurationSe

lock (this)
{
var config = GetConfig();
var config = GetConfig().WithPrefix(_keyPrefix);

foreach (var key in config.GetPropertyNames())
{
if (config.TryGetProperty(key, out var value))
Expand All @@ -39,4 +52,4 @@ private static void TrySetConfigUtil(KeyValueConfigurationCollection appSettings

ConfigUtil.AppSettings = settings;
}
}
}
Loading

0 comments on commit 4f87e23

Please sign in to comment.