-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
FormMappingContext.cs
157 lines (135 loc) · 6.14 KB
/
FormMappingContext.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Components.Forms.Mapping;
namespace Microsoft.AspNetCore.Components.Forms;
/// <summary>
/// The context associated with a given form mapping operation.
/// </summary>
public sealed class FormMappingContext
{
private Dictionary<string, FormMappingError>? _errors;
private List<KeyValuePair<string, FormMappingError>>? _pendingErrors;
private Dictionary<string, Dictionary<string, FormMappingError>>? _errorsByFormName;
internal FormMappingContext(string mappingScopeName)
{
ArgumentNullException.ThrowIfNull(mappingScopeName);
MappingScopeName = mappingScopeName;
}
/// <summary>
/// The mapping scope name.
/// </summary>
public string MappingScopeName { get; }
/// <summary>
/// Retrieves the list of errors for a given model key.
/// </summary>
/// <param name="key">The key used to identify the specific part of the model.</param>
/// <returns>The list of errors associated with that part of the model if any.</returns>
public FormMappingError? GetErrors(string key) =>
_errors?.TryGetValue(key, out var mappingError) == true ? mappingError : null;
/// <summary>
/// Retrieves the list of errors for a given model key.
/// </summary>
/// <param name="key">The key used to identify the specific part of the model.</param>
/// <param name="formName">Form name for a form under this context.</param>
/// <returns>The list of errors associated with that part of the model if any.</returns>
public FormMappingError? GetErrors(string formName, string key) =>
_errorsByFormName?.TryGetValue(formName, out var formErrors) == true &&
formErrors.TryGetValue(key, out var mappingError) == true ? mappingError : null;
/// <summary>
/// Retrieves all the errors for the model.
/// </summary>
/// <returns>The list of errors associated with the model if any.</returns>
public IEnumerable<FormMappingError> GetAllErrors()
{
return GetAllErrorsCore(_errors);
}
private static IEnumerable<FormMappingError> GetAllErrorsCore(Dictionary<string, FormMappingError>? errors)
{
if (errors == null)
{
return Array.Empty<FormMappingError>();
}
return errors.Values;
}
/// <summary>
/// Retrieves all the errors for the model.
/// </summary>
/// <param name="formName">Form name for a form under this context.</param>
/// <returns>The list of errors associated with the model if any.</returns>
public IEnumerable<FormMappingError> GetAllErrors(string formName)
{
return _errorsByFormName?.TryGetValue(formName, out var formErrors) == true ?
GetAllErrorsCore(formErrors) :
Array.Empty<FormMappingError>();
}
/// <summary>
/// Retrieves the attempted value that failed to map for a given model key.
/// </summary>
/// <param name="key">The key used to identify the specific part of the model.</param>
/// <returns>The attempted value associated with that part of the model if any.</returns>
public string? GetAttemptedValue(string key) =>
_errors?.TryGetValue(key, out var mappingError) == true ? mappingError.AttemptedValue : null;
/// <summary>
/// Retrieves the attempted value that failed to map for a given model key.
/// </summary>
/// <param name="formName">Form name for a form under this context.</param>
/// <param name="key">The key used to identify the specific part of the model.</param>
/// <returns>The attempted value associated with that part of the model if any.</returns>
public string? GetAttemptedValue(string formName, string key) =>
_errorsByFormName?.TryGetValue(formName, out var formErrors) == true &&
formErrors.TryGetValue(key, out var mappingError) ? mappingError.AttemptedValue : null;
internal void AddError(string key, FormattableString error, string? attemptedValue)
{
_errors ??= new Dictionary<string, FormMappingError>();
AddErrorCore(_errors, key, error, attemptedValue, ref _pendingErrors);
}
private static void AddErrorCore(Dictionary<string, FormMappingError> errors, string key, FormattableString error, string? attemptedValue, ref List<KeyValuePair<string, FormMappingError>>? pendingErrors)
{
if (!errors.TryGetValue(key, out var mappingError))
{
mappingError = new FormMappingError(key, new List<FormattableString>() { error }, attemptedValue);
errors.Add(key, mappingError);
pendingErrors ??= new();
pendingErrors.Add(new KeyValuePair<string, FormMappingError>(key, mappingError));
}
else
{
mappingError.AddError(error);
}
}
internal void AddError(string formName, string key, FormattableString error, string? attemptedValue)
{
_errorsByFormName ??= new Dictionary<string, Dictionary<string, FormMappingError>>();
if (!_errorsByFormName.TryGetValue(formName, out var formErrors))
{
formErrors = new Dictionary<string, FormMappingError>();
_errorsByFormName.Add(formName, formErrors);
}
AddErrorCore(formErrors, key, error, attemptedValue, ref _pendingErrors);
}
internal void AttachParentValue(string key, object value)
{
if (_pendingErrors == null)
{
return;
}
for (var i = 0; i < _pendingErrors.Count; i++)
{
var (errorKey, error) = _pendingErrors[i];
if (!errorKey.StartsWith(key, StringComparison.Ordinal))
{
throw new InvalidOperationException($"'{errorKey}' does must start with '{key}'");
}
error.Container = value;
}
_pendingErrors.Clear();
}
internal void SetErrors(string formName, FormMappingContext childContext)
{
if (_errorsByFormName == null || !_errorsByFormName.TryGetValue(formName, out var formErrors))
{
return;
}
childContext._errors = formErrors;
}
}