forked from votrongdao/FlowX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormulas.cs
More file actions
222 lines (194 loc) · 9.53 KB
/
Copy pathFormulas.cs
File metadata and controls
222 lines (194 loc) · 9.53 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
218
219
220
221
222
using System.Globalization;
using FlowX;
namespace Crm;
/// <summary>What a formula does to its two operands.</summary>
/// <remarks>
/// <strong>Six, and each is a total function.</strong> Closed for the reason every other
/// vocabulary in this sample is: an open set would be a string nothing dispatches on. Each has an
/// answer for a missing operand, which is stated at the arm that gives it — a formula that threw
/// on an unset field would make one blank cell fail the whole write.
/// </remarks>
public enum FormulaOperation
{
/// <summary>Joins the two as text.</summary>
Concat = 0,
/// <summary>Adds them as numbers.</summary>
Add = 1,
/// <summary>Subtracts the right from the left.</summary>
Subtract = 2,
/// <summary>Multiplies them.</summary>
Multiply = 3,
/// <summary>Divides the left by the right.</summary>
Divide = 4,
/// <summary>The left, or the right when the left is not set.</summary>
Coalesce = 5,
}
/// <summary>Declares a field computed from other fields of the same record.</summary>
/// <param name="Field">The field that holds the answer. Becomes read-only.</param>
/// <param name="Operation">What to do to the operands.</param>
/// <param name="Left">The left operand. Always a field of the same owner.</param>
/// <param name="Right">The right operand as a field, or null when <paramref name="Literal"/> is given.</param>
/// <param name="Literal">The right operand as a constant, or null when <paramref name="Right"/> is given.</param>
public sealed record DefineFormula(
Guid Field,
FormulaOperation Operation,
string Left,
string? Right,
string? Literal);
/// <summary>The formula that was declared.</summary>
/// <param name="FormulaId">Its id.</param>
/// <param name="Field">The field it computes.</param>
public sealed record FormulaDefined(Guid FormulaId, Guid Field);
/// <summary>A declared formula, as much of it as evaluating one needs.</summary>
/// <param name="Id">The formula.</param>
/// <param name="FieldName">The field that holds the answer.</param>
/// <param name="Operation">What to do.</param>
/// <param name="Left">The left operand's field name.</param>
/// <param name="Right">The right operand's field name, or null.</param>
/// <param name="Literal">The right operand's constant, or null.</param>
public sealed record FormulaRow(
Guid Id,
string FieldName,
FormulaOperation Operation,
string Left,
string? Right,
string? Literal);
/// <summary>Refusals the formula machinery can produce.</summary>
public static class FormulaErrors
{
/// <summary>The right operand was given as a field and a constant, or as neither.</summary>
public static Error OperandIsAmbiguous() =>
new(
"crm.formula_operand_ambiguous",
"A formula's right operand is a field or a constant, and this named neither or both.",
ErrorCategory.Validation);
/// <summary>An operand is not a field of the same owner.</summary>
/// <param name="field">What was named.</param>
public static Error OperandNotDeclared(string field) =>
new Error(
"crm.formula_operand_not_declared",
$"'{field}' is not a field of the object this formula's field belongs to.",
ErrorCategory.Validation)
.With("field", field);
/// <summary>An operand is itself computed.</summary>
/// <param name="field">What was named.</param>
/// <remarks>
/// <strong>Refused so that evaluation stays a single pass.</strong> A formula over a formula
/// needs an order to evaluate them in, which is a dependency graph, which can hold a cycle.
/// An administrator who wants one declares the intermediate and reads it — which is better
/// than a nested expression, because the intermediate is nameable, queryable and checkable.
/// </remarks>
public static Error OperandIsComputed(string field) =>
new Error(
"crm.formula_operand_is_computed",
$"'{field}' is itself computed, and a formula may not read another one. Declare the " +
"intermediate field and read that.",
ErrorCategory.Validation)
.With("field", field);
/// <summary>An arithmetic formula was given a constant that is not a number.</summary>
/// <param name="operation">Which operation.</param>
/// <param name="literal">What was sent.</param>
public static Error LiteralIsNotNumeric(FormulaOperation operation, string literal) =>
new Error(
"crm.formula_literal_not_numeric",
$"'{operation}' works on numbers, and '{literal}' is not one.",
ErrorCategory.Validation)
.With("operation", operation.ToString())
.With("literal", literal);
}
/// <summary>
/// Evaluating a formula — a pure function of one record's values.
/// </summary>
/// <remarks>
/// <para>
/// <strong>Nothing here reads a database, a clock or another formula.</strong> The declarations
/// are read once and every formula is evaluated against the same snapshot of the caller's values,
/// in one pass and in no particular order, which is exactly what refusing a formula over a
/// formula buys.
/// </para>
/// <para>
/// <strong>Every arm answers for a missing operand.</strong> A formula that threw on an unset
/// field would make one blank cell fail a whole write, and a formula that silently produced zero
/// would make an empty column read as a real number. Null is the answer where there is nothing
/// to compute, which stores as JSON null and reads back as unset.
/// </para>
/// </remarks>
public static class Formulas
{
/// <summary>What a formula evaluates to over these values.</summary>
/// <param name="formula">The declaration.</param>
/// <param name="values">The record's values, by field name.</param>
/// <returns>The answer, or null when there is nothing to compute.</returns>
/// <exception cref="ArgumentNullException">Any argument is null.</exception>
public static string? Evaluate(
FormulaRow formula,
IReadOnlyDictionary<string, string?> values)
{
ArgumentNullException.ThrowIfNull(formula);
ArgumentNullException.ThrowIfNull(values);
var left = values.GetValueOrDefault(formula.Left);
var right = formula.Literal ?? values.GetValueOrDefault(formula.Right!);
return formula.Operation switch
{
// Coalesce is the one operation with an answer when the left is missing — that is
// what it is for — so it comes before the shared guard below.
FormulaOperation.Coalesce => left ?? right,
// Text, and a missing operand contributes nothing rather than the word "null".
FormulaOperation.Concat when left is not null || right is not null =>
(left ?? string.Empty) + (right ?? string.Empty),
FormulaOperation.Add => Arithmetic(left, right, static (a, b) => a + b),
FormulaOperation.Subtract => Arithmetic(left, right, static (a, b) => a - b),
FormulaOperation.Multiply => Arithmetic(left, right, static (a, b) => a * b),
// Division by zero is null rather than an exception, for the reason the class remarks
// give: a formula must not be able to fail a write. A row whose divisor is zero has
// no answer, and saying so is what a blank cell means.
FormulaOperation.Divide => Arithmetic(
left, right, static (a, b) => b == 0m ? null : a / b),
_ => null,
};
}
/// <summary>Whether an operation works on numbers.</summary>
/// <param name="operation">The operation.</param>
/// <returns>Whether both operands have to parse.</returns>
public static bool IsArithmetic(FormulaOperation operation) =>
operation is FormulaOperation.Add
or FormulaOperation.Subtract
or FormulaOperation.Multiply
or FormulaOperation.Divide;
/// <summary>Every formula's answer, merged over the values it was given.</summary>
/// <param name="formulas">The declarations for the entity.</param>
/// <param name="values">What is being written.</param>
/// <returns>The values, with every computed field set.</returns>
/// <exception cref="ArgumentNullException">Any argument is null.</exception>
/// <remarks>
/// <strong>Read from the input, written to the copy.</strong> Every formula sees the caller's
/// values and none sees another's answer, which is what makes the order they are evaluated in
/// irrelevant — and therefore what makes "no formula over a formula" a rule with teeth rather
/// than a convention.
/// </remarks>
public static IReadOnlyDictionary<string, string?> Apply(
IEnumerable<FormulaRow> formulas,
IReadOnlyDictionary<string, string?> values)
{
ArgumentNullException.ThrowIfNull(formulas);
ArgumentNullException.ThrowIfNull(values);
var computed = new Dictionary<string, string?>(values, StringComparer.Ordinal);
foreach (var formula in formulas)
{
computed[formula.FieldName] = Evaluate(formula, values);
}
return computed;
}
private static string? Arithmetic(
string? left,
string? right,
Func<decimal, decimal, decimal?> apply)
{
if (!decimal.TryParse(left, NumberStyles.Number, CultureInfo.InvariantCulture, out var a) ||
!decimal.TryParse(right, NumberStyles.Number, CultureInfo.InvariantCulture, out var b))
{
return null;
}
return apply(a, b)?.ToString(CultureInfo.InvariantCulture);
}
}