Skip to content

Commit 39ffbcb

Browse files
committed
Box the FileRoute model with goal to avoid versioning
1 parent 910d3cc commit 39ffbcb

7 files changed

Lines changed: 195 additions & 154 deletions

File tree

src/AcceptanceSteps.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Hosting;
88
using Microsoft.Extensions.Logging;
9-
using Microsoft.Extensions.Options;
10-
11-
//using Newtonsoft.Json;
12-
//using Ocelot.Configuration.File;
13-
//using Ocelot.DependencyInjection;
14-
//using Ocelot.Middleware;
159
using Shouldly;
1610
using System.Collections;
1711
using System.Diagnostics;
@@ -22,10 +16,14 @@
2216
using System.Text.Encodings.Web;
2317
using System.Text.Json;
2418
using System.Text.Unicode;
25-
using System.Xml;
2619
using CookieHeaderValue = Microsoft.Net.Http.Headers.CookieHeaderValue;
2720
using MediaTypeHeaderValue = System.Net.Http.Headers.MediaTypeHeaderValue;
2821

22+
//using Newtonsoft.Json;
23+
//using Ocelot.Configuration.File;
24+
//using Ocelot.DependencyInjection;
25+
//using Ocelot.Middleware;
26+
2927
namespace Ocelot.Testing;
3028

