|
1 | | -using System.Collections; |
| 1 | +namespace Ocelot.Testing; |
2 | 2 |
|
3 | | -namespace Ocelot.Testing; |
4 | | - |
5 | | -public static class Box |
| 3 | +public class Box |
6 | 4 | { |
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); |
9 | 13 | } |
10 | 14 |
|
11 | | -public class Box<T> |
| 15 | +public class Box<T> : Box |
12 | 16 | { |
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) |
16 | 20 | { |
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); |
21 | 25 | } |
22 | 26 |
|
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; |
113 | 29 | } |
0 commit comments