Skip to content

Commit ee6699e

Browse files
Merge pull request #63 from aspnet/54_AppSettings-config-with-colon
Update default tokenPattern to be more reasonable.
2 parents f7a396d + 9e1955b commit ee6699e

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

samples/SampleWebApp/Web.config

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
<add name="KV4" vaultName="${ConfigBuilderTestKeyVaultName}" mode="Greedy" version="0de51928e49144ce86eb1de9056ac937" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
3434

3535
<add name="AS_Sub_Test" optional="${Boolean}" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
36+
<add name="AS_Sub_Test2" optional="${app~Settings_Colon-and$friends@super+duper,awesome#cool:Test.}" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
3637
<add name="ExpTest" mode="Expand" escapeExpandedValues="true" jsonFile="~/App_Data/expandTest.json" jsonMode="Flat" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
3738
</builders>
3839
</configBuilders>
3940

4041
<appSettings configBuilders="Environment,Secrets,Json,KeyPerFile">
42+
<add key="app~Settings_Colon-and$friends@super+duper,awesome#cool:Test." value="true" />
4143
<add key="WINDIR" value="Will be replaced by 'Environment' in Strict and Greedy modes." />
4244
<add key="SYSTEMDRIVE" value="Will initally be replaced by 'Environment' in Strict and Greedy modes... but then superceded by 'Secrets' in the same modes." />
4345
<add key="Value_Replaced_By_Environment_In_Expand_Mode" value="${WINDIR}" />
@@ -63,7 +65,7 @@
6365
<!-- key="Secret3" value="Will be added by KV3:Latest3. IFF already added, will be "updated" by KV1 to Latest3. KV2 and KV4 don't have versions matching this secret." -->
6466
</appSettings>
6567

66-
<connectionStrings configBuilders="Json,ExpTest">
68+
<connectionStrings configBuilders="Json,ExpTest,AS_Sub_Test2">
6769
<add name="expansionTest" connectionString="${expandTestCS}" />
6870
<add name="expandTestCS" connectionString="Only replaced in Strict/Greedy modes. Not Expand."/>
6971
<add name="jsonConnectionString1" connectionString="Will be replaced by 'Json' in 'Flat' AND 'Sectional' jsonModes, but with different values." />

src/Base/KeyValueConfigBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,21 @@ public abstract class KeyValueConfigBuilder : ConfigurationBuilder
3939
/// Gets or sets the substitution pattern to be used by the KeyValueConfigBuilder.
4040
/// </summary>
4141
public KeyValueMode Mode { get; private set; } = KeyValueMode.Strict;
42+
4243
/// <summary>
4344
/// Gets or sets a prefix string that must be matched by keys to be considered for value substitution.
4445
/// </summary>
4546
public string KeyPrefix { get { EnsureInitialized(); return _keyPrefix; } }
4647
private string _keyPrefix = "";
48+
4749
private bool StripPrefix { get { EnsureInitialized(); return _stripPrefix; } }
4850
private bool _stripPrefix = false; // Prefix-stripping is all handled in this base class; this is private so it doesn't confuse sub-classes.
4951
/// <summary>
5052
/// Specifies whether the config builder should cause errors if the backing source cannot be found.
5153
/// </summary>
5254
public bool Optional { get { EnsureInitialized(); return _optional; } protected set { _optional = value; } }
5355
private bool _optional = true;
56+
5457
/// <summary>
5558
/// Specifies whether the config builder should cause errors if the backing source cannot be found.
5659
/// </summary>
@@ -60,14 +63,16 @@ public abstract class KeyValueConfigBuilder : ConfigurationBuilder
6063
/// Gets or sets a regular expression used for matching tokens in raw xml during Greedy substitution.
6164
/// </summary>
6265
public string TokenPattern { get { EnsureInitialized(); return _tokenPattern; } protected set { _tokenPattern = value; } }
63-
private string _tokenPattern = @"\$\{(\w+)\}";
66+
//private string _tokenPattern = @"\$\{(\w+)\}";
67+
private string _tokenPattern = @"\$\{(\w[\w-_$@#+,.:~]*)\}"; // Updated to be more reasonable for V2
6468

6569
/// <summary>
6670
/// Looks up a single 'value' for the given 'key.'
6771
/// </summary>
6872
/// <param name="key">The 'key' to look up in the config source. (Prefix handling is not needed here.)</param>
6973
/// <returns>The value corresponding to the given 'key' or null if no value is found.</returns>
7074
public abstract string GetValue(string key);
75+
7176
/// <summary>
7277
/// Retrieves all known key/value pairs for the configuration source where the key begins with with <paramref name="prefix"/>.
7378
/// </summary>
@@ -87,6 +92,7 @@ public abstract class KeyValueConfigBuilder : ConfigurationBuilder
8792
/// <param name="key">The string to be validated. May be partial.</param>
8893
/// <returns>True if the string is valid. False if the string is not a valid key.</returns>
8994
public virtual bool ValidateKey(string key) { return true; }
95+
9096
/// <summary>
9197
/// Transforms the raw key to a new string just before updating items in Strict and Greedy modes.
9298
/// </summary>

0 commit comments

Comments
 (0)