Skip to content

Commit

Permalink
Breaking change: Lock down visibility for FileDescriptor.getSyntax().
Browse files Browse the repository at this point in the history
Syntax will become meaningless once we migrate to editions.  In the meantime, we've implemented APIs to expose the differences between proto2 and proto3 in terms of the features we plan to release in edition 2023.

PiperOrigin-RevId: 520433607
  • Loading branch information
zhangskz authored and Copybara-Service committed Mar 29, 2023
1 parent 5dc171f commit 8c8b2be
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions java/core/src/main/java/com/google/protobuf/Descriptors.java
Expand Up @@ -162,7 +162,7 @@ public List<FileDescriptor> getPublicDependencies() {
}

/** The syntax of the .proto file. */
public enum Syntax {
enum Syntax {
UNKNOWN("unknown"),
PROTO2("proto2"),
PROTO3("proto3");
Expand All @@ -175,7 +175,7 @@ public enum Syntax {
}

/** Get the syntax of the .proto file. */
public Syntax getSyntax() {
Syntax getSyntax() {
if (Syntax.PROTO3.name.equals(proto.getSyntax())) {
return Syntax.PROTO3;
}
Expand Down
Expand Up @@ -35,7 +35,7 @@
import com.google.protobuf.DescriptorProtos.EnumDescriptorProto;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.LegacyDescriptorsUtil.LegacyFileDescriptor;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
Expand Down Expand Up @@ -160,7 +160,7 @@ private RubyModule buildModuleFromDescriptor(ThreadContext context) {

RubyModule enumModule = RubyModule.newModule(runtime);
boolean defaultValueRequiredButNotFound =
descriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3;
LegacyFileDescriptor.getSyntax(descriptor.getFile()) == LegacyFileDescriptor.Syntax.PROTO3;
for (EnumValueDescriptor value : descriptor.getValues()) {
String name = fixEnumConstantName(value.getName());
// Make sure it's a valid constant name before trying to create it
Expand Down
Expand Up @@ -33,7 +33,7 @@
package com.google.protobuf.jruby;

import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.LegacyDescriptorsUtil.LegacyFileDescriptor;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
Expand Down Expand Up @@ -234,7 +234,8 @@ public IRubyObject setValue(ThreadContext context, IRubyObject message, IRubyObj
protected void setDescriptor(
ThreadContext context, FieldDescriptor descriptor, RubyDescriptorPool pool) {
if (descriptor.isRequired()
&& descriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) {
&& LegacyFileDescriptor.getSyntax(descriptor.getFile())
== LegacyFileDescriptor.Syntax.PROTO3) {
throw Utils.createTypeError(
context,
descriptor.getName()
Expand Down
Expand Up @@ -33,8 +33,9 @@
package com.google.protobuf.jruby;

import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor.Syntax.*;
import com.google.protobuf.Descriptors.GenericDescriptor;
import com.google.protobuf.LegacyDescriptorsUtil.LegacyFileDescriptor;
import com.google.protobuf.LegacyDescriptorsUtil.LegacyFileDescriptor.Syntax.*;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
Expand Down Expand Up @@ -95,7 +96,7 @@ public IRubyObject getName(ThreadContext context) {
*/
@JRubyMethod(name = "syntax")
public IRubyObject getSyntax(ThreadContext context) {
switch (fileDescriptor.getSyntax()) {
switch (LegacyFileDescriptor.getSyntax(fileDescriptor)) {
case PROTO2:
return context.runtime.newSymbol("proto2");
case PROTO3:
Expand Down
5 changes: 3 additions & 2 deletions ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
Expand Up @@ -38,10 +38,10 @@
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.LegacyDescriptorsUtil.LegacyFileDescriptor;
import com.google.protobuf.Message;
import com.google.protobuf.UnknownFieldSet;
import com.google.protobuf.util.JsonFormat;
Expand Down Expand Up @@ -73,7 +73,8 @@ public RubyMessage(Ruby runtime, RubyClass klazz, Descriptor descriptor) {
this.builder = DynamicMessage.newBuilder(descriptor);
this.fields = new HashMap<FieldDescriptor, IRubyObject>();
this.oneofCases = new HashMap<OneofDescriptor, FieldDescriptor>();
this.proto3 = descriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3;
this.proto3 =
LegacyFileDescriptor.getSyntax(descriptor.getFile()) == LegacyFileDescriptor.Syntax.PROTO3;
}

/*
Expand Down

0 comments on commit 8c8b2be

Please sign in to comment.