Repository navigation
fix: address review feedback on validation checks and truthiness #1047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -115,9 +115,6 @@ class DataContext implements cf.ExecutionContext { | |||||||||
| if (value.containsKey('call')) { | ||||||||||
| return _evaluateFunctionCall(value as JsonMap); | ||||||||||
| } | ||||||||||
| if (value.containsKey('functionCall') && value['functionCall'] is Map) { | ||||||||||
| return _evaluateStream(value['functionCall']); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| if (value is List) { | ||||||||||
| if (value.isEmpty) return Stream.value(<Object?>[]); | ||||||||||
|
|
@@ -152,7 +149,52 @@ class DataContext implements cf.ExecutionContext { | |||||||||
| for (final Object? key in argsJson.keys) { | ||||||||||
| final argName = key.toString(); | ||||||||||
| final Object? val = argsJson[key]; | ||||||||||
| args[argName] = _evaluateStream(val); | ||||||||||
| if ((name == 'and' || name == 'or') && | ||||||||||
| argName == 'values' && | ||||||||||
| val is List) { | ||||||||||
| final List<Stream<Object?>> itemStreams = val.map((item) { | ||||||||||
| if (item is Map) { | ||||||||||
| if (item.containsKey('functionCall')) { | ||||||||||
| final Object? fc = item['functionCall']; | ||||||||||
| if (fc is Map) { | ||||||||||
| return _evaluateStream(fc); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a nested item inside
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in dcb1029. Added |
||||||||||
| } else { | ||||||||||
| genUiLogger.warning('Invalid condition expression: $item'); | ||||||||||
| return Stream<Object?>.value(false); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| if (!_isValidConditionShape(item)) { | ||||||||||
| genUiLogger.warning('Invalid condition expression: $item'); | ||||||||||
| return Stream<Object?>.value(false); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| return _evaluateStream(item); | ||||||||||
| }).toList(); | ||||||||||
| args[argName] = itemStreams.isEmpty | ||||||||||
| ? Stream.value(<Object?>[]) | ||||||||||
| : itemStreams.combineLatestAll(); | ||||||||||
| } else if (name == 'not' && argName == 'value') { | ||||||||||
| if (val is Map) { | ||||||||||
| if (val.containsKey('functionCall')) { | ||||||||||
| final Object? fc = val['functionCall']; | ||||||||||
| if (fc is Map) { | ||||||||||
| args[argName] = _evaluateStream(fc); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to the
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in dcb1029. Added |
||||||||||
| } else { | ||||||||||
| genUiLogger.warning('Invalid condition expression: $val'); | ||||||||||
| args[argName] = Stream<Object?>.value(true); | ||||||||||
| } | ||||||||||
| } else if (!_isValidConditionShape(val)) { | ||||||||||
| genUiLogger.warning('Invalid condition expression: $val'); | ||||||||||
| args[argName] = Stream<Object?>.value(true); | ||||||||||
| } else { | ||||||||||
| args[argName] = _evaluateStream(val); | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| args[argName] = _evaluateStream(val); | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| args[argName] = _evaluateStream(val); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -180,33 +222,43 @@ class DataContext implements cf.ExecutionContext { | |||||||||
| if (condition == null) return Stream.value(false); | ||||||||||
| if (condition is bool) return Stream.value(condition); | ||||||||||
|
|
||||||||||
| if (condition is Map && | ||||||||||
| !condition.containsKey('path') && | ||||||||||
| !condition.containsKey('call') && | ||||||||||
| !condition.containsKey('functionCall')) { | ||||||||||
| genUiLogger.warning('Invalid condition expression: $condition'); | ||||||||||
| return Stream.value(false); | ||||||||||
| if (condition is Map) { | ||||||||||
| if (condition.containsKey('functionCall')) { | ||||||||||
| final Object? fc = condition['functionCall']; | ||||||||||
| if (fc is Map) { | ||||||||||
| return evaluateConditionStream(fc); | ||||||||||
| } else { | ||||||||||
| genUiLogger.warning('Invalid condition expression: $condition'); | ||||||||||
| return Stream.value(false); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| if (!_isValidConditionShape(condition)) { | ||||||||||
| genUiLogger.warning('Invalid condition expression: $condition'); | ||||||||||
| return Stream.value(false); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| final Stream<Object?> resultStream = _evaluateStream(condition); | ||||||||||
| return resultStream.map(isTruthy); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| bool _isValidConditionShape(Map<Object?, Object?> map) { | ||||||||||
| return map.containsKey('path') || | ||||||||||
| map.containsKey('call') || | ||||||||||
| (map.containsKey('functionCall') && map['functionCall'] is Map); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Helper to determine truthiness according to A2UI expression semantics. | ||||||||||
| @internal | ||||||||||
| bool isTruthy(Object? value) { | ||||||||||
| if (value is bool) return value; | ||||||||||
| if (value == null) return false; | ||||||||||
| if (value is bool) return value; | ||||||||||
| if (value is String) return value.isNotEmpty; | ||||||||||
| if (value is num) return value != 0 && !value.isNaN; | ||||||||||
| if (value is List) return value.isNotEmpty; | ||||||||||
| if (value is Map) { | ||||||||||
| if (value.containsKey('valid') && value['valid'] is bool) { | ||||||||||
| return value['valid'] as bool; | ||||||||||
| } | ||||||||||
| return value.isNotEmpty; | ||||||||||
| } | ||||||||||
| return false; | ||||||||||
| if (value is Map) return value.isNotEmpty; | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Resolves a context map definition against a [DataContext]. | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
errorMessageis a nullableString?andisErroris a local boolean variable, Dart's type promotion does not automatically promoteerrorMessageto a non-nullableStringinside the ternary operator. This will cause a static analysis/compilation error because theTextconstructor expects a non-nullableString. Use the null-assertion operator!to safely cast it, asisErrorguarantees it is non-null.