Skip to content

Commit 88d99e8

Browse files
authored
Remove ConfigQueryResult type and clean up ConsulConfigurationProviderTests. (#28)
1 parent ac0d0a4 commit 88d99e8

12 files changed

Lines changed: 450 additions & 446 deletions

src/Winton.Extensions.Configuration.Consul/ConfigQueryResult.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Winton.Extensions.Configuration.Consul/ConsulConfigurationClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public ConsulConfigurationClient(IConsulClientFactory consulClientFactory, ICons
2626
_source = source;
2727
}
2828

29-
public async Task<IConfigQueryResult> GetConfig()
29+
public async Task<QueryResult<KVPair>> GetConfig()
3030
{
3131
QueryResult<KVPair> result = await GetKvPair().ConfigureAwait(false);
3232
UpdateLastIndex(result);
33-
return new ConfigQueryResult(result);
33+
return result;
3434
}
3535

3636
public IChangeToken Watch(Action<ConsulWatchExceptionContext> onException)

src/Winton.Extensions.Configuration.Consul/ConsulConfigurationProvider.cs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Threading.Tasks;
8+
using Consul;
89
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.Primitives;
11+
using Winton.Extensions.Configuration.Consul.Extensions;
1012

1113
namespace Winton.Extensions.Configuration.Consul
1214
{
@@ -51,12 +53,27 @@ public override void Load()
5153
}
5254
}
5355

