📄
core/src/MessageOptions.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders.Physical; using Microsoft.Extensions.Options; namespace Cfa; public sealed class MessageOptions { [Required] [ValidateEnumeratedItems] public List<MessageRoute> MessageRoutes { get; set; } = []; public static MessageOptions LoadJson(string? path = null, bool optional = false, string? localPath = null) { var userPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".cfa.json"); return LoadLayeredJson( path, optional, userPath, localPath ?? Path.Combine(Environment.CurrentDirectory, ".cfa.json") ); } internal static MessageOptions LoadLayeredJson( string? path, bool optional, string userPath, string? localPath = null ) { var files = new List<PhysicalFileProvider>(); try { using var configuration = new ConfigurationManager(); AddFile(userPath, true); if (localPath is not null) AddFile(localPath, true); if (path is not null) AddFile(path, optional); var services = new ServiceCollection(); services.AddOptions<MessageOptions>().Bind(configuration).Validate<MessageOptionsValidator>(); using var provider = services.BuildServiceProvider(); return provider.GetRequiredService<IOptions<MessageOptions>>().Value; void AddFile(string file, bool isOptional) { var fullPath = Path.GetFullPath(file); var directory = Path.GetDirectoryName(fullPath)!; if (isOptional && !Directory.Exists(directory)) return; var source = new PhysicalFileProvider(directory, ExclusionFilters.None); files.Add(source); configuration.AddJsonFile(source, Path.GetFileName(fullPath), isOptional, reloadOnChange: false); } } finally { foreach (var source in files) source.Dispose(); } } } public sealed class MessageRoute { [Required] [ValidateObjectMembers] public PublisherRule Publisher { get; set; } = new(); [Required] #pragma warning disable IL2026 // T[] implements ICollection; MinLength does not need reflection. [MinLength(1)] #pragma warning restore IL2026 [ValidateEnumeratedItems] public RegistrationRule[] Registrations { get; set; } = []; } public sealed class PublisherRule { [Required] [ValidateObjectMembers] public MethodSelector Method { get; set; } = new(); [Range(0, int.MaxValue)] public int MessageArgument { get; set; } } public sealed class RegistrationRule { [Required] [ValidateObjectMembers] public MethodSelector Method { get; set; } = new(); [Range(0, int.MaxValue)] public int MessageTypeArgument { get; set; } [Range(0, int.MaxValue)] public int HandlerArgument { get; set; } } public sealed class MethodSelector { [Required] public string Type { get; set; } = ""; [Required] public string Name { get; set; } = ""; [Range(0, int.MaxValue)] public int? GenericArity { get; set; } [Range(0, int.MaxValue)] public int? ParameterCount { get; set; } } [OptionsValidator] internal partial class MessageOptionsValidator : IValidateOptions<MessageOptions>;