-
Notifications
You must be signed in to change notification settings - Fork 3
Add protobuf text format support #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbaa52e
Add protobuf text format support
stefanvanburen c70d3c3
Rework text format API per maintainer feedback, fix brace formatting
stefanvanburen 88f0845
Realign migration guide table in README
stefanvanburen 61d601b
Address round of PR review feedback on text format support
stefanvanburen ec3fdde
cleanups
anuraaga 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # `protobuf.txtpb` | ||
|
|
||
| ::: protobuf.txtpb | ||
| options: | ||
| show_root_heading: false |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # Copyright (c) 2025-2026 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. | ||
| """Protobuf text format (`.txtpb`) serialization. | ||
|
|
||
| The [text format](https://protobuf.dev/reference/protobuf/textformat-spec/) | ||
| is a plain-text syntax used for debugging, tests, and config files. | ||
|
|
||
| Examples: | ||
| ```python | ||
| from protobuf.txtpb import message_from_text, message_to_text | ||
|
|
||
| text = message_to_text(user) | ||
| user = message_from_text(User, text) | ||
| ``` | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, TypeVar | ||
|
|
||
| from ._from_text import merge_from_text | ||
| from ._to_text import ToTextOptions, to_text as _to_text_impl | ||
|
|
||
| if TYPE_CHECKING: | ||
| from protobuf._message import Message | ||
| from protobuf._registry import Registry | ||
|
|
||
| T = TypeVar("T", bound="Message") | ||
|
|
||
| __all__ = ["merge_from_text", "message_from_text", "message_to_text"] | ||
|
|
||
|
|
||
| def message_to_text( | ||
| message: Message, | ||
| /, | ||
| *, | ||
| registry: Registry | None = None, | ||
| print_unknown_fields: bool = False, | ||
| ) -> str: | ||
| """Serialize a message to the protobuf text format. | ||
|
|
||
| Unlike standard serialization, unset required fields will not raise an error. | ||
|
|
||
| Args: | ||
| message: The message to serialize. | ||
| registry: A registry for resolving google.protobuf.Any messages | ||
| and extensions. Without it, an Any is written as its raw | ||
| `type_url`/`value` fields and extensions are omitted. | ||
| print_unknown_fields: If `True`, unknown fields are printed by | ||
| field number. | ||
|
|
||
| Returns: | ||
| The message in protobuf text format. | ||
| """ | ||
| return _to_text_impl( | ||
| message, | ||
| ToTextOptions(print_unknown_fields=print_unknown_fields, registry=registry), | ||
| ) | ||
|
|
||
|
|
||
| def message_from_text( | ||
| message_type: type[T], | ||
| text: str | bytes | bytearray, | ||
| *, | ||
| registry: Registry | None = None, | ||
|
Collaborator
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. While it deviates from protobuf-es a bit, I feel it is worth having the |
||
| ignore_unknown_fields: bool = False, | ||
| ) -> T: | ||
| """Create a new message by parsing the protobuf text format. | ||
|
|
||
| To merge into an existing message, use [`merge_from_text`][]. | ||
|
|
||
| Args: | ||
| message_type: The type of message to create. | ||
| text: The text data to parse. | ||
| registry: Required to read `google.protobuf.Any` in its expanded | ||
| `[type.url] {...}` form, and extension fields, from text | ||
| format. | ||
| ignore_unknown_fields: If `True`, unknown fields are silently | ||
| skipped instead of raising an error. | ||
|
|
||
| Raises: | ||
| ValueError: If the text cannot be parsed into the message. This | ||
| includes encountering an unknown field, unless | ||
| ignore_unknown_fields is set. | ||
| RecursionError: If messages are nested deeper than the supported | ||
| limit. | ||
| """ | ||
| message = message_type() | ||
| merge_from_text( | ||
| message, text, registry=registry, ignore_unknown_fields=ignore_unknown_fields | ||
| ) | ||
| return message | ||
Oops, something went wrong.
Oops, something went wrong.
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.
If moving the text format docs to the last section, I think we can remove these from this one and keep them all together under
Text Format