Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
JSON configuration reader #11
Browse files Browse the repository at this point in the history
  • Loading branch information
spetz committed Jul 29, 2016
1 parent ab25d86 commit 8623f27
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 15 deletions.
15 changes: 9 additions & 6 deletions src/Examples/Warden.Spawn.Examples.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using Warden.Spawn.Core;
using Warden.Spawn.Watchers.Web;

Expand All @@ -8,13 +10,14 @@ public class Program
{
public static void Main(string[] args)
{
var watcherConfigurations = new List<IWatcherSpawnConfiguration>
var watcherConfigurationTypes = new Dictionary<string, Type>
{
new WebWatcherSpawnConfiguration("http://www.google.com")
["Web"] = typeof(WebWatcherSpawnConfiguration)
};
var integrationConfigurations = new List<IIntegrationSpawnConfiguration>();
var spawnConfiguration = new WardenSpawnConfiguration("Warden Spawn",
watcherConfigurations, integrationConfigurations);
var integrationConfigurationTypes = new Dictionary<string, Type>();
var spawnConfigurationReader = new WardenSpawnJsonConfigurationReader(watcherConfigurationTypes, integrationConfigurationTypes);
var configurationFile = File.ReadAllText("configuration.json");
var spawnConfiguration = spawnConfigurationReader.Read(configurationFile);
var spawnConfigurator = new WardenSpawnConfigurator();
var spawn = spawnConfigurator.Configure(spawnConfiguration);
var warden = spawn.Spawn();
Expand Down
11 changes: 11 additions & 0 deletions src/Examples/Warden.Spawn.Examples.Console/configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"wardenName": "Warden Spawn",
"watchers": [
{
"type": "Web",
"configuration": {
"url": "http://www.google.com"
}
}
]
}
4 changes: 2 additions & 2 deletions src/Warden.Spawn/Core/IConfigurationReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Warden.Spawn.Core
{
public interface IConfigurationReader
public interface IConfigurationReader<out T> where T : IConfiguration
{

T Read(string configuration);
}
}
10 changes: 7 additions & 3 deletions src/Warden.Spawn/Core/WardenSpawnConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ namespace Warden.Spawn.Core
{
public class WardenSpawnConfiguration : IWardenSpawnConfiguration
{
public string WardenName { get; }
public IEnumerable<IWatcherSpawnConfiguration> Watchers { get; }
public IEnumerable<IIntegrationSpawnConfiguration> Integrations { get; }
public string WardenName { get; protected set; }
public IEnumerable<IWatcherSpawnConfiguration> Watchers { get; protected set; }
public IEnumerable<IIntegrationSpawnConfiguration> Integrations { get; protected set; }

protected WardenSpawnConfiguration()
{
}

public WardenSpawnConfiguration(string wardenName,
IEnumerable<IWatcherSpawnConfiguration> watchers,
Expand Down
83 changes: 83 additions & 0 deletions src/Warden.Spawn/Core/WardenSpawnJsonConfigurationReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Warden.Spawn.Core
{
public class WardenSpawnJsonConfigurationReader : IConfigurationReader<IWardenSpawnConfiguration>
{
private readonly IDictionary<string, Type> _watcherConfigurationTypes;
private readonly IDictionary<string, Type> _integrationConfigurationTypes;
private readonly JsonSerializerSettings _jsonSerializerSettings;

private readonly JsonSerializerSettings _defaultJsonSerializerSettings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateFormatString = "yyyy-MM-dd H:mm:ss",
Formatting = Formatting.Indented,
DefaultValueHandling = DefaultValueHandling.Populate,
NullValueHandling = NullValueHandling.Include,
Converters = new List<JsonConverter>
{
new Newtonsoft.Json.Converters.StringEnumConverter
{
AllowIntegerValues = true,
CamelCaseText = true
}
}
};

public WardenSpawnJsonConfigurationReader(IDictionary<string, Type> watcherConfigurationTypes,
IDictionary<string, Type> integrationConfigurationTypes,
JsonSerializerSettings jsonSerializerSettings = null)
{
_watcherConfigurationTypes = watcherConfigurationTypes;
_integrationConfigurationTypes = integrationConfigurationTypes;
_jsonSerializerSettings = jsonSerializerSettings ?? _defaultJsonSerializerSettings;
}

public IWardenSpawnConfiguration Read(string configuration)
{
var spawnConfiguration = JsonConvert.DeserializeObject<dynamic>(configuration, _jsonSerializerSettings);
var watcherConfigurations = ResolveWatcherConfigurations(spawnConfiguration);
var integrationConfigurations = ResolveIntegrationConfigurations(spawnConfiguration);
var wardenName = spawnConfiguration.wardenName.ToString();

return new WardenSpawnConfiguration(wardenName, watcherConfigurations, integrationConfigurations);
}

private IEnumerable<IWatcherSpawnConfiguration> ResolveWatcherConfigurations(dynamic spawnConfiguration)
{
var watchers = spawnConfiguration.watchers;
if (watchers == null)
yield break;

foreach (var watcher in watchers)
{
var watcherType = _watcherConfigurationTypes[watcher.type.ToString()];
var watcherConfigurationText = JsonConvert.SerializeObject(watcher.configuration);
var watcherConfiguration = JsonConvert.DeserializeObject(watcherConfigurationText, watcherType);

yield return watcherConfiguration;
}
}

private IEnumerable<IIntegrationSpawnConfiguration> ResolveIntegrationConfigurations(dynamic spawnConfiguration)
{
var integrations = spawnConfiguration.integrations;
if (integrations == null)
yield break;

foreach (var integration in integrations)
{
var integrationType = _integrationConfigurationTypes[integration.type.ToString()];
var integrationConfigurationText = JsonConvert.SerializeObject(integration.configuration);
var integrationConfiguration = JsonConvert.DeserializeObject(integrationConfigurationText, integrationType);

yield return integrationConfiguration;
}
}
}
}
8 changes: 5 additions & 3 deletions src/Warden.Spawn/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"version": "1.0.0",
"description": "Warden Spawn allows to create the Warden instance by using human readable configuration files.",
"authors": [ "Piotr Gankiewicz" ],
Expand All @@ -8,8 +8,10 @@
"licenseUrl": "https://github.com/warden-stack/Warden-Spawn/blob/master/LICENSE"
},
"dependencies": {
"Warden": "1.2.1"
},
"Warden": "1.2.1",
"System.Linq": "4.1.0",
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
"net461": {},
"netcoreapp1.0": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ namespace Warden.Spawn.Watchers.Web
{
public class WebWatcherSpawnConfiguration : IWatcherSpawnConfiguration
{
public string Url { get; }
public string Url { get; protected set; }

protected WebWatcherSpawnConfiguration()
{
}

public WebWatcherSpawnConfiguration(string url)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Warden.Spawn.Core;

namespace Warden.Spawn.Watchers.Web
{
public class WebWatcherSpawnJsonConfigurationReader : IConfigurationReader<WebWatcherSpawnConfiguration>
{
public WebWatcherSpawnConfiguration Read(string configuration)
{
return new WebWatcherSpawnConfiguration("google.com");
}
}
}

0 comments on commit 8623f27

Please sign in to comment.