Skip to content

Commit 86ab252

Browse files
thomasvlcopybara-github
authored andcommitted
[ObjC] Deprecate GPBFieldDescriptor.isOptional.
This also deprecates the internal `GPBFieldOptional` flag, the generator will be updated to stop using it in the future. PiperOrigin-RevId: 740378571
1 parent 649bc1c commit 86ab252

6 files changed

+12
-5
lines changed

objectivec/GPBDescriptor.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ __attribute__((objc_subclassing_restricted))
173173
/** Whether this field is required. Only valid for proto2 fields. */
174174
@property(nonatomic, readonly, getter=isRequired) BOOL required;
175175
/** Whether this field is optional. */
176-
@property(nonatomic, readonly, getter=isOptional) BOOL optional;
176+
@property(nonatomic, readonly, getter=isOptional) BOOL optional DEPRECATED_MSG_ATTRIBUTE(
177+
"Check if fieldType is GPBFieldTypeSingle and that it is NOT required.");
177178
/** Type of field (single, repeated, map). */
178179
@property(nonatomic, readonly) GPBFieldType fieldType;
179180
/** Type of the key if the field is a map. The value's type is -dataType. */

objectivec/GPBDescriptor.m

+7-1
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,14 @@ + (instancetype)allocDescriptorForClass:(Class)messageClass
142142
#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
143143
// No real value in checking all the fields individually, just check the combined flags at the
144144
// end.
145+
#pragma clang diagnostic push
146+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
145147
GPBFieldFlags unknownFieldFlags =
146148
(GPBFieldFlags)(~(GPBFieldRequired | GPBFieldRepeated | GPBFieldPacked | GPBFieldOptional |
147149
GPBFieldHasDefaultValue | GPBFieldClearHasIvarOnZero |
148150
GPBFieldTextFormatNameCustom | GPBFieldMapKeyMask));
149151
NSAssert((mergedFieldFlags & unknownFieldFlags) == 0, @"Internal error: unknown field flags set");
152+
#pragma clang diagnostic pop
150153
#endif // defined(DEBUG) && DEBUG
151154

152155
BOOL wireFormat = (flags & GPBDescriptorInitializationFlag_WireFormat) != 0;
@@ -611,9 +614,12 @@ - (BOOL)isRequired {
611614
return (description_->flags & GPBFieldRequired) != 0;
612615
}
613616

617+
#pragma clang diagnostic push
618+
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
614619
- (BOOL)isOptional {
615-
return (description_->flags & GPBFieldOptional) != 0;
620+
return self.fieldType == GPBFieldTypeSingle && !self.isRequired;
616621
}
622+
#pragma clang diagnostic pop
617623

618624
- (GPBFieldType)fieldType {
619625
GPBFieldFlags flags = description_->flags;

objectivec/GPBDescriptor_PackagePrivate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef NS_OPTIONS(uint16_t, GPBFieldFlags) {
1919
GPBFieldRequired = 1 << 0,
2020
GPBFieldRepeated = 1 << 1,
2121
GPBFieldPacked = 1 << 2,
22-
GPBFieldOptional = 1 << 3,
22+
GPBFieldOptional __attribute__((deprecated)) = 1 << 3,
2323
GPBFieldHasDefaultValue = 1 << 4,
2424

2525
// Indicate that the field should "clear" when set to zero value. This is the

objectivec/GPBMessage.m

-2
Original file line numberDiff line numberDiff line change
@@ -1428,8 +1428,6 @@ - (BOOL)isInitialized {
14281428
return NO;
14291429
}
14301430
} else {
1431-
NSAssert(field.isOptional, @"%@: Single message field %@ not required or optional?",
1432-
[self class], field.name);
14331431
if (GPBGetHasIvarField(self, field)) {
14341432
GPBMessage *message = GPBGetMessageMessageField(self, field);
14351433
if (!message.initialized) {

objectivec/Tests/GPBMessage30007FormatTest.m

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ typedef GPB_ENUM(Message30007_SubMessage_FieldNumber) {
138138
NS_ASSUME_NONNULL_END
139139

140140
#pragma clang diagnostic push
141+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
141142
#pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
142143

143144
GPBObjCClassDeclaration(Message30007);

objectivec/Tests/GPBMessage40310FormatTest.m

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ typedef GPB_ENUM(Message40310_SubMessage_FieldNumber) {
137137
NS_ASSUME_NONNULL_END
138138

139139
#pragma clang diagnostic push
140+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
140141
#pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
141142

142143
GPBObjCClassDeclaration(Message40310);

0 commit comments

Comments
 (0)