Skip to content

Move off label and use isRepeated/isRequired. #1765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Sources/SwiftProtobufPluginLibrary/Descriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,9 @@ public final class FieldDescriptor {
/// Declared type of this field.
public private(set) var type: Google_Protobuf_FieldDescriptorProto.TypeEnum

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

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

/// Use !isRequired() && !isRepeated() instead.
@available(*, deprecated, message: "Use !isRequired() && !isRepeated() instead.")
@available(*, deprecated, message: "Use !isRequired && !isRepeated instead.")
public var isOptional: Bool { label == .optional }

/// Is this field packable.
public var isPackable: Bool {
// This logic comes from the C++ FieldDescriptor::is_packable() impl.
label == .repeated && FieldDescriptor.isPackable(type: type)
isRepeated && FieldDescriptor.isPackable(type: type)
}
/// If this field is packable and packed.
public var isPacked: Bool {
Expand Down Expand Up @@ -1140,7 +1142,7 @@ public final class FieldDescriptor {
/// repeated fields, and singular proto3 fields without "optional".
public var hasPresence: Bool {
// This logic comes from the C++ FieldDescriptor::has_presence() impl.
guard label != .repeated else { return false }
guard !isRepeated else { return false }
switch type {
case .group, .message:
// Groups/messages always get field presence.
Expand Down
24 changes: 11 additions & 13 deletions Sources/protoc-gen-swift/Descriptor+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,26 +332,24 @@ extension FieldDescriptor {
case .sint64: result = "Int64"
}

if label == .repeated {
if isRepeated {
return "[\(result)]"
}
return result
}

func swiftStorageType(namer: SwiftProtobufNamer) -> String {
let swiftType = self.swiftType(namer: namer)
switch label {
case .repeated:
if isRepeated {
return swiftType
}
guard realContainingOneof == nil else {
return swiftType
}
if hasPresence {
return "\(swiftType)?"
} else {
return swiftType
case .optional, .required:
guard realContainingOneof == nil else {
return swiftType
}
if hasPresence {
return "\(swiftType)?"
} else {
return swiftType
}
}
}

Expand Down Expand Up @@ -384,7 +382,7 @@ extension FieldDescriptor {
if isMap {
return "[:]"
}
if label == .repeated {
if isRepeated {
return "[]"
}

Expand Down
16 changes: 10 additions & 6 deletions Sources/protoc-gen-swift/ExtensionSetGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class ExtensionSetGenerator {

var extensionFieldType: String {
let label: String
switch fieldDescriptor.label {
case .optional: label = "Optional"
case .required: label = "Required"
case .repeated: label = fieldDescriptor.isPacked ? "Packed" : "Repeated"
if fieldDescriptor.isRequired {
label = "Required"
} else if fieldDescriptor.isRepeated {
label = fieldDescriptor.isPacked ? "Packed" : "Repeated"
} else {
label = "Optional"
}

let modifier: String
Expand Down Expand Up @@ -69,7 +71,8 @@ class ExtensionSetGenerator {

var fieldNamePath: String
if fieldDescriptor.containingType.useMessageSetWireFormat && fieldDescriptor.type == .message
&& fieldDescriptor.label == .optional && fieldDescriptor.messageType === fieldDescriptor.extensionScope
&& (!fieldDescriptor.isRepeated && !fieldDescriptor.isRequired)
&& fieldDescriptor.messageType === fieldDescriptor.extensionScope
{
fieldNamePath = fieldDescriptor.messageType!.fullName
} else {
Expand Down Expand Up @@ -105,7 +108,8 @@ class ExtensionSetGenerator {
p.print("}")

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