-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem.cs
More file actions
217 lines (186 loc) · 6.83 KB
/
Copy pathProblem.cs
File metadata and controls
217 lines (186 loc) · 6.83 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.Runtime.CompilerServices;
using System.Text;
namespace RoyalCode.SmartProblems;
#pragma warning disable S4050 // implement operatiors == != Equals GetHashCode
/// <summary>
/// An object that represents a problem that occurred in the system, with details and category.
/// </summary>
public sealed class Problem
{
#region implicit operators
/// <summary>
/// Converts a <see cref="Problem"/> to a <see cref="Task"/> of <see cref="Result"/>.
/// </summary>
/// <param name="problem"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Task<Result>(Problem problem) => Task.FromResult(new Result(problem));
/// <summary>
/// Creates a collection of problems with both problems.
/// </summary>
/// <param name="a">A problem to add.</param>
/// <param name="b">Other problem to add.</param>
/// <returns>A new instance of the problems collection (<see cref="Problems"/>).</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Problems operator +(Problem a, Problem b)
{
Problems collection = [a, b];
return collection;
}
#endregion
#region Static functions
/// <summary>
/// Default function to convert the problem to a string.
/// </summary>
public Func<Problem, string> ToStringFactory { get; set; } = DefaultToString;
#endregion
private string? _property;
/// <summary>
/// <para>
/// Description of the problem, like a message to the user.
/// </para>
/// </summary>
public required string Detail { get; init; }
/// <summary>
/// <para>
/// The category of the problem.
/// </para>
/// </summary>
public required ProblemCategory Category { get; init; }
/// <summary>
/// <para>
/// The type id of the problem, related to the problem details type.
/// </para>
/// </summary>
public string? TypeId { get; init; }
/// <summary>
/// Optional, the property that the problem is related to.
/// </summary>
public string? Property { get => _property; init => _property = value; }
/// <summary>
/// Optional, extra information about the problem.
/// </summary>
public IDictionary<string, object?>? Extensions { get; set; }
/// <summary>
/// Adds a new key-value pair to the extensions.
/// </summary>
/// <param name="key">The key of the data.</param>
/// <param name="value">The value of the data.</param>
/// <returns>
/// The same instance of the problem.
/// </returns>
public Problem With(string key, object? value)
{
Extensions ??= new Dictionary<string, object?>(StringComparer.Ordinal);
Extensions[key] = value;
return this;
}
/// <summary>
/// Adds a new key-value pair to the extensions.
/// </summary>
/// <param name="key">The key of the data.</param>
/// <param name="value">The value of the data.</param>
/// <returns>
/// The same instance of the problem.
/// </returns>
public Problem With<TEnum>(string key, TEnum value)
where TEnum : Enum
{
return With(key, value.ToString());
}
/// <summary>
/// Chains a property to the beginning of the current property, forming a nested property (e.g. Parent.Current).
/// </summary>
/// <remarks>
/// When <see cref="Property"/> or <paramref name="parentProperty"/> is null or empty, this method does nothing.
/// </remarks>
/// <param name="parentProperty">Name of the property to be chained.</param>
/// <returns>The same instance of the problem.</returns>
public Problem ChainProperty(string? parentProperty)
{
if (string.IsNullOrEmpty(parentProperty) || string.IsNullOrEmpty(_property))
return this;
_property = $"{parentProperty}.{Property}";
return this;
}
/// <summary>
/// Chains a collection property with index to the beginning of the current property (e.g. Parent[0].Current).
/// </summary>
/// <remarks>
/// When <see cref="Property"/> or <paramref name="parentProperty"/> is null or empty, this method does nothing.
/// </remarks>
/// <param name="parentProperty">Name of the property to be chained.</param>
/// <param name="index">Collection index.</param>
/// <returns>The same instance of the problem.</returns>
public Problem ChainProperty(string? parentProperty, int index)
{
if (string.IsNullOrEmpty(parentProperty) || string.IsNullOrEmpty(_property))
return this;
_property = $"{parentProperty}[{index}].{Property}";
return this;
}
/// <summary>
/// Replaces the current property with a new one.
/// </summary>
/// <param name="newProperty">The new property name.</param>
/// <returns>The same instance of the problem.</returns>
public Problem ReplaceProperty(string? newProperty)
{
_property = newProperty;
return this;
}
/// <inheritdoc />
public override string ToString() => ToStringFactory(this);
/// <summary>
/// Default function to convert the problem to a string.
/// </summary>
/// <param name="problem">The problem to convert.</param>
/// <returns>A string representation of the problem.</returns>
private static string DefaultToString(Problem problem)
{
var builder = new StringBuilder();
builder.Append($"Category: {problem.Category}, ");
builder.Append($"Details: {problem.Detail}, ");
if (!string.IsNullOrEmpty(problem.Property))
{
builder.Append($"Property: {problem.Property}, ");
}
if (!string.IsNullOrEmpty(problem.TypeId))
{
builder.Append($"TypeId: {problem.TypeId}, ");
}
if (problem.Extensions != null && problem.Extensions.Count > 0)
{
builder.Append("Extensions: { ");
foreach (var kvp in problem.Extensions)
{
builder.Append($"{kvp.Key}: {kvp.Value}, ");
}
// Remove a última vírgula e espaço
builder.Length -= 2;
builder.Append(" }");
}
else
{
// Remove a última vírgula e espaço se não houver extensões
builder.Length -= 2;
}
return builder.ToString();
}
/// <summary>
/// Converts the problem to a <see cref="Result"/> object.
/// </summary>
/// <returns></returns>
public Result AsResult()
{
return new Result(this);
}
/// <summary>
/// Converts the problem to a <see cref="Result{TValue}"/> object with a specified value type.
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <returns></returns>
public Result<TValue> AsResult<TValue>()
{
return new Result<TValue>(this);
}
}