Skip to content

Commit

Permalink
Move only implementation of debugDescription into #if DEBUG clauses
Browse files Browse the repository at this point in the history
squash
  • Loading branch information
gjcairo committed Apr 17, 2024
1 parent f7aab42 commit 41b8919
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 101 deletions.
170 changes: 81 additions & 89 deletions Sources/SwiftProtobuf/ExtensionFields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// so you can't actually access the contained value itself.
//
@preconcurrency
public protocol AnyExtensionField: Sendable {
public protocol AnyExtensionField: Sendable, CustomDebugStringConvertible {
func hash(into hasher: inout Hasher)
var protobufExtension: any AnyMessageExtension { get }
func isEqual(other: any AnyExtensionField) -> Bool
Expand Down Expand Up @@ -73,6 +73,14 @@ public struct OptionalExtensionField<T: FieldType>: ExtensionField {
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return String(reflecting: value)
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -106,16 +114,6 @@ public struct OptionalExtensionField<T: FieldType>: ExtensionField {
}
}

#if DEBUG
extension OptionalExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
get {
return String(reflecting: value)
}
}
}
#endif

///
/// Repeated fields
///
Expand All @@ -134,6 +132,14 @@ public struct RepeatedExtensionField<T: FieldType>: ExtensionField {
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -161,14 +167,6 @@ public struct RepeatedExtensionField<T: FieldType>: ExtensionField {
}
}

#if DEBUG
extension RepeatedExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
}
}
#endif

///
/// Packed Repeated fields
///
Expand All @@ -190,6 +188,14 @@ public struct PackedExtensionField<T: FieldType>: ExtensionField {
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -217,14 +223,6 @@ public struct PackedExtensionField<T: FieldType>: ExtensionField {
}
}

#if DEBUG
extension PackedExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
}
}
#endif

///
/// Enum extensions
///
Expand All @@ -243,6 +241,14 @@ public struct OptionalEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return String(reflecting: value)
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -278,16 +284,6 @@ public struct OptionalEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
}
}

#if DEBUG
extension OptionalEnumExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
get {
return String(reflecting: value)
}
}
}
#endif

///
/// Repeated Enum fields
///
Expand All @@ -306,6 +302,14 @@ public struct RepeatedEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -335,14 +339,6 @@ public struct RepeatedEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
}
}

#if DEBUG
extension RepeatedEnumExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
}
}
#endif

///
/// Packed Repeated Enum fields
///
Expand All @@ -364,6 +360,14 @@ public struct PackedEnumExtensionField<E: Enum>: ExtensionField where E.RawValue
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -393,14 +397,6 @@ public struct PackedEnumExtensionField<E: Enum>: ExtensionField where E.RawValue
}
}

#if DEBUG
extension PackedEnumExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
}
}
#endif

//
// ========== Message ==========
//
Expand All @@ -420,6 +416,14 @@ public struct OptionalMessageExtensionField<M: Message & Equatable>:
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return String(reflecting: value)
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
value.hash(into: &hasher)
Expand Down Expand Up @@ -458,16 +462,6 @@ public struct OptionalMessageExtensionField<M: Message & Equatable>:
}
}

#if DEBUG
extension OptionalMessageExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
get {
return String(reflecting: value)
}
}
}
#endif

public struct RepeatedMessageExtensionField<M: Message & Equatable>:
ExtensionField {
public typealias BaseType = M
Expand All @@ -484,6 +478,14 @@ public struct RepeatedMessageExtensionField<M: Message & Equatable>:
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
for e in value {
Expand Down Expand Up @@ -518,14 +520,6 @@ public struct RepeatedMessageExtensionField<M: Message & Equatable>:
}
}

#if DEBUG
extension RepeatedMessageExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]"
}
}
#endif

//
// ======== Groups within Messages ========
//
Expand All @@ -548,6 +542,14 @@ public struct OptionalGroupExtensionField<G: Message & Hashable>:
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return value.debugDescription
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -586,16 +588,6 @@ public struct OptionalGroupExtensionField<G: Message & Hashable>:
}
}

#if DEBUG
extension OptionalGroupExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
get {
return value.debugDescription
}
}
}
#endif

public struct RepeatedGroupExtensionField<G: Message & Hashable>:
ExtensionField {
public typealias BaseType = G
Expand All @@ -612,6 +604,14 @@ public struct RepeatedGroupExtensionField<G: Message & Hashable>:
self.protobufExtension = protobufExtension
self.value = value
}

public var debugDescription: String {
#if DEBUG
return "[" + value.map{$0.debugDescription}.joined(separator: ",") + "]"
#else
return String(reflecting: type(of: self))
#endif
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -643,11 +643,3 @@ public struct RepeatedGroupExtensionField<G: Message & Hashable>:
return Internal.areAllInitialized(value)
}
}

#if DEBUG
extension RepeatedGroupExtensionField: CustomDebugStringConvertible {
public var debugDescription: String {
return "[" + value.map{$0.debugDescription}.joined(separator: ",") + "]"
}
}
#endif
15 changes: 5 additions & 10 deletions Sources/SwiftProtobuf/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/// The actual functionality is implemented either in the generated code or in
/// default implementations of the below methods and properties.
@preconcurrency
public protocol Message: _CommonMessageConformances {
public protocol Message: Sendable, CustomDebugStringConvertible {
/// Creates a new message with all of its fields initialized to their default
/// values.
init()
Expand Down Expand Up @@ -113,13 +113,6 @@ public protocol Message: _CommonMessageConformances {
func isEqualTo(message: any Message) -> Bool
}

#if DEBUG
public typealias _CommonMessageConformances =
Sendable & CustomDebugStringConvertible
#else
public typealias _CommonMessageConformances = Sendable
#endif

extension Message {
/// Generated proto2 messages that contain required fields, nested messages
/// that contain required fields, and/or extensions will provide their own
Expand All @@ -137,10 +130,10 @@ extension Message {
hasher = visitor.hasher
}

#if DEBUG
/// A description generated by recursively visiting all fields in the message,
/// including messages.
public var debugDescription: String {
#if DEBUG
// TODO Ideally there would be something like serializeText() that can
// take a prefix so we could do something like:
// [class name](
Expand All @@ -149,8 +142,10 @@ extension Message {
let className = String(reflecting: type(of: self))
let header = "\(className):\n"
return header + textFormatString()
#else
return String(reflecting: type(of: self))
#endif
}
#endif

/// Creates an instance of the message type on which this method is called,
/// executes the given block passing the message in as its sole `inout`
Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftProtobuf/SimpleExtensionMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral {

}

#if DEBUG
extension SimpleExtensionMap: CustomDebugStringConvertible {
public var debugDescription: String {
#if DEBUG
var names = [String]()
for (_, list) in fields {
for e in list {
Expand All @@ -111,6 +111,8 @@ extension SimpleExtensionMap: CustomDebugStringConvertible {
}
let d = names.joined(separator: ",")
return "SimpleExtensionMap(\(d))"
#else
return String(reflecting: type(of: self))
#endif
}
}
#endif

0 comments on commit 41b8919

Please sign in to comment.