56+
private Dictionary<string, string> ConvertResultToDictionary(QueryResult<KVPair> queryResult)
57+
{
58+
if (!queryResult.HasValue())
59+
{
60+
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
61+
}
62+
63+
using (var configStream = new MemoryStream(queryResult.Value()))
64+
{
65+
return new Dictionary<string, string>(
66+
_source.Parser.Parse(configStream),
67+
StringComparer.OrdinalIgnoreCase);
68+
}
69+
}
70+
5471
private async Task DoLoad(bool reloading)
5572
{
5673
try
5774
{
58-
IConfigQueryResult configQueryResult = await _consulConfigClient.GetConfig().ConfigureAwait(false);
59-
if (!configQueryResult.Exists && !_source.Optional)
75+
QueryResult<KVPair> queryResult = await _consulConfigClient.GetConfig().ConfigureAwait(false);
76+
if (!queryResult.HasValue() && !_source.Optional)
6077
{
6178
if (!reloading)
6279
{
@@ -68,36 +85,15 @@ private async Task DoLoad(bool reloading)
6885
return;
6986
}
7087

71-
LoadIntoMemory(configQueryResult);
88+
Data = ConvertResultToDictionary(queryResult);
7289
}
7390
catch (Exception exception)
7491
{
75-
HandleLoadException(exception);
76-
}
77-
}
78-
79-
private void HandleLoadException(Exception exception)
80-
{
81-
var exceptionContext = new ConsulLoadExceptionContext(_source, exception);
82-
_source.OnLoadException?.Invoke(exceptionContext);
83-
if (!exceptionContext.Ignore)
84-
{
85-
throw exception;
86-
}
87-
}
88-
89-
private void LoadIntoMemory(IConfigQueryResult configQueryResult)
90-
{
91-
if (!configQueryResult.Exists)
92-
{
93-
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
94-
}
95-
else
96-
{
97-
using (var configStream = new MemoryStream(configQueryResult.Value))
92+
var exceptionContext = new ConsulLoadExceptionContext(_source, exception);
93+
_source.OnLoadException?.Invoke(exceptionContext);
94+
if (!exceptionContext.Ignore)
9895
{
99-
IDictionary<string, string> parsedData = _source.Parser.Parse(configStream);
100-
Data = new Dictionary<string, string>(parsedData, StringComparer.OrdinalIgnoreCase);
96+
throw;
10197
}
10298
}
10399
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Winton. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENCE in the project root for license information.
3+
4+
using System.Net;
5+
using Consul;
6+
7+
namespace Winton.Extensions.Configuration.Consul.Extensions
8+
{
9+
internal static class KvPairQueryResultExtensions
10+
{
11+
internal static bool HasValue(this QueryResult<KVPair> queryResult)
12+
{
13+
return queryResult?.StatusCode != HttpStatusCode.NotFound
14+
&& queryResult.Value()?.Length > 0;
15+
}
16+
17+
internal static byte[] Value(this QueryResult<KVPair> queryResult)
18+
{
19+
return queryResult?.Response?.Value;
20+
}
21+
}
22+
}

src/Winton.Extensions.Configuration.Consul/IConfigQueryResult.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Winton.Extensions.Configuration.Consul/IConsulConfigurationClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Threading.Tasks;
6+
using Consul;
67
using Microsoft.Extensions.Primitives;
78

89
namespace Winton.Extensions.Configuration.Consul
@@ -11,8 +12,8 @@ namespace Winton.Extensions.Configuration.Consul
1112
internal interface IConsulConfigurationClient
1213
{
1314
/// <summary>Gets the config from consul asynchronously.</summary>
14-
/// <returns>A Task containing the result of the query for the config.</returns>
15-
Task<IConfigQueryResult> GetConfig();
15+
/// <returns>A task containing the result of the query for the config.</returns>
16+
Task<QueryResult<KVPair>> GetConfig();
1617

1718
/// <summary>Watches the config for changes.</summary>
1819
/// <param name="onException">An action to be invoked if an exception occurs during the watch.</param>

test/Website/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Program
77
{
88
public static void Main(string[] args)
99
{
10-
var host = new WebHostBuilder()
10+
IWebHost host = new WebHostBuilder()
1111
.UseKestrel()
1212
.UseContentRoot(Directory.GetCurrentDirectory())
1313
.UseIISIntegration()
@@ -17,4 +17,4 @@ public static void Main(string[] args)
1717
host.Run();
1818
}
1919
}
20-
}
20+
}

test/Website/Startup.cs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,47 @@ namespace Winton.Extensions.Configuration.Consul.Website
1111
{
1212
public class Startup
1313
{
14-
private readonly CancellationTokenSource _consulConfigCancellationTokenSource = new CancellationTokenSource();
1514
private readonly IConfigurationRoot _configuration;
15+
private readonly CancellationTokenSource _consulConfigCancellationTokenSource = new CancellationTokenSource();
1616

1717
public Startup(IHostingEnvironment env, ILoggerFactory loggerFactory)
1818
{
1919
loggerFactory
2020
.AddConsole(LogLevel.Debug)
2121
.AddDebug(LogLevel.Debug);
2222

23-
var builder = new ConfigurationBuilder()
23+
IConfigurationBuilder builder = new ConfigurationBuilder()
2424
.SetBasePath(env.ContentRootPath)
2525
.AddConsul(
2626
"appsettings.json",
2727
_consulConfigCancellationTokenSource.Token,
2828
options =>
2929
{
30-
options.ConsulConfigurationOptions = cco =>
31-
{
32-
cco.Address = new Uri("http://consul:8500");
33-
};
30+
options.ConsulConfigurationOptions = cco => { cco.Address = new Uri("http://consul:8500"); };
3431
options.Optional = true;
3532
options.ReloadOnChange = true;
36-
options.OnLoadException = (exceptionContext) =>
37-
{
38-
exceptionContext.Ignore = true;
39-
};
33+
options.OnLoadException = exceptionContext => { exceptionContext.Ignore = true; };
4034
})
4135
.AddEnvironmentVariables();
4236
_configuration = builder.Build();
4337
}
4438

45-
public void ConfigureServices(IServiceCollection services)
46-
{
47-
services
48-
.AddSwaggerGen(c =>
49-
{
50-
c.SwaggerDoc("v1", new Info { Title = "Test Website", Version = "v1" });
51-
})
52-
.AddSingleton(_configuration)
53-
.AddMvc();
54-
}
55-
5639
public void Configure(IApplicationBuilder app, IApplicationLifetime appLifetime)
5740
{
5841
app
5942
.UseMvc()
6043
.UseSwagger()
61-
.UseSwaggerUI(c =>
62-
{
63-
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Test Website");
64-
});
44+
.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Test Website"); });
6545

6646
appLifetime.ApplicationStopping.Register(_consulConfigCancellationTokenSource.Cancel);
6747
}
48+
49+
public void ConfigureServices(IServiceCollection services)
50+
{
51+
services
52+
.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "Test Website", Version = "v1" }); })
53+
.AddSingleton(_configuration)
54+
.AddMvc();
55+
}
6856
}
69-
}
57+
}

test/Winton.Extensions.Configuration.Consul.Test/ConfigQueryResultTests.cs

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)