Skip to content

Commit 2984644

Browse files
committed
Add support for number operations.
1 parent d6fe738 commit 2984644

File tree

7 files changed

+142
-65
lines changed

7 files changed

+142
-65
lines changed

lib/src/api/models/action/action.dart

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,81 @@ abstract class ActionModel with SerializableMixin {
152152
R? accept<R>(ActionVisitor<R> visitor);
153153
}
154154

155+
/// Defines the operation to be performed on a list type variable.
156+
enum ListOperation {
157+
/// Replace entire list.
158+
replace,
159+
160+
/// Replaces the list with a custom variable.
161+
set,
162+
163+
/// Add [newValue] to the list.
164+
add,
165+
166+
/// Insert [newValue] at [index] in the list.
167+
insert,
168+
169+
/// Remove value at [index] from the list.
170+
removeAt,
171+
172+
/// Remove [newValue] from the list.
173+
remove,
174+
175+
/// Update value at [index] with [newValue] in the list.
176+
update;
177+
178+
/// Returns a string representation of this enum.
179+
String get prettify => switch (this) {
180+
ListOperation.replace => 'Replace',
181+
ListOperation.add => 'Add',
182+
ListOperation.set => 'Set',
183+
ListOperation.insert => 'Insert',
184+
ListOperation.removeAt => 'Remove At',
185+
ListOperation.remove => 'Remove',
186+
ListOperation.update => 'Update'
187+
};
188+
}
189+
190+
/// Defines the operation to be performed on a map type variable.
191+
enum MapOperation {
192+
/// Replace entire map.
193+
replace,
194+
195+
/// Add [newValue] to the map.
196+
add,
197+
198+
/// Remove [key] from the map.
199+
remove,
200+
201+
/// Update value at [key] with [newValue] in the map.
202+
update,
203+
204+
/// Same as replace but meant to replace the entire map with a new map from
205+
/// a variable.
206+
set;
207+
208+
/// Returns a string representation of this enum.
209+
String get prettify => switch (this) {
210+
MapOperation.replace => 'Replace',
211+
MapOperation.add => 'Add',
212+
MapOperation.remove => 'Remove',
213+
MapOperation.update => 'Update',
214+
MapOperation.set => 'Set'
215+
};
216+
}
217+
218+
/// Represents operations that can be performed on a number.
219+
enum NumberOperation {
220+
/// Replace the value at the given index.
221+
set,
222+
223+
/// Add the value to the existing value.
224+
add,
225+
226+
/// Subtract the value from the existing value.
227+
subtract,
228+
}
229+
155230
/// A base action class that represents an action that can be performed to
156231
/// modify some data in some way depending on the data type.
157232
abstract interface class DataOperationInterface {
@@ -174,4 +249,7 @@ abstract interface class DataOperationInterface {
174249

175250
/// Key of the value to be updated/removed/inserted in the map.
176251
abstract final String mapKey;
177-
}
252+
253+
/// Operation to be performed on the int/double type variable.
254+
abstract final NumberOperation numberOperation;
255+
}

lib/src/api/models/action/set_cloud_storage_action.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ class UpdateDocumentSubAction extends SetCloudStorageSubAction
298298
@override
299299
final bool toggled;
300300

301+
@override
302+
final NumberOperation numberOperation;
303+
301304
/// Creates a new [UpdateDocumentSubAction].
302305
const UpdateDocumentSubAction({
303306
this.path = '',
@@ -312,6 +315,7 @@ class UpdateDocumentSubAction extends SetCloudStorageSubAction
312315
this.newValue = '',
313316
this.toggled = false,
314317
this.useRawValue = false,
318+
this.numberOperation = NumberOperation.set,
315319
}) : super(operation: SetCloudStorageOperation.updateDocument);
316320

317321
/// Duplicates this [UpdateDocumentSubAction] with given data overrides.
@@ -330,6 +334,7 @@ class UpdateDocumentSubAction extends SetCloudStorageSubAction
330334
String? newValue,
331335
bool? toggled,
332336
bool? useRawValue,
337+
NumberOperation? numberOperation,
333338
}) {
334339
return UpdateDocumentSubAction(
335340
path: path ?? this.path,
@@ -344,6 +349,7 @@ class UpdateDocumentSubAction extends SetCloudStorageSubAction
344349
newValue: newValue ?? this.newValue,
345350
toggled: toggled ?? this.toggled,
346351
useRawValue: useRawValue ?? this.useRawValue,
352+
numberOperation: numberOperation ?? this.numberOperation,
347353
);
348354
}
349355

@@ -369,5 +375,6 @@ class UpdateDocumentSubAction extends SetCloudStorageSubAction
369375
newValue,
370376
toggled,
371377
useRawValue,
378+
numberOperation,
372379
];
373380
}

lib/src/api/models/action/set_cloud_storage_action.g.dart

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/models/action/set_storage_action.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class SetStorageAction extends ActionModel
7171
@override
7272
final bool toggled;
7373

