Skip to content

Commit d82de8f

Browse files
Deprecate Descriptor Label. As an alternative, add helper methods for checking whether a field is required or repeated.
This is for PHP and Ruby. PiperOrigin-RevId: 743308499
1 parent a9d281d commit d82de8f

File tree

9 files changed

+105
-20
lines changed

9 files changed

+105
-20
lines changed

php/ext/google/protobuf/def.c

+26
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,37 @@ PHP_METHOD(FieldDescriptor, getNumber) {
390390
/*
391391
* FieldDescriptor::getLabel()
392392
*
393+
* DEPRECATED: Use isRequired() or isRepeated() instead.
394+
*
393395
* Returns the label of this field as an integer.
394396
*/
395397
PHP_METHOD(FieldDescriptor, getLabel) {
398+
zend_error(E_USER_WARNING,
399+
"getLabel is deprecated. Use isRequired or isRepeated instead.\n");
396400
FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
397401
RETURN_LONG(upb_FieldDef_Label(intern->fielddef));
398402
}
399403

404+
/*
405+
* FieldDescriptor::isRequired()
406+
*
407+
* Returns true if this field is a required field.
408+
*/
409+
PHP_METHOD(FieldDescriptor, isRequired) {
410+
FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
411+
RETURN_BOOL(upb_FieldDef_IsRequired(intern->fielddef));
412+
}
413+
414+
/*
415+
* FieldDescriptor::isRepeated()
416+
*
417+
* Returns true if this field is a repeated field.
418+
*/
419+
PHP_METHOD(FieldDescriptor, isRepeated) {
420+
FieldDescriptor* intern = (FieldDescriptor*)Z_OBJ_P(getThis());
421+
RETURN_BOOL(upb_FieldDef_IsRepeated(intern->fielddef));
422+
}
423+
400424
/*
401425
* FieldDescriptor::getType()
402426
*
@@ -500,6 +524,8 @@ static zend_function_entry FieldDescriptor_methods[] = {
500524
PHP_ME(FieldDescriptor, getName, arginfo_void, ZEND_ACC_PUBLIC)
501525
PHP_ME(FieldDescriptor, getNumber, arginfo_void, ZEND_ACC_PUBLIC)
502526
PHP_ME(FieldDescriptor, getLabel, arginfo_void, ZEND_ACC_PUBLIC)
527+
PHP_ME(FieldDescriptor, isRequired, arginfo_void, ZEND_ACC_PUBLIC)
528+
PHP_ME(FieldDescriptor, isRepeated, arginfo_void, ZEND_ACC_PUBLIC)
503529
PHP_ME(FieldDescriptor, getType, arginfo_void, ZEND_ACC_PUBLIC)
504530
PHP_ME(FieldDescriptor, isMap, arginfo_void, ZEND_ACC_PUBLIC)
505531
PHP_ME(FieldDescriptor, getEnumType, arginfo_void, ZEND_ACC_PUBLIC)

php/src/Google/Protobuf/FieldDescriptor.php

+18
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,31 @@ public function getNumber()
4444
}
4545

4646
/**
47+
* @deprecated Use isRepeated() or isRequired() instead.
48+
*
4749
* @return int
4850
*/
4951
public function getLabel()
5052
{
5153
return $this->internal_desc->getLabel();
5254
}
5355

56+
/**
57+
* @return boolean
58+
*/
59+
public function isRequired()
60+
{
61+
return $this->internal_desc->isRequired();
62+
}
63+
64+
/**
65+
* @return boolean
66+
*/
67+
public function isRepeated()
68+
{
69+
return $this->internal_desc->isRepeated();
70+
}
71+
5472
/**
5573
* @return int
5674
*/

php/src/Google/Protobuf/Internal/FieldDescriptor.php

+5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public function getLabel()
104104
return $this->label;
105105
}
106106

