Skip to content
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

Encoding and JSON support for single unknown enum fields #2961

Merged
merged 2 commits into from
Jun 4, 2024

Conversation

oldergod
Copy link
Member

@oldergod oldergod commented May 23, 2024

When an enum has the specific proto3 generated UNRECOGNIZED constant and that the value is set to it, the message needs not to send anything on encoding + needs some magic on JSON serialization to replace it by the actual constant tag.

Look at the first commit to see the actual change without the generated code noise.

#2725

@oldergod oldergod force-pushed the bquenaudon.2024-05-20.interoptestforunrecognized branch from 3fcb049 to af0aa6e Compare May 23, 2024 13:15
Comment on lines 76 to 91
// We peekJson in order to be able to read the value twice if need be.
val peekInput = input.peekJson()
val value = jsonAdapters[index].fromJson(peekInput)
val protoAdapter = fieldBinding.adapter
if ((value as? WireEnum)?.value == -1 &&
protoAdapter.syntax == Syntax.PROTO_3 &&
protoAdapter is EnumAdapter<*>
) {
// The tag for this enum's constant is unknown to our runtime. It has been assigned to
// `UNRECOGNIZED(-1)` and we put its actual value in the unknown fields.
builder.addUnknownField(fieldBinding.tag, FieldEncoding.VARINT, input.nextInt())
} else {
// We need to skip it since we passed our peeked Json to the json adapter.
input.skipValue()
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have currently no idea how to do that on the Gson side

@oldergod oldergod force-pushed the bquenaudon.2024-05-20.interoptestforunrecognized branch from bc8766a to 66853e3 Compare May 23, 2024 19:54
Base automatically changed from bquenaudon.2024-05-16.unrecognized_encoding to master May 24, 2024 10:19

val value = jsonAdapters[index].fromJson(input)
// We peekJson in order to be able to read the value twice if need be.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// We peekJson in order to be able to read the value twice if need be.
// We peekJson in order to be able to read the value twice if need to access for building unknown values.

@oldergod oldergod force-pushed the bquenaudon.2024-05-20.interoptestforunrecognized branch from 66853e3 to 438d05c Compare June 4, 2024 17:48
@oldergod oldergod merged commit fa0ce13 into master Jun 4, 2024
11 checks passed
@oldergod oldergod deleted the bquenaudon.2024-05-20.interoptestforunrecognized branch June 4, 2024 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants