Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Microsoft.Framework.ConfigurationModel/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,29 @@ private KeyValuePair<string, IConfiguration> CreateConfigurationFocus(string pre
new ConfigurationFocus(this, prefix + segment + Constants.KeyDelimiter));
}

/// <summary>
/// Adds a new configuration source.
/// </summary>
/// <param name="configurationSource">The configuration source to add.</param>
/// <returns>The same configuration source.</returns>
public IConfigurationSourceRoot Add(IConfigurationSource configurationSource)
{
configurationSource.Load();
return AddLoadedSource(configurationSource);
return Add(configurationSource, load: true);
}

internal IConfigurationSourceRoot AddLoadedSource(IConfigurationSource configurationSource)
/// <summary>
/// Adds a new configuration source.
/// </summary>
/// <param name="configurationSource">The configuration source to add.</param>
/// <param name="load">If true, the configuration source's <see cref="IConfigurationSource.Load"/> method will be called.</param>
/// <returns>The same configuration source.</returns>
/// <remarks>This method is intended only for test scenarios.</remarks>
public IConfigurationSourceRoot Add(IConfigurationSource configurationSource, bool load)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a doc comment to this, perhaps warning that it's for testing scenarios only?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(And add a doc comment to the other Add() method while you're at it? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Btw, the Add overload with 2 arguments is just on this implementation. Most users will use configuration through IConfiguration which doesn't expose the overload

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, that's good to know. Good to document anyway 😄

{
if (load)
{
configurationSource.Load();
}
_sources.Add(configurationSource);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: InternalsVisibleTo("Microsoft.Framework.ConfigurationModel.Test")]
[assembly: InternalsVisibleTo("Microsoft.Framework.ConfigurationModel.Json.Test")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: AssemblyMetadata("Serviceable", "True")]
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public void ImplicitArrayItemReplacement()
jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

var config = new Configuration();
config.AddLoadedSource(jsonConfigSource1);
config.AddLoadedSource(jsonConfigSource2);
config.Add(jsonConfigSource1, load: false);
config.Add(jsonConfigSource2, load: false);

Assert.Equal(3, config.GetSubKeys("ip").Count());
Assert.Equal("15.16.17.18", config.Get("ip:0"));
Expand Down Expand Up @@ -135,8 +135,8 @@ public void ExplicitArrayReplacement()
jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

var config = new Configuration();
config.AddLoadedSource(jsonConfigSource1);
config.AddLoadedSource(jsonConfigSource2);
config.Add(jsonConfigSource1, load: false);
config.Add(jsonConfigSource2, load: false);

Assert.Equal(3, config.GetSubKeys("ip").Count());
Assert.Equal("1.2.3.4", config.Get("ip:0"));
Expand Down Expand Up @@ -168,8 +168,8 @@ public void ArrayMerge()
jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

var config = new Configuration();
config.AddLoadedSource(jsonConfigSource1);
config.AddLoadedSource(jsonConfigSource2);
config.Add(jsonConfigSource1, load: false);
config.Add(jsonConfigSource2, load: false);

Assert.Equal(4, config.GetSubKeys("ip").Count());
Assert.Equal("1.2.3.4", config.Get("ip:0"));
Expand All @@ -193,7 +193,7 @@ public void ArraysAreKeptInFileOrder()
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

var config = new Configuration();
config.AddLoadedSource(jsonConfigSource);
config.Add(jsonConfigSource, load: false);

var subkey = config.GetSubKey("setting");
var indexSubkeys = subkey.GetSubKeys().ToArray();
Expand Down Expand Up @@ -222,7 +222,7 @@ public void PropertiesAreSortedByNumberOnlyFirst()
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

var config = new Configuration();
config.AddLoadedSource(jsonConfigSource);
config.Add(jsonConfigSource, load: false);

var subkey = config.GetSubKey("setting");
var indexSubkeys = subkey.GetSubKeys().ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using Microsoft.Framework.ConfigurationModel.Json;
using Microsoft.Framework.ConfigurationModel.Test;
using Newtonsoft.Json;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public void LoadAndCombineKeyValuePairsFromDifferentConfigurationSources()
bool memRet1, memRet2, memRet3;

// Act
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.AddLoadedSource(memConfigSrc3);
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);

memRet1 = config.TryGet("mem1:keyinmem1", out memVal1);
memRet2 = config.TryGet("Mem2:KeyInMem2", out memVal2);
Expand Down Expand Up @@ -103,8 +103,8 @@ public void NewConfigurationSourceOverridesOldOneWhenKeyIsDuplicated()
var config = new Configuration();

// Act
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);

// Assert
Assert.Equal("ValueInMem2", config.Get("Key1:Key2"));
Expand All @@ -124,9 +124,9 @@ public void SettingValueUpdatesAllConfigurationSources()
var memConfigSrc3 = new MemoryConfigurationSource(dict);

var config = new Configuration();
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.AddLoadedSource(memConfigSrc3);
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);

// Act
config.Set("Key1", "NewValue1");
Expand Down Expand Up @@ -165,9 +165,9 @@ public void CanGetSubKey()
var memConfigSrc3 = new MemoryConfigurationSource(dic3);

var config = new Configuration();
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.AddLoadedSource(memConfigSrc3);
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);

string memVal1, memVal2, memVal3, memVal4, memVal5;
bool memRet1, memRet2, memRet3, memRet4, memRet5;
Expand Down Expand Up @@ -227,9 +227,9 @@ public void CanGetSubKeys()
var memConfigSrc3 = new MemoryConfigurationSource(dic3);

var config = new Configuration();
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.AddLoadedSource(memConfigSrc3);
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);

// Act
var configFocusList = config.GetSubKeys("Data");
Expand Down Expand Up @@ -266,9 +266,9 @@ public void CanIterateWithGenericEnumerator()
var config = new Configuration();

// Act
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.AddLoadedSource(memConfigSrc3);
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);

// Assert
var enumerator = config.Sources.GetEnumerator();
Expand Down Expand Up @@ -304,9 +304,9 @@ public void CanIterateAfterCastedToIEnumerable()
var config = new Configuration();

// Act
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.AddLoadedSource(memConfigSrc3);
config.Add(memConfigSrc1, load: false);
config.Add(memConfigSrc2, load: false);
config.Add(memConfigSrc3, load: false);

var enumerable = config as IEnumerable;

Expand Down