-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathContext.cs
58 lines (48 loc) · 2.4 KB
/
Context.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using DefaultDocumentation.Api;
using Newtonsoft.Json.Linq;
namespace DefaultDocumentation.Internal
{
internal class Context : IContext
{
private readonly JObject _configuration;
public Context(
JObject configuration,
Type[] availableTypes)
{
_configuration = configuration;
string fileNameFactory = GetSetting<string>(nameof(IRawSettings.FileNameFactory));
FileNameFactory = string.IsNullOrEmpty(fileNameFactory) ? null : availableTypes
.Where(t => typeof(IFileNameFactory).IsAssignableFrom(t) && !t.IsAbstract)
.Select(t => (IFileNameFactory)Activator.CreateInstance(t))
.LastOrDefault(f => f.Name == fileNameFactory || $"{f.GetType().FullName} {f.GetType().Assembly.GetName().Name}" == fileNameFactory)
?? throw new Exception($"FileNameFactory '{fileNameFactory}' not found");
string[] sections = GetSetting<string[]>(nameof(IRawSettings.Sections));
if (sections != null)
{
Dictionary<string, ISection> availableSections = availableTypes
.Where(t => typeof(ISection).IsAssignableFrom(t) && !t.IsAbstract)
.Select(t => (ISection)Activator.CreateInstance(t))
.GroupBy(w => w.Name)
.ToDictionary(w => w.Key, w => w.Last());
Sections = sections
.Select(id =>
availableSections.TryGetValue(id, out ISection section)
? section
: availableTypes
.Where(t => typeof(ISection).IsAssignableFrom(t) && !t.IsAbstract && $"{t.FullName} {t.Assembly.GetName().Name}" == id)
.Select(t => (ISection)Activator.CreateInstance(t))
.FirstOrDefault()
?? throw new Exception($"Section '{id}' not found"))
.ToArray();
}
}
#region IContext
public IFileNameFactory FileNameFactory { get; }
public IEnumerable<ISection> Sections { get; }
public T GetSetting<T>(string name) => _configuration.TryGetValue(name, StringComparison.OrdinalIgnoreCase, out JToken value) ? value.ToObject<T>() : default;
#endregion
}
}