Skip to content

Commit

Permalink
Add support for EDITIONS in Java and Java Lite syntax enum / bits.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 555522301
  • Loading branch information
zhangskz authored and copybara-github committed Aug 10, 2023
1 parent 09647a1 commit 924a152
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ private static ProtoSyntax convertSyntax(FileDescriptor.Syntax syntax) {
return ProtoSyntax.PROTO2;
case PROTO3:
return ProtoSyntax.PROTO3;
case EDITIONS:
return ProtoSyntax.EDITIONS;
default:
throw new IllegalArgumentException("Unsupported syntax: " + syntax);
}
Expand Down
14 changes: 13 additions & 1 deletion java/core/src/main/java/com/google/protobuf/Descriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public List<FileDescriptor> getPublicDependencies() {
enum Syntax {
UNKNOWN("unknown"),
PROTO2("proto2"),
PROTO3("proto3");
PROTO3("proto3"),
EDITIONS("editions");

Syntax(String name) {
this.name = name;
Expand All @@ -182,16 +183,27 @@ enum Syntax {
Syntax getSyntax() {
if (Syntax.PROTO3.name.equals(proto.getSyntax())) {
return Syntax.PROTO3;
} else if (Syntax.EDITIONS.name.equals(proto.getSyntax())) {
return Syntax.EDITIONS;
}
return Syntax.PROTO2;
}

/** Get the edition of the .proto file. */
public String getEdition() {
return proto.getEdition();
}

public void copyHeadingTo(FileDescriptorProto.Builder protoBuilder) {
protoBuilder.setName(getName()).setSyntax(getSyntax().name);
if (!getPackage().isEmpty()) {
protoBuilder.setPackage(getPackage());
}

if (getSyntax().equals(Syntax.EDITIONS)) {
protoBuilder.setEdition(getEdition());
}

if (!getOptions().equals(FileOptions.getDefaultInstance())) {
protoBuilder.setOptions(getOptions());
}
Expand Down
3 changes: 2 additions & 1 deletion java/core/src/main/java/com/google/protobuf/ProtoSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
@ExperimentalApi
public enum ProtoSyntax {
PROTO2,
PROTO3;
PROTO3,
EDITIONS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@CheckReturnValue
final class RawMessageInfo implements MessageInfo {
private static final int IS_PROTO2_BIT = 0x1;
private static final int IS_EDITION_BIT = 0x4;

private final MessageLite defaultInstance;

Expand All @@ -61,7 +62,8 @@ final class RawMessageInfo implements MessageInfo {
* <p>The integer sequence encoded in the String object has the following layout:
*
* <ul>
* <li>[0]: flags, flags & 0x1 = is proto2?, flags & 0x2 = is message?
* <li>[0]: flags, flags & 0x1 = is proto2?, flags & 0x2 = is message?, flags & 0x4 = is
* edition?
* <li>[1]: field count, if 0, this is the end of the integer sequence and the corresponding
* Object[] array should be null.
* <li>[2]: oneof count
Expand Down Expand Up @@ -215,6 +217,8 @@ public MessageLite getDefaultInstance() {
public ProtoSyntax getSyntax() {
if ((flags & IS_PROTO2_BIT) != 0) {
return ProtoSyntax.PROTO2;
} else if ((flags & IS_EDITION_BIT) == 0x4) {
return ProtoSyntax.EDITIONS;
} else {
return ProtoSyntax.PROTO3;
}
Expand Down
5 changes: 5 additions & 0 deletions src/google/protobuf/compiler/java/message_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo(
if (descriptor_->options().message_set_wire_format()) {
flags |= 0x2;
}
if (FileDescriptorLegacy(descriptor_->file()).syntax() ==
FileDescriptorLegacy::Syntax::SYNTAX_EDITIONS) {
flags |= 0x4;
}

WriteIntToUtf16CharSequence(flags, &chars);
WriteIntToUtf16CharSequence(descriptor_->field_count(), &chars);

Expand Down

0 comments on commit 924a152

Please sign in to comment.