Skip to content

Commit 3c71751

Browse files
committed
Keep formula values of computed variables
A variable whose value is a ${= ...} formula is a computed variable the runtime evaluates; sanitizing it by type wiped it for lists, maps and numbers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011fXBKiHRftTsgsVPHEZAWz
1 parent 399d667 commit 3c71751

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,12 @@ class VariableData
195195
String? sanitizeValueForVariableType(Object? value, VariableType type) {
196196
if (value == null) return null;
197197
// if (value.isEmpty) return '';
198-
// Do not sanitize if the value contains a variable path.
199-
if (value.toString().contains(variablePathPattern)) return value.toString();
198+
// Do not sanitize if the value contains a variable path, or is a formula:
199+
// a computed variable stores `${= ...}` and the runtime evaluates it.
200+
if (value.toString().contains(variablePathPattern) ||
201+
value.toString().trimLeft().startsWith(r'${=')) {
202+
return value.toString();
203+
}
200204
switch (type) {
201205
case VariableType.text:
202206
case VariableType.image:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:codelessly_api/codelessly_api.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
test('a formula value is kept as is for every variable type', () {
6+
const String formula = r"${= filter(units, u => u.status == 'Available')}";
7+
for (final VariableType type in VariableType.values) {
8+
expect(
9+
VariableData(id: 'v', name: 'v', value: formula, type: type).value,
10+
formula,
11+
reason: '$type',
12+
);
13+
}
14+
expect(
15+
VariableData(
16+
id: 'v',
17+
name: 'v',
18+
value: '[]',
19+
type: VariableType.list,
20+
).copyWith(value: r'${= range(3)}').value,
21+
r'${= range(3)}',
22+
);
23+
});
24+
25+
test('other values are still sanitized by type', () {
26+
expect(
27+
VariableData(
28+
id: 'v',
29+
name: 'v',
30+
value: 'abc',
31+
type: VariableType.integer,
32+
).value,
33+
isNot('abc'),
34+
);
35+
});
36+
}

0 commit comments

Comments
 (0)