This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
134 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Examples/Warden.Spawn.Examples.Console/configuration.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/Warden.Spawn/Core/WardenSpawnJsonConfigurationReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Watchers/Warden.Spawn.Watchers.Web/WebWatcherSpawnJsonConfigurationReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |