Skip to content

Commit 9d469c6

Browse files
authored
Move off label and use isRepeated/isRequired. (#1765)
Continues tracking changes in protocolbuffers/protobuf#20687.
1 parent e4dcf71 commit 9d469c6

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

Sources/SwiftProtobufPluginLibrary/Descriptor.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,9 @@ public final class FieldDescriptor {
10891089
/// Declared type of this field.
10901090
public private(set) var type: Google_Protobuf_FieldDescriptorProto.TypeEnum
10911091

1092-
/// optional/required/repeated
1092+
/// This should never be called directly. Use isRequired and isRepeated
1093+
/// helper methods instead.
1094+
// TODO(thomasvl): @available(*, deprecated, message: "Use isRequired or isRepeated instead.")
10931095
public let label: Google_Protobuf_FieldDescriptorProto.Label
10941096

10951097
/// Whether or not the field is required. For proto2 required fields and
@@ -1102,13 +1104,13 @@ public final class FieldDescriptor {
11021104
public var isRepeated: Bool { label == .repeated }
11031105

11041106
/// Use !isRequired() && !isRepeated() instead.
1105-
@available(*, deprecated, message: "Use !isRequired() && !isRepeated() instead.")
1107+
@available(*, deprecated, message: "Use !isRequired && !isRepeated instead.")
11061108
public var isOptional: Bool { label == .optional }
11071109

11081110
/// Is this field packable.
11091111
public var isPackable: Bool {
11101112
// This logic comes from the C++ FieldDescriptor::is_packable() impl.
1111-
label == .repeated && FieldDescriptor.isPackable(type: type)
1113+
isRepeated && FieldDescriptor.isPackable(type: type)
11121114
}
11131115
/// If this field is packable and packed.
11141116
public var isPacked: Bool {
@@ -1140,7 +1142,7 @@ public final class FieldDescriptor {
11401142
/// repeated fields, and singular proto3 fields without "optional".
11411143
public var hasPresence: Bool {
11421144
// This logic comes from the C++ FieldDescriptor::has_presence() impl.
1143-
guard label != .repeated else { return false }
1145+
guard !isRepeated else { return false }
11441146
switch type {
11451147
case .group, .message:
11461148
// Groups/messages always get field presence.

Sources/protoc-gen-swift/Descriptor+Extensions.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -332,26 +332,24 @@ extension FieldDescriptor {
332332
case .sint64: result = "Int64"
333333
}
334334

335-
if label == .repeated {
335+
if isRepeated {
336336
return "[\(result)]"
337337
}
338338
return result
339339
}
340340

341341
func swiftStorageType(namer: SwiftProtobufNamer) -> String {
342342
let swiftType = self.swiftType(namer: namer)
343-
switch label {
344-
case .repeated:
343+
if isRepeated {
344+
return swiftType
345+
}
346+
guard realContainingOneof == nil else {
347+
return swiftType
348+
}
349+
if hasPresence {
350+
return "\(swiftType)?"
351+
} else {
345352
return swiftType
346-
case .optional, .required:
347-
guard realContainingOneof == nil else {
348-
return swiftType
349-
}
350-
if hasPresence {
351-
return "\(swiftType)?"
352-
} else {
353-
return swiftType
354-
}
355353
}
356354
}
357355

@@ -384,7 +382,7 @@ extension FieldDescriptor {
384382
if isMap {
385383
return "[:]"
386384
}
387-
if label == .repeated {
385+
if isRepeated {
388386
return "[]"
389387
}
390388

Sources/protoc-gen-swift/ExtensionSetGenerator.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ class ExtensionSetGenerator {
3333

3434
var extensionFieldType: String {
3535
let label: String
36-
switch fieldDescriptor.label {
37-
case .optional: label = "Optional"
38-
case .required: label = "Required"
39-
case .repeated: label = fieldDescriptor.isPacked ? "Packed" : "Repeated"
36+
if fieldDescriptor.isRequired {
37+
label = "Required"
38+
} else if fieldDescriptor.isRepeated {
39+
label = fieldDescriptor.isPacked ? "Packed" : "Repeated"
40+
} else {
41+
label = "Optional"
4042
}
4143

4244
let modifier: String
@@ -69,7 +71,8 @@ class ExtensionSetGenerator {
6971

7072
var fieldNamePath: String
7173
if fieldDescriptor.containingType.useMessageSetWireFormat && fieldDescriptor.type == .message
72-
&& fieldDescriptor.label == .optional && fieldDescriptor.messageType === fieldDescriptor.extensionScope
74+
&& (!fieldDescriptor.isRepeated && !fieldDescriptor.isRequired)
75+
&& fieldDescriptor.messageType === fieldDescriptor.extensionScope
7376
{
7477
fieldNamePath = fieldDescriptor.messageType!.fullName
7578
} else {
@@ -105,7 +108,8 @@ class ExtensionSetGenerator {
105108
p.print("}")
106109

107110
// Repeated extension fields can use .isEmpty and clear by setting to the empty list.
108-
if fieldDescriptor.label != .repeated {
111+
// Everything else gets a "has" helper.
112+
if !fieldDescriptor.isRepeated {
109113
p.print(
110114
"/// Returns true if extension `\(swiftFullExtensionName)`\n/// has been explicitly set.",
111115
"\(visibility)var \(extensionNames.has): Bool {"

0 commit comments

Comments
 (0)