Skip to content

Commit 79abf2c

Browse files
committed
Computed variables: VariableData.formula
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AjsYaRkTHNKszSHjd3j264
1 parent 65103be commit 79abf2c

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎lib/src/api/models/variables_model.dart‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,24 @@ class VariableData
114114
/// metadata or any other information that is not part of the core variable.
115115
final Map<String, dynamic> extra;
116116

117+
/// A formula expression (what goes inside `${= ...}`) that computes this
118+
/// variable's value from other variables, e.g. `fresh < freshLimit`. Empty
119+
/// for a variable that holds a value. A computed variable is read-only:
120+
/// actions that set it are ignored, and it changes whenever a variable its
121+
/// formula reads changes.
122+
final String formula;
123+
124+
/// Whether this variable's value comes from [formula].
125+
bool get isComputed => formula.trim().isNotEmpty;
126+
117127
/// Creates a new [VariableData].
118128
VariableData({
119129
required this.id,
120130
required this.name,
121131
Object? value = '',
122132
this.type = VariableType.text,
123133
Map<String, dynamic>? extra,
134+
this.formula = '',
124135
}) : value = sanitizeValueForVariableType(value, type) ?? '',
125136
extra = extra ?? {};
126137

@@ -133,6 +144,7 @@ class VariableData
133144
bool? isUsed,
134145
Set<String>? nodes,
135146
Map<String, dynamic>? extra,
147+
String? formula,
136148
}) {
137149
final String? sanitizedValue = value == null
138150
? null
@@ -143,11 +155,12 @@ class VariableData
143155
value: sanitizedValue ?? this.value,
144156
type: type ?? this.type,
145157
extra: extra ?? this.extra,
158+
formula: formula ?? this.formula,
146159
);
147160
}
148161

149162
@override
150-
List<Object?> get props => [id, name, value, type];
163+
List<Object?> get props => [id, name, value, type, formula];
151164

152165
@override
153166
int compareTo(VariableData other) => name.compareTo(other.name);
@@ -168,6 +181,7 @@ class VariableData
168181
name: name,
169182
value: value,
170183
type: type,
184+
formula: formula,
171185
);
172186

173187
/// Returns the value converted to the appropriate type according to [type].
@@ -243,6 +257,7 @@ class CanvasVariableData extends VariableData {
243257
super.type,
244258
super.value,
245259
super.extra,
260+
super.formula,
246261
});
247262

248263
@override
@@ -255,6 +270,7 @@ class CanvasVariableData extends VariableData {
255270
String? canvasId,
256271
Set<String>? nodes,
257272
Map<String, dynamic>? extra,
273+
String? formula,
258274
}) {
259275
final String? sanitizedValue = value == null
260276
? null
@@ -266,6 +282,7 @@ class CanvasVariableData extends VariableData {
266282
id: id ?? this.id,
267283
canvasId: canvasId ?? this.canvasId,
268284
extra: extra ?? this.extra,
285+
formula: formula ?? this.formula,
269286
);
270287
}
271288

@@ -301,6 +318,7 @@ class RuntimeVariableData extends VariableData {
301318
super.value = '',
302319
super.type = VariableType.text,
303320
super.extra,
321+
super.formula,
304322
}) : super(id: id ?? generateId());
305323

306324
@override
@@ -312,6 +330,7 @@ class RuntimeVariableData extends VariableData {
312330
bool? isUsed,
313331
Set<String>? nodes,
314332
Map<String, dynamic>? extra,
333+
String? formula,
315334
}) {
316335
final String? sanitizedValue = value == null
317336
? null
@@ -322,6 +341,7 @@ class RuntimeVariableData extends VariableData {
322341
value: sanitizedValue ?? this.value,
323342
type: type ?? this.type,
324343
extra: extra ?? this.extra,
344+
formula: formula ?? this.formula,
325345
);
326346
}
327347
}

‎lib/src/api/models/variables_model.g.dart‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:codelessly_api/codelessly_api.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
test('a computed variable round-trips its formula', () {
6+
final variable = VariableData(
7+
id: 'over',
8+
name: 'over',
9+
type: VariableType.boolean,
10+
formula: 'level > limit',
11+
);
12+
final back = VariableData.fromJson(variable.toJson());
13+
expect(back, variable);
14+
expect(back.isComputed, isTrue);
15+
expect(variable.withCanvas('c').formula, 'level > limit');
16+
expect(variable.copyWith(name: 'x').formula, 'level > limit');
17+
});
18+
19+
test('a plain variable stores no formula', () {
20+
final json = VariableData(id: 'a', name: 'a', value: '1').toJson();
21+
expect(json.containsKey('formula'), isFalse);
22+
expect(VariableData.fromJson(json).isComputed, isFalse);
23+
});
24+
}

0 commit comments

Comments
 (0)