3129
public class AcceptanceSteps : IDisposable
@@ -44,7 +42,7 @@ public AcceptanceSteps()
4442
{
4543
_testId = Guid.NewGuid();
4644
random = new Random();
47-
ocelotConfigFileName = $"{_testId:N}-ocelot.json"; // {ConfigurationBuilderExtensions.PrimaryConfigFile}";
45+
ocelotConfigFileName = $"ocelot-{_testId:N}.json"; // {ConfigurationBuilderExtensions.PrimaryConfigFile}";
4846
Files = [ocelotConfigFileName];
4947
Folders = [];
5048
handler = new();
@@ -121,10 +119,7 @@ protected virtual string SerializeJson(/*FileConfiguration*/object from, ref str
121119
{
122120
toFile ??= ocelotConfigFileName;
123121
Files.Add(toFile); // register for disposing
124-
125-
// return JsonConvert.SerializeObject(from, Formatting.Indented);
126-
string json = JsonSerializer.Serialize(from, JsonWebIndented);
127-
return json;
122+
return JsonSerializer.Serialize(from, JsonWebIndented /*Formatting.Indented*/);
128123
}
129124

130125
public readonly static JsonSerializerOptions JsonWebIndented = new()
@@ -143,9 +138,7 @@ public void WithBasicConfiguration(WebHostBuilderContext hosting, IConfiguration
143138
config.AddJsonFile(ocelotConfigFileName, false, false);
144139
}
145140

146-
//public static void WithAddOcelot(IServiceCollection services) => services.AddOcelot();
147141
public static void WithAddOcelot(IServiceCollection services) => Ocelot.AddOcelot(services); // services.AddOcelot();
148-
149142
public static void WithUseOcelot(IApplicationBuilder app) => Ocelot.UseOcelot(app).Wait(); // app.UseOcelot().Wait();
150143
public static Task<IApplicationBuilder> WithUseOcelotAsync(IApplicationBuilder app) => Ocelot.UseOcelot(app); // app.UseOcelot();
151144

src/Box.cs

Lines changed: 20 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,29 @@
1-
using System.Collections;
1+
namespace Ocelot.Testing;
22

3-
namespace Ocelot.Testing;
4-
5-
public static class Box
3+
public class Box
64
{
7-
public static Box<T> In<T>(T instance) => new(instance);
8-
public static Box<T> With<T>(T instance) => In(instance);
5+
public static TResult In<TResult, TBoxee>(TBoxee instance)
6+
where TBoxee : class
7+
where TResult : Box<TBoxee>
8+
=> (TResult)Activator.CreateInstance(typeof(TResult), instance)!;
9+
public static TResult With<TResult, TBoxee>(TBoxee instance)
10+
where TBoxee : class
11+
where TResult : Box<TBoxee>
12+
=> In<TResult, TBoxee>(instance);
913
}
1014

11-
public class Box<T>
15+
public class Box<T> : Box
1216
{
13-
private readonly T _route;
14-
private readonly Type _type;
15-
public Box(T route)
17+
protected readonly T Instance;
18+
protected readonly Type Me;
19+
public Box(T instance, string? typeName = null)
1620
{
17-
if (route?.GetType().FullName != "Ocelot.Configuration.File.FileRoute")
18-
throw new ArgumentException("Is not type of Ocelot.Configuration.File.FileRoute", nameof(route));
19-
_route = route;
20-
_type = typeof(T); //route.GetType();
21+
if (instance?.GetType().FullName != typeName)
22+
throw new ArgumentException($"Is not type of {typeName ?? "?"}", nameof(instance));
23+
Instance = instance;
24+
Me = typeof(T);
2125
}
2226

23-
public T Out() => _route;
24-
public T Unbox() => Out();
25-
26-
public Box<T> Hosts(params object[] hosts)
27-
{
28-
//route.DownstreamHostAndPorts.AddRange(hosts);
29-
var property = _type.GetProperty("DownstreamHostAndPorts");
30-
IList? downstreamHostAndPorts = property?.GetValue(_route) as IList;
31-
ArgumentNullException.ThrowIfNull(downstreamHostAndPorts);
32-
for (int i = 0; i < hosts.Length; i++)
33-
{
34-
var host = hosts[i];
35-
if (host?.GetType().FullName != "Ocelot.Configuration.File.FileHostAndPort")
36-
throw new ArgumentException($"Argument at index {i} is not type of Ocelot.Configuration.File.FileHostAndPort", nameof(hosts));
37-
downstreamHostAndPorts.Add(host);
38-
}
39-
return this;
40-
}
41-
42-
public Box<T> Priority(int priority)
43-
{
44-
//route.Priority = priority;
45-
var property = _type.GetProperty("Priority");
46-
property?.SetValue(_route, priority);
47-
return this;
48-
}
49-
50-
public Box<T> Methods(params string[] methods)
51-
{
52-
//route.UpstreamHttpMethod = [.. methods];
53-
var property = _type.GetProperty("UpstreamHttpMethod");
54-
IList upstreamHttpMethod = property?.GetValue(_route) as IList
55-
?? throw new ArgumentNullException(nameof(upstreamHttpMethod));
56-
for (int i = 0; i < methods.Length; i++)
57-
{
58-
string method = methods[i];
59-
upstreamHttpMethod.Add(method);
60-
}
61-
return this;
62-
}
63-
64-
public Box<T> UpstreamHeaderTransform(params KeyValuePair<string, string>[] pairs)
65-
{
66-
//route.UpstreamHeaderTransform = new Dictionary<string, string>(pairs);
67-
var property = _type.GetProperty("UpstreamHeaderTransform");
68-
IDictionary<string, string> upstreamHeaderTransform = property?.GetValue(_route) as IDictionary<string, string>
69-
?? throw new ArgumentNullException(nameof(upstreamHeaderTransform));
70-
for (int i = 0; i < pairs.Length; i++)
71-
{
72-
var kv = pairs[i];
73-
upstreamHeaderTransform.Add(kv.Key, kv.Value);
74-
}
75-
return this;
76-
}
77-
78-
public Box<T> UpstreamHeaderTransform(string key, string value)
79-
{
80-
//route.UpstreamHeaderTransform.TryAdd(key, value);
81-
var property = _type.GetProperty("UpstreamHeaderTransform");
82-
IDictionary<string, string> upstreamHeaderTransform = property?.GetValue(_route) as IDictionary<string, string>
83-
?? throw new ArgumentNullException(nameof(upstreamHeaderTransform));
84-
upstreamHeaderTransform.TryAdd(key, value);
85-
return this;
86-
}
87-
88-
public Box<T> HttpHandlerOptions(/*FileHttpHandlerOptions*/object options)
89-
{
90-
//route.HttpHandlerOptions = options;
91-
if (options?.GetType().FullName != "Ocelot.Configuration.File.FileHttpHandlerOptions")
92-
throw new ArgumentException($"Is not type of Ocelot.Configuration.File.FileHttpHandlerOptions", nameof(options));
93-
var property = _type.GetProperty("HttpHandlerOptions");
94-
property?.SetValue(_route, options);
95-
return this;
96-
}
97-
98-
public Box<T> Key(string? key)
99-
{
100-
//route.Key = key;
101-
var property = _type.GetProperty("Key");
102-
property?.SetValue(_route, key);
103-
return this;
104-
}
105-
106-
public Box<T> UpstreamHost(string? upstreamHost)
107-
{
108-
//route.UpstreamHost = upstreamHost;
109-
var property = _type.GetProperty("UpstreamHost");
110-
property?.SetValue(_route, upstreamHost);
111-
return this;
112-
}
27+
public T Out() => Instance;
28+
public T Unbox() => Instance;
11329
}

src/FileRouteBox.cs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System.Collections;
2+
3+
namespace Ocelot.Testing;
4+
5+
public static class FileRouteBox
6+
{
7+
public static FileRouteBox<T> In<T>(T route) where T : class
8+
=> new(route);
9+
public static FileRouteBox<T> With<T>(T route) where T : class
10+
=> new(route);
11+
}
12+
13+
public class FileRouteBox<T> : Box<T> where T : class // FileRoute
14+
{
15+
public FileRouteBox(T route) : base(route, "Ocelot.Configuration.File.FileRoute")
16+
{ }
17+
18+
public static FileRouteBox<T> In(T route) => new(route);
19+
public static FileRouteBox<T> With(T route) => new(route);
20+
21+
public FileRouteBox<T> Hosts<H>(params H[] hosts) where H : class // FileHostAndPort
22+
{
23+
//route.DownstreamHostAndPorts.AddRange(hosts);
24+
var property = Me.GetProperty("DownstreamHostAndPorts");
25+
IList? downstreamHostAndPorts = property?.GetValue(Instance) as IList;
26+
ArgumentNullException.ThrowIfNull(downstreamHostAndPorts);
27+
for (int i = 0; i < hosts.Length; i++)
28+
{
29+
var host = hosts[i];
30+
if (host?.GetType().FullName != "Ocelot.Configuration.File.FileHostAndPort")
31+
throw new ArgumentException($"Argument at index {i} is not type of Ocelot.Configuration.File.FileHostAndPort", nameof(hosts));
32+
downstreamHostAndPorts.Add(host);
33+
}
34+
return this;
35+
}
36+
37+
public FileRouteBox<T> Priority(int priority)
38+
{
39+
//route.Priority = priority;
40+
var property = Me.GetProperty("Priority");
41+
property?.SetValue(Instance, priority);
42+
return this;
43+
}
44+
45+
public FileRouteBox<T> Methods(params string[] methods)
46+
{
47+
//route.UpstreamHttpMethod = [.. methods];
48+
var property = Me.GetProperty("UpstreamHttpMethod");
49+
IList upstreamHttpMethod = property?.GetValue(Instance) as IList
50+
?? throw new ArgumentNullException(nameof(upstreamHttpMethod));
51+
foreach (var method in methods)
52+
{
53+
upstreamHttpMethod.Add(method);
54+
}
55+
return this;
56+
}
57+
58+
public FileRouteBox<T> UpstreamHeaderTransform(params KeyValuePair<string, string>[] pairs)
59+
{
60+
//route.UpstreamHeaderTransform = new Dictionary<string, string>(pairs);
61+
var property = Me.GetProperty("UpstreamHeaderTransform");
62+
IDictionary<string, string> upstreamHeaderTransform = property?.GetValue(Instance) as IDictionary<string, string>
63+
?? throw new ArgumentNullException(nameof(upstreamHeaderTransform));
64+
for (int i = 0; i < pairs.Length; i++)
65+
{
66+
var kv = pairs[i];
67+
upstreamHeaderTransform.Add(kv.Key, kv.Value);
68+
}
69+
return this;
70+
}
71+
72+
public FileRouteBox<T> UpstreamHeaderTransform(string key, string value)
73+
{
74+
//route.UpstreamHeaderTransform.TryAdd(key, Instance);
75+
var property = Me.GetProperty("UpstreamHeaderTransform");
76+
IDictionary<string, string> upstreamHeaderTransform = property?.GetValue(Instance) as IDictionary<string, string>
77+
?? throw new ArgumentNullException(nameof(upstreamHeaderTransform));
78+
upstreamHeaderTransform.TryAdd(key, value);
79+
return this;
80+
}
81+
82+
public FileRouteBox<T> DownstreamHeaderTransform(string key, string value)
83+
{
84+
//route.DownstreamHeaderTransform.TryAdd(key, value);
85+
var property = Me.GetProperty("DownstreamHeaderTransform");
86+
IDictionary<string, string> downstreamHeaderTransform = property?.GetValue(Instance) as IDictionary<string, string>
87+
?? throw new ArgumentNullException(nameof(downstreamHeaderTransform));
88+
downstreamHeaderTransform.TryAdd(key, value);
89+
return this;
90+
}
91+
92+
public FileRouteBox<T> HandlerOptions<O>(O options) where O : class // FileHttpHandlerOptions
93+
{
94+
//route.HttpHandlerOptions = options;
95+
if (options?.GetType().FullName != "Ocelot.Configuration.File.FileHttpHandlerOptions")
96+
throw new ArgumentException($"Is not type of Ocelot.Configuration.File.FileHttpHandlerOptions", nameof(options));
97+
var property = Me.GetProperty("HttpHandlerOptions");
98+
property?.SetValue(Instance, options);
99+
return this;
100+
}
101+
102+
public FileRouteBox<T> Key(string? key)
103+
{
104+
//route.Key = key;
105+
var property = Me.GetProperty("Key");
106+
property?.SetValue(Instance, key);
107+
return this;
108+
}
109+
110+
public FileRouteBox<T> UpstreamHost(string? upstreamHost)
111+
{
112+
//route.UpstreamHost = upstreamHost;
113+
var property = Me.GetProperty("UpstreamHost");
114+
property?.SetValue(Instance, upstreamHost);
115+
return this;
116+
}
117+
}

src/FileRouteExtensions.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
1-
//using Ocelot.Configuration.File;
2-
namespace Ocelot.Testing;
1+
namespace Ocelot.Testing;
32

3+
/// <summary>
4+
/// For type: Ocelot.Configuration.File.FileRoute
5+
/// </summary>
46
public static class FileRouteExtensions
57
{
6-
public static /*FileRoute*/T WithHosts<T>(this /*FileRoute*/T route, params /*FileHostAndPort*/object[] hosts)
7-
=> Box.In(route).Hosts(hosts).Out(); //route.DownstreamHostAndPorts.AddRange(hosts);
8-
9-
public static /*FileRoute*/T WithPriority<T>(this /*FileRoute*/T route, int priority)
10-
=> Box.In(route).Priority(priority).Out(); //route.Priority = priority;
11-
12-
public static /*FileRoute*/T WithMethods<T>(this /*FileRoute*/T route, params string[] methods)
13-
=> Box.In(route).Methods(methods).Out(); //route.UpstreamHttpMethod = [.. methods];
14-
15-
public static /*FileRoute*/T WithUpstreamHeaderTransform<T>(this /*FileRoute*/T route, params KeyValuePair<string, string>[] pairs)
16-
=> Box.In(route).UpstreamHeaderTransform(pairs).Out(); //route.UpstreamHeaderTransform = new Dictionary<string, string>(pairs);
17-
18-
public static /*FileRoute*/T WithUpstreamHeaderTransform<T>(this /*FileRoute*/T route, string key, string value)
19-
=> Box.In(route).UpstreamHeaderTransform(key, value).Out(); //route.UpstreamHeaderTransform.TryAdd(key, value);
20-
21-
public static /*FileRoute*/T WithHttpHandlerOptions<T>(this /*FileRoute*/T route, /*FileHttpHandlerOptions*/object options)
22-
=> Box.In(route).HttpHandlerOptions(options).Out(); //route.HttpHandlerOptions = options;
23-
24-
public static /*FileRoute*/T WithKey<T>(this /*FileRoute*/T route, string? key)
25-
=> Box.In(route).Key(key).Out(); //route.Key = key;
26-
27-
public static /*FileRoute*/T WithUpstreamHost<T>(this /*FileRoute*/T route, string? upstreamHost)
28-
=> Box.In(route).UpstreamHost(upstreamHost).Out(); //route.UpstreamHost = upstreamHost;
8+
public static R WithHosts<R, H>(this R route, params H[] hosts)
9+
where R : class // FileRoute
10+
where H : class // FileHostAndPort
11+
=> Box.In<FileRouteBox<R>, R>(route).Hosts(hosts).Out(); //route.DownstreamHostAndPorts.AddRange(hosts);
12+
13+
public static R WithPriority<R>(this R route, int priority)
14+
where R : class // FileRoute
15+
=> FileRouteBox<R>.In(route).Priority(priority).Out(); //route.Priority = priority;
16+
17+
public static R WithMethods<R>(this R route, params string[] methods)
18+
where R : class // FileRoute
19+
=> FileRouteBox.In(route).Methods(methods).Out(); //route.UpstreamHttpMethod = [.. methods];
20+
21+
public static R WithUpstreamHeaderTransform<R>(this R route, params KeyValuePair<string, string>[] pairs)
22+
where R : class // FileRoute
23+
=> new FileRouteBox<R>(route).UpstreamHeaderTransform(pairs).Out(); //route.UpstreamHeaderTransform = new Dictionary<string, string>(pairs);
24+
25+
public static R WithUpstreamHeaderTransform<R>(this R route, string key, string value)
26+
where R : class // FileRoute
27+
=> new FileRouteBox<R>(route).UpstreamHeaderTransform(key, value).Out(); //route.UpstreamHeaderTransform.TryAdd(key, value);
28+
29+
public static R WithDownstreamHeaderTransform<R>(this R route, string key, string value)
30+
where R : class // FileRoute
31+
=> new FileRouteBox<R>(route).DownstreamHeaderTransform(key, value).Out(); //route.DownstreamHeaderTransform.TryAdd(key, value);
32+
33+
public static R WithHttpHandlerOptions<R, O>(this R route, O options)
34+
where R : class // FileRoute
35+
where O : class // FileHttpHandlerOptions
36+
=> new FileRouteBox<R>(route).HandlerOptions(options).Out(); //route.HttpHandlerOptions = options;
37+
38+
public static R WithKey<R>(this R route, string? key)
39+
where R : class // FileRoute
40+
=> new FileRouteBox<R>(route).Key(key).Out(); //route.Key = key;
41+
42+
public static R WithUpstreamHost<R>(this R route, string? upstreamHost)
43+
where R : class // FileRoute
44+
=> new FileRouteBox<R>(route).UpstreamHost(upstreamHost).Out(); //route.UpstreamHost = upstreamHost;
2945
}

0 commit comments

Comments
 (0)