diff --git a/README.md b/README.md index f4ffaa8..0679378 100644 --- a/README.md +++ b/README.md @@ -6,19 +6,26 @@ version 1.0.0.6: Thanks to https://github.com/Programm3r the package now has the Also in this version is the ability to get settings async -# TLDR; +# How to Install -1. Package-Install SimpleJsonConfig -2. Use this code + Package-Install SimpleJsonConfig +# Using the code + +```csharp var reader = new ConfigReader(); var value = reader.GetSetting ("Testing"); +``` + +Create a folder default and add a file default.json -3. Create a folder default and add a file default.json. Set the poperty "Copy to Output Directory" to "Copy Always". This will copy the setting file to the root of your assembly everytime you compile. - +```csharp Example: {testing: "foo"} +``` + +Create a folder default and add a file default.json. Set the poperty "Copy to Output Directory" to "Copy Always". This will copy the setting file to the root of your assembly everytime you compile. # Enviroments The Library uses a convention based method of determining where to look for config files. The default convention is to look in a folder called default that is located in the same directory as the excecuting binary. @@ -34,5 +41,7 @@ version 1.0.0.5: Added the ability to specify a root folder. If the environmenta version 1.0.0.6: Thanks to [Programm3r](https://github.com/Programm3r) the package now has the ability to read json configs from a web store. -Also in this version is the ability to get settings async. +What's new +version 1.0.0.5: Added the ability to specify a root folder. If the environmental varialbel called `RootFolder` is set the system will look for the other folders within this folder. This makes it easier to group config folders into a central group. + \ No newline at end of file diff --git a/src/SimpleJsonConfig.Test/ConfigReaderTests.cs b/src/SimpleJsonConfig.Test/ConfigReaderTests.cs index 85bf33f..f350766 100644 --- a/src/SimpleJsonConfig.Test/ConfigReaderTests.cs +++ b/src/SimpleJsonConfig.Test/ConfigReaderTests.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace SimpleJsonConfig.Test @@ -27,6 +28,16 @@ public void GetSetting_NoEnviroment_TypeOfString() Assert.AreEqual(expectedValue, actualValue); } + [TestMethod] + public async Task GetSetting_NoEnviroment_TypeOfStringAsync() + { + var configReader = new ConfigReader(); + const string expectedValue = "TestValue"; + var actualValue = await configReader.GetSettingAsync("TestKey"); + + Assert.AreEqual(expectedValue, actualValue); + } + [TestMethod] public void GetSetting_NoEnviroment_TypeOfPerson() { @@ -122,7 +133,7 @@ public void GetSetting_RootFolderSpecified() Environment.SetEnvironmentVariable("RootFolder", "config"); var configReader = new ConfigReader(); - string expectedValue = "TestValueInCustomRootConfig"; + const string expectedValue = "TestValueInCustomRootConfig"; var actualValue = configReader.GetSetting("TestKey"); Assert.AreEqual(expectedValue, actualValue); diff --git a/src/SimpleJsonConfig.Test/EmbeddedResourceProviderTests.cs b/src/SimpleJsonConfig.Test/EmbeddedResourceProviderTests.cs index 06f0910..8492f45 100644 --- a/src/SimpleJsonConfig.Test/EmbeddedResourceProviderTests.cs +++ b/src/SimpleJsonConfig.Test/EmbeddedResourceProviderTests.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; using SimpleJsonConfig.Providers; namespace SimpleJsonConfig.Test @@ -11,12 +6,14 @@ namespace SimpleJsonConfig.Test [TestClass] public class EmbeddedResourceProviderTests { - private IJsonSourceProvider BuildProvider() + private static IJsonSourceProvider BuildProvider() { - var provider = new EmbeddedResourceJsonProvider(typeof(ConfigReaderTests.Person), "SimpleJsonConfig.Test.embedded_resource", + var provider = new EmbeddedResourceJsonProvider(typeof(ConfigReaderTests.Person), + "SimpleJsonConfig.Test.embedded_resource", "default.json"); return provider; } + [TestMethod] public void Embedded_GetSetting_NoEnviroment_TypeOfString() { @@ -32,12 +29,7 @@ public void Embedded_GetSetting_NoEnviroment_TypeOfString() public void Embedded_GetSetting_NoEnviroment_TypeOfPerson() { var configReader = new ConfigReader(BuildProvider()); - var expectedValue = new ConfigReaderTests.Person - { - Name = "foo", - Surname = "bar" - - }; + var expectedValue = new ConfigReaderTests.Person {Name = "foo", Surname = "bar"}; var actualValue = configReader.GetSetting("TestObjectKey"); Assert.AreEqual(expectedValue.Name, actualValue.Name); @@ -60,4 +52,4 @@ public class Person public string Surname { get; set; } } } -} +} \ No newline at end of file diff --git a/src/SimpleJsonConfig.Test/HttpClientSourceProviderTest.cs b/src/SimpleJsonConfig.Test/HttpClientSourceProviderTest.cs index 6e560f0..86daafe 100644 --- a/src/SimpleJsonConfig.Test/HttpClientSourceProviderTest.cs +++ b/src/SimpleJsonConfig.Test/HttpClientSourceProviderTest.cs @@ -8,13 +8,7 @@ namespace SimpleJsonConfig.Test [TestClass] public class HttpClientSourceProviderTest { - public IJsonSourceProvider SourceProvider - { - get - { - return new HttpClientSourceProvider("http://jsonplaceholder.typicode.com/users/1", HttpMethod.Get); - } - } + public IJsonSourceProvider SourceProvider => new HttpClientSourceProvider("http://jsonplaceholder.typicode.com/users/1", HttpMethod.Get); [TestMethod] public async Task Http_Async_GetSetting_TypeOfString() @@ -22,7 +16,7 @@ public async Task Http_Async_GetSetting_TypeOfString() // arrange var configReader = new ConfigReader(this.SourceProvider); // act - var expectedValue = "Leanne Graham"; + const string expectedValue = "Leanne Graham"; var actualValue = await configReader.GetSettingAsync("name"); // assert Assert.AreEqual(expectedValue, actualValue); @@ -65,7 +59,7 @@ public void Http_Sync_GetSetting_TypeOfString() var provider = new HttpClientSourceProvider("http://date.jsontest.com/", HttpMethod.Get); var configReader = new ConfigReader(this.SourceProvider); // act - var expectedValue = "Leanne Graham"; + const string expectedValue = "Leanne Graham"; var actualValue = configReader.GetSetting("name"); // assert Assert.AreEqual(expectedValue, actualValue); diff --git a/src/SimpleJsonConfig.Test/PathProvider/PathProviderTest.cs b/src/SimpleJsonConfig.Test/PathProvider/PathProviderTest.cs index 36f6bf6..c938572 100644 --- a/src/SimpleJsonConfig.Test/PathProvider/PathProviderTest.cs +++ b/src/SimpleJsonConfig.Test/PathProvider/PathProviderTest.cs @@ -5,8 +5,7 @@ namespace SimpleJsonConfig.Test.PathProvider [TestClass] public class PathProviderTest { - - SimpleJsonConfig.PathProvider.PathProvider PathProvider { get; set; } + private SimpleJsonConfig.PathProvider.PathProvider PathProvider { get; set; } public PathProviderTest() { diff --git a/src/SimpleJsonConfig/ConfigReader.cs b/src/SimpleJsonConfig/ConfigReader.cs index 007cbd3..8d086b3 100644 --- a/src/SimpleJsonConfig/ConfigReader.cs +++ b/src/SimpleJsonConfig/ConfigReader.cs @@ -43,7 +43,6 @@ public ConfigReader(IJsonSourceProvider jsonSource) /// public T GetSetting(string key) { - try { var stream = jsonSourceProvider.GetJsonStream(); if (stream == null) return default(T); diff --git a/src/SimpleJsonConfig/Providers/DefaultJsonSourceProvider.cs b/src/SimpleJsonConfig/Providers/DefaultJsonSourceProvider.cs index f73f137..c031bf7 100644 --- a/src/SimpleJsonConfig/Providers/DefaultJsonSourceProvider.cs +++ b/src/SimpleJsonConfig/Providers/DefaultJsonSourceProvider.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; using System.IO; -using System.Text; using System.Linq; -using System.Text; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using System.Diagnostics; @@ -11,7 +9,10 @@ namespace SimpleJsonConfig.Providers { - + /// + /// Default Json Source Provider Class + /// + /// /// /// The Env variables mentioned below do not need to be set. A default configution can be used. Just ensure you have a folder called dev in your bin directory with your config files. /// This class provides the default json source provider. Please note the env variables the class will look at are @@ -28,24 +29,47 @@ public class DefaultJsonSourceProvider : IJsonSourceProvider private const string DefaultEnviroment = "dev"; + /// + /// Gets or sets the path provider. + /// + /// + /// The path provider. + /// private PathProvider.PathProvider PathProvider { get; set; } + /// + /// Initializes a new instance of the class. + /// + /// The path provider. public DefaultJsonSourceProvider(PathProvider.PathProvider pathProvider) { this.PathProvider = pathProvider; } - public DefaultJsonSourceProvider() { this.PathProvider = new PathProvider.PathProvider(); } - public Stream GetJsonStream() + private static async Task ReadAllFileAsync(string filename) { - // Look for default folder and dev or development folders + using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) + { + var buff = new byte[file.Length]; + await file.ReadAsync(buff, 0, (int)file.Length); + return new MemoryStream(buff); + } + } + + /// + /// Gets the files from environment. + /// + /// + private IEnumerable GetFilesFromEnvironment() + { + // Look for default folder and development or development folders var environment = Environment.GetEnvironmentVariable(ConfEnv); - environment = String.IsNullOrEmpty(environment) ? DefaultEnviroment : environment; + environment = string.IsNullOrEmpty(environment) ? DefaultEnviroment : environment; var rootFolder = Environment.GetEnvironmentVariable(RootFolder); this.PathProvider.RootPath = rootFolder; @@ -53,6 +77,23 @@ public Stream GetJsonStream() var enviromentPath = environment.ToLower(); var path = this.PathProvider.GetConfigPath(enviromentPath); + return !Directory.Exists(path) ? null : Directory.GetFiles(path); + } + + /// + /// Gets the json stream. + /// + /// + public Stream GetJsonStream() + { + var files = this.GetFilesFromEnvironment(); + return (from file in files + let extension = Path.GetExtension(file) + where extension != null && extension.ToLower() + .Equals(FileExtention.ToLower()) + select file).Select(File.OpenRead) + .FirstOrDefault(); + var streams = new List(); Trace.TraceInformation("Environment: {0}", environment); @@ -83,9 +124,20 @@ public Stream GetJsonStream() } } - public Task GetJsonStreamAsync() + /// + /// Gets the json stream asynchronous. + /// + /// + public async Task GetJsonStreamAsync() { - throw new NotImplementedException(); + var files = this.GetFilesFromEnvironment(); + var query = from file in files + let extension = Path.GetExtension(file) + where extension != null && extension.ToLower() + .Equals(FileExtention.ToLower()) + select file; + + return await ReadAllFileAsync(query.FirstOrDefault()); } } } diff --git a/src/SimpleJsonConfig/Providers/EmbeddedResourceJsonProvider.cs b/src/SimpleJsonConfig/Providers/EmbeddedResourceJsonProvider.cs index 70c6541..41f0a48 100644 --- a/src/SimpleJsonConfig/Providers/EmbeddedResourceJsonProvider.cs +++ b/src/SimpleJsonConfig/Providers/EmbeddedResourceJsonProvider.cs @@ -39,7 +39,7 @@ public EmbeddedResourceJsonProvider(Type typeInAssembly, string nameSpace, strin /// public Stream GetJsonStream() { - var resourceName = String.Format("{0}.{1}", _nameSpace, _resourceName); + var resourceName = $"{_nameSpace}.{_resourceName}"; var stream = _typeInAssembly.Assembly.GetManifestResourceStream(resourceName); return stream; }