107+
public function isRequired()
108+
{
109+
return $this->label === GPBLabel::REQUIRED;
110+
}
111+
107112
public function isRepeated()
108113
{
109114
return $this->label === GPBLabel::REPEATED;

php/src/Google/Protobuf/Internal/Message.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Google\Protobuf\Internal\CodedInputStream;
1717
use Google\Protobuf\Internal\CodedOutputStream;
1818
use Google\Protobuf\Internal\DescriptorPool;
19-
use Google\Protobuf\Internal\GPBLabel;
2019
use Google\Protobuf\Internal\GPBType;
2120
use Google\Protobuf\Internal\GPBWire;
2221
use Google\Protobuf\Internal\MapEntry;
@@ -105,7 +104,7 @@ private function initWithGeneratedPool()
105104
$this->$setter($map_field);
106105
break;
107106
}
108-
} else if ($field->getLabel() === GPBLabel::REPEATED) {
107+
} else if ($field->isRepeated()) {
109108
switch ($field->getType()) {
110109
case GPBType::MESSAGE:
111110
case GPBType::GROUP:
@@ -129,7 +128,7 @@ private function initWithGeneratedPool()
129128
$oneof = $this->desc->getOneofDecl()[$field->getOneofIndex()];
130129
$oneof_name = $oneof->getName();
131130
$this->$oneof_name = new OneofField($oneof);
132-
} else if ($field->getLabel() === GPBLabel::OPTIONAL &&
131+
} else if (!$field->isRequired() && !$field->isRepeated() &&
133132
PHP_INT_SIZE == 4) {
134133
switch ($field->getType()) {
135134
case GPBType::INT64:
@@ -557,7 +556,7 @@ public function clear()
557556
$this->$setter($map_field);
558557
break;
559558
}
560-
} else if ($field->getLabel() === GPBLabel::REPEATED) {
559+
} else if ($field->isRepeated()) {
561560
switch ($field->getType()) {
562561
case GPBType::MESSAGE:
563562
case GPBType::GROUP:
@@ -581,7 +580,7 @@ public function clear()
581580
$oneof = $this->desc->getOneofDecl()[$field->getOneofIndex()];
582581
$oneof_name = $oneof->getName();
583582
$this->$oneof_name = new OneofField($oneof);
584-
} else if ($field->getLabel() === GPBLabel::OPTIONAL) {
583+
} else if (!$field->isRequired() && !$field->isRepeated()) {
585584
switch ($field->getType()) {
586585
case GPBType::DOUBLE :
587586
case GPBType::FLOAT :
@@ -652,13 +651,13 @@ public function discardUnknownFields()
652651
foreach ($map as $key => $value) {
653652
$value->discardUnknownFields();
654653
}
655-
} else if ($field->getLabel() === GPBLabel::REPEATED) {
654+
} else if ($field->isRepeated()) {
656655
$getter = $field->getGetter();
657656
$arr = $this->$getter();
658657
foreach ($arr as $sub) {
659658
$sub->discardUnknownFields();
660659
}
661-
} else if ($field->getLabel() === GPBLabel::OPTIONAL) {
660+
} else if (!$field->isRequired() && !$field->isRepeated()) {
662661
$getter = $field->getGetter();
663662
$sub = $this->$getter();
664663
if (!is_null($sub)) {
@@ -706,7 +705,7 @@ public function mergeFrom($msg)
706705
}
707706
}
708707
}
709-
} else if ($field->getLabel() === GPBLabel::REPEATED) {
708+
} else if ($field->isRepeated()) {
710709
if (count($msg->$getter()) != 0) {
711710
foreach ($msg->$getter() as $tmp) {
712711
if ($field->getType() == GPBType::MESSAGE) {
@@ -719,7 +718,7 @@ public function mergeFrom($msg)
719718
}
720719
}
721720
}
722-
} else if ($field->getLabel() === GPBLabel::OPTIONAL) {
721+
} else if (!$field->isRequired() && !$field->isRepeated()) {
723722
if($msg->$getter() !== $this->defaultValue($field)) {
724723
$tmp = $msg->$getter();
725724
if ($field->getType() == GPBType::MESSAGE) {

php/tests/DescriptorsTest.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,17 @@ public function testFieldDescriptor()
142142
$fieldDesc = $fieldDescMap[1];
143143
$this->assertSame('optional_int32', $fieldDesc->getName());
144144
$this->assertSame(1, $fieldDesc->getNumber());
145-
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
145+
$this->assertFalse($fieldDesc->isRequired());
146+
$this->assertFalse($fieldDesc->isRepeated());
146147
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
147148
$this->assertFalse($fieldDesc->isMap());
148149

149150
// Optional enum field
150151
$fieldDesc = $fieldDescMap[16];
151152
$this->assertSame('optional_enum', $fieldDesc->getName());
152153
$this->assertSame(16, $fieldDesc->getNumber());
153-
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
154+
$this->assertFalse($fieldDesc->isRequired());
155+
$this->assertFalse($fieldDesc->isRepeated());
154156
$this->assertSame(self::GPBTYPE_ENUM, $fieldDesc->getType());
155157
$this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $fieldDesc->getEnumType());
156158
$this->assertFalse($fieldDesc->isMap());
@@ -159,7 +161,8 @@ public function testFieldDescriptor()
159161
$fieldDesc = $fieldDescMap[17];
160162
$this->assertSame('optional_message', $fieldDesc->getName());
161163
$this->assertSame(17, $fieldDesc->getNumber());
162-
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
164+
$this->assertFalse($fieldDesc->isRequired());
165+
$this->assertFalse($fieldDesc->isRepeated());
163166
$this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType());
164167
$this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType());
165168
$this->assertFalse($fieldDesc->isMap());
@@ -168,15 +171,15 @@ public function testFieldDescriptor()
168171
$fieldDesc = $fieldDescMap[31];
169172
$this->assertSame('repeated_int32', $fieldDesc->getName());
170173
$this->assertSame(31, $fieldDesc->getNumber());
171-
$this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel());
174+
$this->assertTrue($fieldDesc->isRepeated());
172175
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
173176
$this->assertFalse($fieldDesc->isMap());
174177

