-
Notifications
You must be signed in to change notification settings - Fork 16
Introduce MessageReflector to enable alternative protobuf runtimes #202
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
andrewparmet
wants to merge
8
commits into
bufbuild:main
Choose a base branch
from
andrewparmet:enable-lazy-protokt-evaluation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8f04da
re-apply changes
andrewparmet ef1a296
Merge branch 'main' into enable-lazy-protokt-evaluation
andrewparmet 3860282
update from rebased branch
andrewparmet 558f3a9
Merge branch 'main' into enable-lazy-protokt-evaluation
andrewparmet d980745
use longs for enums
andrewparmet 22315c9
Merge branch 'main' into enable-lazy-protokt-evaluation
andrewparmet 2e2fb76
remove unused import
andrewparmet da301b2
Merge branch 'main' into enable-lazy-protokt-evaluation
andrewparmet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/build/buf/protovalidate/MessageReflector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2023-2025 Buf Technologies, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package build.buf.protovalidate; | ||
|
|
||
| import com.google.protobuf.Descriptors.FieldDescriptor; | ||
|
|
||
| /** | ||
| * {@link MessageReflector} is a wrapper around a protobuf message that provides reflective access | ||
| * to the underlying message. | ||
| * | ||
| * <p>{@link MessageReflector} is a runtime-independent interface. Any protobuf runtime that | ||
| * implements this interface can wrap its messages and, along with their {@link | ||
| * com.google.protobuf.Descriptors.Descriptor}s, protovalidate-java will be able to validate them. | ||
| */ | ||
| public interface MessageReflector { | ||
| /** | ||
| * Whether the wrapped message has the field described by the provided field descriptor. | ||
| * | ||
| * @param field The field descriptor to check for. | ||
| * @return Whether the field is present. | ||
| */ | ||
| boolean hasField(FieldDescriptor field); | ||
|
|
||
| /** | ||
| * Get the value described by the provided field descriptor. | ||
| * | ||
| * @param field The field descriptor for which to retrieve a value. | ||
| * @return The value corresponding to the field descriptor. | ||
| */ | ||
| Value getField(FieldDescriptor field); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/build/buf/protovalidate/ProtobufMessageReflector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright 2023-2025 Buf Technologies, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package build.buf.protovalidate; | ||
|
|
||
| import com.google.protobuf.Descriptors.FieldDescriptor; | ||
| import com.google.protobuf.Message; | ||
|
|
||
| class ProtobufMessageReflector implements MessageReflector { | ||
| private final Message message; | ||
|
|
||
| ProtobufMessageReflector(Message message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public Message getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasField(FieldDescriptor field) { | ||
| return message.hasField(field); | ||
| } | ||
|
|
||
| @Override | ||
| public Value getField(FieldDescriptor field) { | ||
| return new ObjectValue(field, message.getField(field)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The CEL library needs to support the protobuf runtime, otherwise this will never be recognized as the protobuf message.
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.
I'm not sure I understand what you mean here - when the protovalidate runtime (this project) requests the value of this MessageValue object using this method, it's intending to pass the value into the CEL runtime.
Looking at the usage today in CelPrograms:
where the return value of
value(Object.class)is justthis.value, which is a protobufMessage.After this PR, this becomes:
which is a more specific request than "cast this to an Object" - it's "please give me this value as it can be interpreted by CEL." The answer happens to be the same, as the new implementation returns
value.getMessage(), which is the same protobufMessagevalue as before.In the specific case of protokt, I have not implemented custom support for CEL to read protokt's
protokt.v1.Messageformat, so when this is requested from a protokt message we'll wrap the message in a DynamicMessage that CEL can read. It's an area we can optimize further later by providing reflection support for CEL.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.
This is what I was referring to, unless the CEL library itself supports alternate runtimes, I am not sure how we can fully support this.
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.
Right now, when asked to evaluate a Message for CEL, the protokt runtime converts it to a protobuf-java DynamicMessage. This is the same thing it does today for the root message object to enable protovalidate today, but is suboptimal since we can access basic fields (i.e., those used in validations evaluated from this library instead of within CEL) without converting the entire message to a DynamicMessage.
The plan was: If we benchmark and find that full message conversion is common and a bottleneck, we'll build native CEL support for protokt's Message interface as an extension in CEL.