Skip to content

Commit dbd4dce

Browse files
habermancopybara-github
authored andcommitted
Breaking Change: Removed syntax and added has_presence?/is_packed?.
Closes #15313 PiperOrigin-RevId: 596962024
1 parent e7be866 commit dbd4dce

9 files changed

Lines changed: 85 additions & 63 deletions

File tree

ruby/ext/google/protobuf_c/defs.c

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ static VALUE FileDescriptor_alloc(VALUE klass) {
489489
* call-seq:
490490
* FileDescriptor.new => file
491491
*
492-
* Returns a new file descriptor. The syntax must be set before it's passed
492+
* Returns a new file descriptor. May
493493
* to a builder.
494494
*/
495495
static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
@@ -519,28 +519,6 @@ static VALUE FileDescriptor_name(VALUE _self) {
519519
return name == NULL ? Qnil : rb_str_new2(name);
520520
}
521521

522-
/*
523-
* call-seq:
524-
* FileDescriptor.syntax => syntax
525-
*
526-
* Returns this file descriptors syntax.
527-
*
528-
* Valid syntax versions are:
529-
* :proto2 or :proto3.
530-
*/
531-
static VALUE FileDescriptor_syntax(VALUE _self) {
532-
FileDescriptor* self = ruby_to_FileDescriptor(_self);
533-
534-
switch (upb_FileDef_Syntax(self->filedef)) {
535-
case kUpb_Syntax_Proto3:
536-
return ID2SYM(rb_intern("proto3"));
537-
case kUpb_Syntax_Proto2:
538-
return ID2SYM(rb_intern("proto2"));
539-
default:
540-
return Qnil;
541-
}
542-
}
543-
544522
/*
545523
* call-seq:
546524
* FileDescriptor.options => options
@@ -564,7 +542,6 @@ static void FileDescriptor_register(VALUE module) {
564542
rb_define_alloc_func(klass, FileDescriptor_alloc);
565543
rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
566544
rb_define_method(klass, "name", FileDescriptor_name, 0);
567-
rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
568545
rb_define_method(klass, "options", FileDescriptor_options, 0);
569546
rb_gc_register_address(&cFileDescriptor);
570547
cFileDescriptor = klass;
@@ -736,6 +713,28 @@ static VALUE FieldDescriptor_default(VALUE _self) {
736713
return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
737714
}
738715

716+
/*
717+
* call-seq:
718+
* FieldDescriptor.has_presence? => bool
719+
*
720+
* Returns whether this field tracks presence.
721+
*/
722+
static VALUE FieldDescriptor_has_presence(VALUE _self) {
723+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
724+
return upb_FieldDef_HasPresence(self->fielddef) ? Qtrue : Qfalse;
725+
}
726+
727+
/*
728+
* call-seq:
729+
* FieldDescriptor.is_packed? => bool
730+
*
731+
* Returns whether this is a repeated field that uses packed encoding.
732+
*/
733+
static VALUE FieldDescriptor_is_packed(VALUE _self) {
734+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
735+
return upb_FieldDef_IsPacked(self->fielddef) ? Qtrue : Qfalse;
736+
}
737+
739738
/*
740739
* call-seq:
741740
* FieldDescriptor.json_name => json_name
@@ -943,6 +942,8 @@ static void FieldDescriptor_register(VALUE module) {
943942
rb_define_method(klass, "name", FieldDescriptor_name, 0);
944943
rb_define_method(klass, "type", FieldDescriptor__type, 0);
945944
rb_define_method(klass, "default", FieldDescriptor_default, 0);
945+
rb_define_method(klass, "has_presence?", FieldDescriptor_has_presence, 0);
946+
rb_define_method(klass, "is_packed?", FieldDescriptor_is_packed, 0);
946947
rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
947948
rb_define_method(klass, "label", FieldDescriptor_label, 0);
948949
rb_define_method(klass, "number", FieldDescriptor_number, 0);
@@ -1163,6 +1164,17 @@ static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
11631164
upb_EnumDef_File(self->enumdef));
11641165
}
11651166

1167+
/*
1168+
* call-seq:
1169+
* EnumDescriptor.is_closed? => bool
1170+
*
1171+
* Returns whether this enum is open or closed.
1172+
*/
1173+
static VALUE EnumDescriptor_is_closed(VALUE _self) {
1174+
EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
1175+
return upb_EnumDef_IsClosed(self->enumdef) ? Qtrue : Qfalse;
1176+
}
1177+
11661178
/*
11671179
* call-seq:
11681180
* EnumDescriptor.name => name
@@ -1275,6 +1287,7 @@ static void EnumDescriptor_register(VALUE module) {
12751287
rb_define_method(klass, "each", EnumDescriptor_each, 0);
12761288
rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
12771289
rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
1290+
rb_define_method(klass, "is_closed?", EnumDescriptor_is_closed, 0);
12781291
rb_define_method(klass, "options", EnumDescriptor_options, 0);
12791292
rb_include_module(klass, rb_mEnumerable);
12801293
rb_gc_register_address(&cEnumDescriptor);

ruby/lib/google/protobuf/ffi/descriptor.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class FFI
158158
attach_function :oneof_count, :upb_MessageDef_OneofCount, [Descriptor], :int
159159
attach_function :message_options, :Descriptor_serialized_options, [Descriptor, :pointer, Internal::Arena], :pointer
160160
attach_function :get_well_known_type, :upb_MessageDef_WellKnownType, [Descriptor], WellKnown
161-
attach_function :message_def_syntax, :upb_MessageDef_Syntax, [Descriptor], Syntax
162161
attach_function :find_msg_def_by_name, :upb_MessageDef_FindByNameWithSize, [Descriptor, :string, :size_t, :FieldDefPointer, :OneofDefPointer], :bool
163162
end
164163
end

ruby/lib/google/protobuf/ffi/field_descriptor.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ def has_presence?
156156
@has_presence ||= Google::Protobuf::FFI.get_has_presence(self)
157157
end
158158

159+
##
160+
# Tests if this is a repeated field that uses packed encoding.
161+
#
162+
# @return [Boolean] True iff this field is packed
163+
def is_packed?
164+
@is_packed ||= Google::Protobuf::FFI.get_is_packed(self)
165+
end
166+
159167
# @param msg [Google::Protobuf::Message]
160168
def clear(msg)
161169
if msg.class.descriptor != Google::Protobuf::FFI.get_containing_message_def(self)
@@ -304,6 +312,7 @@ class FFI
304312
attach_function :get_default, :upb_FieldDef_Default, [FieldDescriptor], MessageValue.by_value
305313
attach_function :get_subtype_as_enum, :upb_FieldDef_EnumSubDef, [FieldDescriptor], EnumDescriptor
306314
attach_function :get_has_presence, :upb_FieldDef_HasPresence, [FieldDescriptor], :bool
315+
attach_function :get_is_packed, :upb_FieldDef_IsPacked, [FieldDescriptor], :bool
307316
attach_function :is_map, :upb_FieldDef_IsMap, [FieldDescriptor], :bool
308317
attach_function :is_repeated, :upb_FieldDef_IsRepeated, [FieldDescriptor], :bool
309318
attach_function :is_sub_message, :upb_FieldDef_IsSubMessage, [FieldDescriptor], :bool

ruby/lib/google/protobuf/ffi/file_descriptor.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module Protobuf
1010
class FFI
1111
# FileDescriptor
1212
attach_function :file_def_name, :upb_FileDef_Name, [:FileDef], :string
13-
attach_function :file_def_syntax, :upb_FileDef_Syntax, [:FileDef], Syntax
1413
attach_function :file_def_pool, :upb_FileDef_Pool, [:FileDef], :DefPool
1514
attach_function :file_options, :FileDescriptor_serialized_options, [:FileDef, :pointer, Internal::Arena], :pointer
1615
end
@@ -31,17 +30,6 @@ def inspect
3130
"#{self.class.name}: #{name}"
3231
end
3332

34-
def syntax
35-
case Google::Protobuf::FFI.file_def_syntax(@file_def)
36-
when :Proto3
37-
:proto3
38-
when :Proto2
39-
:proto2
40-
else
41-
nil
42-
end
43-
end
44-
4533
def name
4634
Google::Protobuf::FFI.file_def_name(@file_def)
4735
end

ruby/src/main/java/com/google/protobuf/jruby/RubyFieldDescriptor.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ public IRubyObject getLabel(ThreadContext context) {
9292
return label;
9393
}
9494

95+
/*
96+
* call-seq:
97+
* FieldDescriptor.has_presence? => bool
98+
*
99+
* Returns whether this field tracks presence.
100+
*/
101+
@JRubyMethod(name = "has_presence?")
102+
public IRubyObject hasPresence(ThreadContext context) {
103+
return this.descriptor.hasPresence() ? context.runtime.getTrue() : context.runtime.getFalse();
104+
}
105+
106+
/*
107+
* call-seq:
108+
* FieldDescriptor.is_packed? => bool
109+
*
110+
* Returns whether this is a repeated field that uses packed encoding.
111+
*/
112+
@JRubyMethod(name = "is_packed?")
113+
public IRubyObject isPacked(ThreadContext context) {
114+
return this.descriptor.isPacked() ? context.runtime.getTrue() : context.runtime.getFalse();
115+
}
116+
95117
/*
96118
* call-seq:
97119
* FieldDescriptor.name => name

ruby/src/main/java/com/google/protobuf/jruby/RubyFileDescriptor.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,6 @@ public IRubyObject getName(ThreadContext context) {
8686
return name == null ? context.nil : context.runtime.newString(name);
8787
}
8888

89-
/*
90-
* call-seq:
91-
* FileDescriptor.syntax => syntax
92-
*
93-
* Returns this file descriptors syntax.
94-
*
95-
* Valid syntax versions are:
96-
* :proto2 or :proto3.
97-
*/
98-
@JRubyMethod(name = "syntax")
99-
public IRubyObject getSyntax(ThreadContext context) {
100-
switch (LegacyFileDescriptor.getSyntax(fileDescriptor)) {
101-
case PROTO2:
102-
return context.runtime.newSymbol("proto2");
103-
case PROTO3:
104-
return context.runtime.newSymbol("proto3");
105-
default:
106-
return context.nil;
107-
}
108-
}
109-
11089
@JRubyMethod
11190
public IRubyObject options(ThreadContext context) {
11291
RubyDescriptorPool pool = (RubyDescriptorPool) RubyDescriptorPool.generatedPool(null, null);

ruby/tests/basic.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,10 @@ def test_file_descriptor
553553
file_descriptor = TestMessage.descriptor.file_descriptor
554554
refute_nil file_descriptor
555555
assert_equal "basic_test.proto", file_descriptor.name
556-
assert_equal :proto3, file_descriptor.syntax
557556

558557
file_descriptor = TestEnum.descriptor.file_descriptor
559558
refute_nil file_descriptor
560559
assert_equal "basic_test.proto", file_descriptor.name
561-
assert_equal :proto3, file_descriptor.syntax
562560
end
563561

564562
def test_map_freeze
@@ -639,6 +637,17 @@ def test_map_fields_respond_to? # regression test for issue 9202
639637
end
640638
end
641639

640+
def test_has_presence
641+
assert_true TestMessage.descriptor.lookup("optional_int32").has_presence?
642+
assert_false TestMessage.descriptor.lookup("repeated_int32").has_presence?
643+
assert_false TestSingularFields.descriptor.lookup("singular_int32").has_presence?
644+
end
645+
646+
def test_is_packed
647+
assert_false TestMessage.descriptor.lookup("optional_int32").is_packed?
648+
assert_true TestMessage.descriptor.lookup("repeated_int32").is_packed?
649+
end
650+
642651
def test_file_descriptor_options
643652
file_descriptor = TestMessage.descriptor.file_descriptor
644653

ruby/tests/basic_proto2.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,10 @@ def test_file_descriptor
231231
file_descriptor = TestMessage.descriptor.file_descriptor
232232
refute_nil file_descriptor
233233
assert_equal "basic_test_proto2.proto", file_descriptor.name
234-
assert_equal :proto2, file_descriptor.syntax
235234

236235
file_descriptor = TestEnum.descriptor.file_descriptor
237236
refute_nil file_descriptor
238237
assert_equal "basic_test_proto2.proto", file_descriptor.name
239-
assert_equal :proto2, file_descriptor.syntax
240238
end
241239

242240
def test_oneof_fields_respond_to? # regression test for issue 9202
@@ -254,6 +252,11 @@ def test_oneof_fields_respond_to? # regression test for issue 9202
254252
refute msg.has_d?
255253
end
256254

255+
def test_is_packed
256+
assert_false TestMessage.descriptor.lookup("optional_int32").is_packed?
257+
assert_false TestMessage.descriptor.lookup("repeated_int32").is_packed?
258+
end
259+
257260
def test_extension
258261
message = TestExtensions.new
259262
extension = Google::Protobuf::DescriptorPool.generated_pool.lookup 'basic_test_proto2.optional_int32_extension'

upb/reflection/field_def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ uint32_t upb_FieldDef_Index(const upb_FieldDef* f);
4747
bool upb_FieldDef_IsExtension(const upb_FieldDef* f);
4848
UPB_API bool upb_FieldDef_IsMap(const upb_FieldDef* f);
4949
bool upb_FieldDef_IsOptional(const upb_FieldDef* f);
50-
bool upb_FieldDef_IsPacked(const upb_FieldDef* f);
50+
UPB_API bool upb_FieldDef_IsPacked(const upb_FieldDef* f);
5151
bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f);
5252
UPB_API bool upb_FieldDef_IsRepeated(const upb_FieldDef* f);
5353
bool upb_FieldDef_IsRequired(const upb_FieldDef* f);

0 commit comments

Comments
 (0)