175178
// Repeated message field
176179
$fieldDesc = $fieldDescMap[47];
177180
$this->assertSame('repeated_message', $fieldDesc->getName());
178181
$this->assertSame(47, $fieldDesc->getNumber());
179-
$this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel());
182+
$this->assertTrue($fieldDesc->isRepeated());
180183
$this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType());
181184
$this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType());
182185
$this->assertFalse($fieldDesc->isMap());
@@ -187,7 +190,8 @@ public function testFieldDescriptor()
187190
$fieldDesc = $fieldDescMap[51];
188191
$this->assertSame('oneof_int32', $fieldDesc->getName());
189192
$this->assertSame(51, $fieldDesc->getNumber());
190-
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
193+
$this->assertFalse($fieldDesc->isRequired());
194+
$this->assertFalse($fieldDesc->isRepeated());
191195
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
192196
$this->assertFalse($fieldDesc->isMap());
193197
$this->assertSame($fieldDesc->getContainingOneof(), $fieldDesc->getRealContainingOneof());
@@ -200,7 +204,8 @@ public function testFieldDescriptor()
200204
$fieldDesc = $fieldDescMap[52];
201205
$this->assertSame('proto3_optional_int32', $fieldDesc->getName());
202206
$this->assertSame(52, $fieldDesc->getNumber());
203-
$this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel());
207+
$this->assertFalse($fieldDesc->isRequired());
208+
$this->assertFalse($fieldDesc->isRepeated());
204209
$this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType());
205210
$this->assertFalse($fieldDesc->isMap());
206211
$this->assertNull($fieldDesc->getRealContainingOneof());
@@ -210,7 +215,7 @@ public function testFieldDescriptor()
210215
$fieldDesc = $fieldDescMap[71];
211216
$this->assertSame('map_int32_enum', $fieldDesc->getName());
212217
$this->assertSame(71, $fieldDesc->getNumber());
213-
$this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel());
218+
$this->assertTrue($fieldDesc->isRepeated());
214219
$this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType());
215220
$this->assertTrue($fieldDesc->isMap());
216221
$mapDesc = $fieldDesc->getMessageType();

ruby/ext/google/protobuf_c/defs.c

+26
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,28 @@ static VALUE FieldDescriptor_has_presence(VALUE _self) {
798798
return upb_FieldDef_HasPresence(self->fielddef) ? Qtrue : Qfalse;
799799
}
800800

