Skip to content
Open
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: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
("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.
Expand All @@ -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.


13 changes: 12 additions & 1 deletion src/SimpleJsonConfig.Test/ConfigReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SimpleJsonConfig.Test
Expand Down Expand Up @@ -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<string>("TestKey");

Assert.AreEqual(expectedValue, actualValue);
}

[TestMethod]
public void GetSetting_NoEnviroment_TypeOfPerson()
{
Expand Down Expand Up @@ -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<string>("TestKey");

Assert.AreEqual(expectedValue, actualValue);
Expand Down
22 changes: 7 additions & 15 deletions src/SimpleJsonConfig.Test/EmbeddedResourceProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
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
{
[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()
{
Expand All @@ -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<ConfigReaderTests.Person>("TestObjectKey");

Assert.AreEqual(expectedValue.Name, actualValue.Name);
Expand All @@ -60,4 +52,4 @@ public class Person
public string Surname { get; set; }
}
}
}
}
12 changes: 3 additions & 9 deletions src/SimpleJsonConfig.Test/HttpClientSourceProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@ 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()
{
// arrange
var configReader = new ConfigReader(this.SourceProvider);
// act
var expectedValue = "Leanne Graham";
const string expectedValue = "Leanne Graham";
var actualValue = await configReader.GetSettingAsync<string>("name");
// assert
Assert.AreEqual(expectedValue, actualValue);
Expand Down Expand Up @@ -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<string>("name");
// assert
Assert.AreEqual(expectedValue, actualValue);
Expand Down
3 changes: 1 addition & 2 deletions src/SimpleJsonConfig.Test/PathProvider/PathProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
1 change: 0 additions & 1 deletion src/SimpleJsonConfig/ConfigReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public ConfigReader(IJsonSourceProvider jsonSource)
/// <returns></returns>
public T GetSetting<T>(string key)
{
try
{
var stream = jsonSourceProvider.GetJsonStream();
if (stream == null) return default(T);
Expand Down
70 changes: 61 additions & 9 deletions src/SimpleJsonConfig/Providers/DefaultJsonSourceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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;


namespace SimpleJsonConfig.Providers
{

/// <summary>
/// Default Json Source Provider Class
/// </summary>
/// <seealso cref="SimpleJsonConfig.Providers.IJsonSourceProvider" />
/// <summary>
/// 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
Expand All @@ -28,31 +29,71 @@ public class DefaultJsonSourceProvider : IJsonSourceProvider
private const string DefaultEnviroment = "dev";


/// <summary>
/// Gets or sets the path provider.
/// </summary>
/// <value>
/// The path provider.
/// </value>
private PathProvider.PathProvider PathProvider { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="DefaultJsonSourceProvider"/> class.
/// </summary>
/// <param name="pathProvider">The path provider.</param>
public DefaultJsonSourceProvider(PathProvider.PathProvider pathProvider)
{
this.PathProvider = pathProvider;
}


public DefaultJsonSourceProvider()
{
this.PathProvider = new PathProvider.PathProvider();
}

public Stream GetJsonStream()
private static async Task<Stream> 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);
}
}

/// <summary>
/// Gets the files from environment.
/// </summary>
/// <returns></returns>
private IEnumerable<string> 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;

var enviromentPath = environment.ToLower();
var path = this.PathProvider.GetConfigPath(enviromentPath);

return !Directory.Exists(path) ? null : Directory.GetFiles(path);
}

/// <summary>
/// Gets the json stream.
/// </summary>
/// <returns></returns>
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<Stream>();

Trace.TraceInformation("Environment: {0}", environment);
Expand Down Expand Up @@ -83,9 +124,20 @@ public Stream GetJsonStream()
}
}

public Task<Stream> GetJsonStreamAsync()
/// <summary>
/// Gets the json stream asynchronous.
/// </summary>
/// <returns></returns>
public async Task<Stream> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public EmbeddedResourceJsonProvider(Type typeInAssembly, string nameSpace, strin
/// <returns></returns>
public Stream GetJsonStream()
{
var resourceName = String.Format("{0}.{1}", _nameSpace, _resourceName);
var resourceName = $"{_nameSpace}.{_resourceName}";
var stream = _typeInAssembly.Assembly.GetManifestResourceStream(resourceName);
return stream;
}
Expand Down