74+
@override
75+
final NumberOperation numberOperation;
76+
7477
/// Creates a new [SetStorageAction].
7578
SetStorageAction({
7679
this.key = 'key',
@@ -82,6 +85,7 @@ class SetStorageAction extends ActionModel
8285
this.index = '0',
8386
this.mapOperation = MapOperation.replace,
8487
this.mapKey = 'key',
88+
this.numberOperation = NumberOperation.set,
8589
}) : super(type: ActionType.setStorage);
8690

8791
/// Duplicates this [SetStorageAction] with given data overrides.
@@ -95,6 +99,7 @@ class SetStorageAction extends ActionModel
9599
String? index,
96100
MapOperation? mapOperation,
97101
String? mapKey,
102+
NumberOperation? numberOperation,
98103
}) {
99104
// final String? sanitizedValue = value == null
100105
// ? null
@@ -110,6 +115,7 @@ class SetStorageAction extends ActionModel
110115
index: index ?? this.index,
111116
mapOperation: mapOperation ?? this.mapOperation,
112117
mapKey: mapKey ?? this.mapKey,
118+
numberOperation: numberOperation ?? this.numberOperation,
113119
);
114120
}
115121

@@ -124,6 +130,7 @@ class SetStorageAction extends ActionModel
124130
index,
125131
mapOperation,
126132
mapKey,
133+
numberOperation,
127134
];
128135

129136
/// Creates a new [SetStorageAction] instance from a JSON data.

lib/src/api/models/action/set_storage_action.g.dart

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/models/action/set_variable_action.dart

Lines changed: 16 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,6 @@ import 'action.dart';
1212

1313
part 'set_variable_action.g.dart';
1414

15-
/// Defines the operation to be performed on a list type variable.
16-
enum ListOperation {
17-
/// Replace entire list.
18-
replace,
19-
20-
/// Replaces the list with a custom variable.
21-
set,
22-
23-
/// Add [newValue] to the list.
24-
add,
25-
26-
/// Insert [newValue] at [index] in the list.
27-
insert,
28-
29-
/// Remove value at [index] from the list.
30-
removeAt,
31-
32-
/// Remove [newValue] from the list.
33-
remove,
34-
35-
/// Update value at [index] with [newValue] in the list.
36-
update;
37-
38-
/// Returns a string representation of this enum.
39-
String get prettify => switch (this) {
40-
ListOperation.replace => 'Replace',
41-
ListOperation.add => 'Add',
42-
ListOperation.set => 'Set',
43-
ListOperation.insert => 'Insert',
44-
ListOperation.removeAt => 'Remove At',
45-
ListOperation.remove => 'Remove',
46-
ListOperation.update => 'Update'
47-
};
48-
}
49-
50-
/// Defines the operation to be perfomed on a map type variable.
51-
enum MapOperation {
52-
/// Replace entire map.
53-
replace,
54-
55-
/// Add [newValue] to the map.
56-
add,
57-
58-
/// Remove [key] from the map.
59-
remove,
60-
61-
/// Update value at [key] with [newValue] in the map.
62-
update,
63-
64-
/// Same as replace but meant to replace the entire map with a new map from
65-
/// a variable.
66-
set;
67-
68-
/// Returns a string representation of this enum.
69-
String get prettify => switch (this) {
70-
MapOperation.replace => 'Replace',
71-
MapOperation.add => 'Add',
72-
MapOperation.remove => 'Remove',
73-
MapOperation.update => 'Update',
74-
MapOperation.set => 'Set'
75-
};
76-
}
77-
7815
/// An action that sets value of a variable.
7916
@JsonSerializable()
8017
class SetVariableAction extends ActionModel
@@ -101,6 +38,9 @@ class SetVariableAction extends ActionModel
10138
@override
10239
final bool toggled;
10340

41+
@override
42+
final NumberOperation numberOperation;
43+
10444
/// Creates a new [SetValueAction].
10545
SetVariableAction({
10646
required this.variable,
@@ -110,10 +50,20 @@ class SetVariableAction extends ActionModel
11050
this.index = '0',
11151
this.mapOperation = MapOperation.replace,
11252
this.mapKey = 'key',
53+
this.numberOperation = NumberOperation.set,
11354
}) : super(type: ActionType.setVariable);
11455

11556
@override
116-
List<Object?> get props => [variable, newValue];
57+
List<Object?> get props => [
58+
variable,
59+
newValue,
60+
toggled,
61+
listOperation,
62+
index,
63+
mapOperation,
64+
mapKey,
65+
numberOperation,
66+
];
11767

11868
/// Duplicates this [SetVariableAction] with given data overrides.
11969
SetVariableAction copyWith({
@@ -124,6 +74,7 @@ class SetVariableAction extends ActionModel
12474
String? index,
12575
MapOperation? mapOperation,
12676
String? mapKey,
77+
NumberOperation? numberOperation,
12778
}) =>
12879
SetVariableAction(
12980
variable: variable ?? this.variable,
@@ -133,6 +84,7 @@ class SetVariableAction extends ActionModel
13384
index: index ?? this.index,
13485
mapOperation: mapOperation ?? this.mapOperation,
13586
mapKey: mapKey ?? this.mapKey,
87+
numberOperation: numberOperation ?? this.numberOperation,
13688
);
13789

13890
/// Creates a new [SetVariableAction] instance from a JSON data.

lib/src/api/models/action/set_variable_action.g.dart

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)