801+
/*
802+
* call-seq:
803+
* FieldDescriptor.required? => bool
804+
*
805+
* Returns whether this is a required field.
806+
*/
807+
static VALUE FieldDescriptor_is_required(VALUE _self) {
808+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
809+
return upb_FieldDef_IsRequired(self->fielddef) ? Qtrue : Qfalse;
810+
}
811+
812+
/*
813+
* call-seq:
814+
* FieldDescriptor.repeated? => bool
815+
*
816+
* Returns whether this is a repeated field.
817+
*/
818+
static VALUE FieldDescriptor_is_repeated(VALUE _self) {
819+
FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
820+
return upb_FieldDef_IsRepeated(self->fielddef) ? Qtrue : Qfalse;
821+
}
822+
801823
/*
802824
* call-seq:
803825
* FieldDescriptor.is_packed? => bool
@@ -823,6 +845,8 @@ static VALUE FieldDescriptor_json_name(VALUE _self) {
823845
}
824846

825847
/*
848+
* DEPRECATED: Use repeated? or required? instead.
849+
*
826850
* call-seq:
827851
* FieldDescriptor.label => label
828852
*
@@ -1038,6 +1062,8 @@ static void FieldDescriptor_register(VALUE module) {
10381062
rb_define_method(klass, "type", FieldDescriptor__type, 0);
10391063
rb_define_method(klass, "default", FieldDescriptor_default, 0);
10401064
rb_define_method(klass, "has_presence?", FieldDescriptor_has_presence, 0);
1065+
rb_define_method(klass, "required?", FieldDescriptor_is_required, 0);
1066+
rb_define_method(klass, "repeated?", FieldDescriptor_is_repeated, 0);
10411067
rb_define_method(klass, "is_packed?", FieldDescriptor_is_packed, 0);
10421068
rb_define_method(klass, "json_name", FieldDescriptor_json_name, 0);
10431069
rb_define_method(klass, "label", FieldDescriptor_label, 0);

ruby/ext/google/protobuf_c/message.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static VALUE Message_field_accessor(VALUE _self, const upb_FieldDef* f,
398398
upb_MessageValue msgval =
399399
upb_Message_GetFieldByDef(Message_Get(_self, NULL), f);
400400

401-
if (upb_FieldDef_Label(f) == kUpb_Label_Repeated) {
401+
if (upb_FieldDef_IsRepeated(f)) {
402402
// Map repeated fields to a new type with ints
403403
VALUE arr = rb_ary_new();
404404
size_t i, n = upb_Array_Size(msgval.array_val);
@@ -592,7 +592,7 @@ static void Message_InitFieldFromValue(upb_Message* msg, const upb_FieldDef* f,
592592
if (upb_FieldDef_IsMap(f)) {
593593
upb_Map* map = upb_Message_Mutable(msg, f, arena).map;
594594
Map_InitFromValue(map, f, val, arena);
595-
} else if (upb_FieldDef_Label(f) == kUpb_Label_Repeated) {
595+
} else if (upb_FieldDef_IsRepeated(f)) {
596596
upb_Array* arr = upb_Message_Mutable(msg, f, arena).array;
597597
RepeatedField_InitFromValue(arr, f, val, arena);
598598
} else if (upb_FieldDef_IsSubMessage(f)) {

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

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def type
6767
@type ||= Google::Protobuf::FFI.get_type(self)
6868
end
6969

70+
# DEPRECATED: Use required? or repeated? instead.
7071
def label
7172
@label ||= Google::Protobuf::FFI.get_label(self)
7273
end
@@ -197,6 +198,10 @@ def map?
197198
@map ||= Google::Protobuf::FFI.is_map self
198199
end
199200

201+
def required?
202+
@required ||= Google::Protobuf::FFI.is_required self
203+
end
204+
200205
def repeated?
201206
@repeated ||= Google::Protobuf::FFI.is_repeated self
202207
end
@@ -325,6 +330,7 @@ class FFI
325330
attach_function :get_has_presence, :upb_FieldDef_HasPresence, [FieldDescriptor], :bool
326331
attach_function :get_is_packed, :upb_FieldDef_IsPacked, [FieldDescriptor], :bool
327332
attach_function :is_map, :upb_FieldDef_IsMap, [FieldDescriptor], :bool
333+
attach_function :is_required, :upb_FieldDef_IsRequired, [FieldDescriptor], :bool
328334
attach_function :is_repeated, :upb_FieldDef_IsRepeated, [FieldDescriptor], :bool
329335
attach_function :is_sub_message, :upb_FieldDef_IsSubMessage, [FieldDescriptor], :bool
330336
attach_function :get_json_name, :upb_FieldDef_JsonName, [FieldDescriptor], :string

upb/reflection/field_def.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool upb_FieldDef_IsOptional(const upb_FieldDef* f);
5252
UPB_API bool upb_FieldDef_IsPacked(const upb_FieldDef* f);
5353
bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f);
5454
UPB_API bool upb_FieldDef_IsRepeated(const upb_FieldDef* f);
55-
bool upb_FieldDef_IsRequired(const upb_FieldDef* f);
55+
UPB_API bool upb_FieldDef_IsRequired(const upb_FieldDef* f);
5656
bool upb_FieldDef_IsString(const upb_FieldDef* f);
5757
UPB_API bool upb_FieldDef_IsSubMessage(const upb_FieldDef* f);
5858
UPB_API const char* upb_FieldDef_JsonName(const upb_FieldDef* f);

0 commit comments

Comments
 (0)