Skip to content

Deprecate FieldDescriptor.isOptional. #1764

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
13 changes: 7 additions & 6 deletions Sources/SwiftProtobufPluginLibrary/Descriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1092,18 +1092,19 @@ public final class FieldDescriptor {
/// optional/required/repeated
public let label: Google_Protobuf_FieldDescriptorProto.Label

/// Shorthand for `label` == `.required`.
///
/// NOTE: This could also be a map as the are also repeated fields.
/// Whether or not the field is required. For proto2 required fields and
/// Editions `LEGACY_REQUIRED` fields.
public var isRequired: Bool {
// Implementation comes from FieldDescriptor::is_required()
features.fieldPresence == .legacyRequired
}
/// Shorthand for `label` == `.optional`
public var isOptional: Bool { label == .optional }
/// Shorthand for `label` == `.repeated`
/// Whether or not the field is repeated/map field.
public var isRepeated: Bool { label == .repeated }

/// 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.
Expand Down
4 changes: 2 additions & 2 deletions Sources/protoc-gen-swift/MessageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ class MessageGenerator {
"\(e.containingType.fullName) has the option message_set_wire_format but \(e.fullName) is a non message extension field."
return
}
guard e.isOptional else {
guard !e.isRequired && !e.isRepeated else {
errorString =
"\(e.containingType.fullName) has the option message_set_wire_format but \(e.fullName) is not a \"optional\" extension field."
"\(e.containingType.fullName) has the option message_set_wire_format but \(e.fullName) cannot be required nor repeated extension field."
return
}
}
Expand Down