Skip to content

Commit

Permalink
support string concatenation with +
Browse files Browse the repository at this point in the history
  • Loading branch information
atn832 committed Jan 10, 2023
1 parent 7e28fbc commit 5b618cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ From <https://github.com/google/cel-spec/blob/master/doc/langdef.md#functions>.
| _+_ | (int, int) -> int | arithmetic ||
| | (uint, uint) -> uint | arithmetic ||
| | (double, double) -> double | arithmetic | |
| | (string, string) -> string | String concatenation. | |
| | (string, string) -> string | String concatenation. | |
| | (bytes, bytes) -> bytes | bytes concatenation | |
| | (list(A), list(A)) -> list(A) | List concatenation. | |
| | (google.protobuf.Timestamp, google.protobuf.Duration) -> google.protobuf.Timestamp | arithmetic ||
Expand Down
8 changes: 7 additions & 1 deletion lib/src/common/types/string.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:cel/src/common/types/bool.dart';
import 'package:cel/src/common/types/traits/matcher.dart';
import 'package:cel/src/common/types/traits/math.dart';
import 'package:cel/src/common/types/traits/traits.dart';

import '../overloads/overloads.dart';
Expand All @@ -16,7 +17,7 @@ final stringType = Type_('string', {
Traits.SizerType
});

class StringValue extends Value implements Receiver, Matcher {
class StringValue extends Value implements Receiver, Matcher, Adder {
StringValue(this.value);

@override
Expand All @@ -34,6 +35,11 @@ class StringValue extends Value implements Receiver, Matcher {
BooleanValue match(Value pattern) {
return BooleanValue(RegExp(pattern.value).hasMatch(value));
}

@override
add(Value other) {
return StringValue(value + other.value);
}
}

// class String_ extends String implements Receiver {
Expand Down
6 changes: 6 additions & 0 deletions test/cel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ void main() {
final p = environment.makeProgram(ast);
expect(p.evaluate({}), 2.6);
});
test('string', () {
final environment = Environment.standard();
final ast = environment.compile('"ab" + "cd"');
final p = environment.makeProgram(ast);
expect(p.evaluate({}), 'abcd');
});
});
test('-', () {
final environment = Environment.standard();
Expand Down

0 comments on commit 5b618cd

Please sign in to comment.