Skip to content

[Feature Request] Bitwise operation support in predefined CEL #362

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

Open
KeXiangWang opened this issue May 7, 2025 · 6 comments
Open

[Feature Request] Bitwise operation support in predefined CEL #362

KeXiangWang opened this issue May 7, 2025 · 6 comments
Labels
Feature New feature or request

Comments

@KeXiangWang
Copy link

Feature description:
Support bitwise operations in predefined CEL rules.

For example,

extend buf.validate.Int32Rules {
  optional bool power_of_two = 100000 [(buf.validate.predefined).cel = {
    id: "int32.power_of_two"
    message: "value must be a power of two"
    expression: "this >= 0 && (this & (this - 1)) == 0"
  }];
}

although the above rules can be successfully complied by protoc, when execute, I got this error:

compilation error: failed to compile standard rule "xxxxx.power_of_two": compilation error: failed to compile expression int32.power_of_two: ERROR: <input>:1:20: Syntax error: token recognition error at: '& '
 | this >= 0 && (this & (this - 1)) == 0

I think CEL itself support bitwise operations? I'm not sure whether protovalidate disable it or there's some reason it doesn't work.

My toolchain version:
protoc: libprotoc 3.21.9
validate.proto: https://github.com/bufbuild/protovalidate/blob/51ee9b3a85ed11c19a2b8023aa2520cca82f431e/proto/protovalidate/buf/validate/validate.proto
protovalidate-go: buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.6-20250425153114-8976f5be98c1.1

Problem it solves or use case:
When predefining some more complicated rules.

Proposed implementation or solution:
Sorry, I'm still not familiar with the implementation yet.

Contribution:
Will to contribute, if I can get necessary guidance.

Examples or references:
None

Additional context:
None

@KeXiangWang KeXiangWang added the Feature New feature or request label May 7, 2025
@KeXiangWang KeXiangWang changed the title [Feature Request] Bitwise operation support in CEL [Feature Request] Bitwise operation support in predefined CEL May 7, 2025
@KeXiangWang
Copy link
Author

Oh, sorry. I just checked the cel-spec repo. Looks like bitwise operations have not been implement on their side. google/cel-spec#286. When they support them, protovalidate only need to followup the updates.

@KeXiangWang
Copy link
Author

KeXiangWang commented May 7, 2025

However, the cel-go has already implemented the bitwise operations. https://github.com/google/cel-go/blob/f6d3c92171c2c8732a8d0a4b24d6729df4261520/ext/math.go#L95 Is it possible for protovalidate-go support bitwise operations as well?

@smaye81
Copy link
Member

smaye81 commented May 8, 2025

Hi, @KeXiangWang. We're trying to both follow the spec and keep our implementations consistent, so we'd rather not deviate quite yet if we don't have to. That being said, this is a fair request for when it gets added to the spec. Would you mind creating an issue for adding this on the protovalidate-go repo?

@KeXiangWang
Copy link
Author

We're trying to both follow the spec and keep our implementations consistent, so we'd rather not deviate quite yet if we don't have to.

Understood. Thanks!
I took a look yesterday of your implementation for these kind of extension, which seems not too non-trivial. If I can help implement the ext/math for protovalidate-go and submit a PR to the repo, would you consider merging it into main branch?

@KeXiangWang
Copy link
Author

KeXiangWang commented May 8, 2025

I've copied most of the information of this issue to bufbuild/protovalidate-go#246. Feel free to close this issue if you think it's inappropriate to be here.

@smaye81
Copy link
Member

smaye81 commented May 9, 2025

If I can help implement the ext/math for protovalidate-go and submit a PR to the repo, would you consider merging it into main branch?

Sure. Appreciate the interest. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants