From 41b89197ba8ce3c02f83154431b77dac1da21981 Mon Sep 17 00:00:00 2001 From: Gus Cairo Date: Mon, 8 Apr 2024 15:06:38 +0100 Subject: [PATCH] Move only implementation of debugDescription into #if DEBUG clauses squash --- Sources/SwiftProtobuf/ExtensionFields.swift | 170 +++++++++--------- Sources/SwiftProtobuf/Message.swift | 15 +- .../SwiftProtobuf/SimpleExtensionMap.swift | 6 +- 3 files changed, 90 insertions(+), 101 deletions(-) diff --git a/Sources/SwiftProtobuf/ExtensionFields.swift b/Sources/SwiftProtobuf/ExtensionFields.swift index 435fc210e..c1696041a 100644 --- a/Sources/SwiftProtobuf/ExtensionFields.swift +++ b/Sources/SwiftProtobuf/ExtensionFields.swift @@ -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 @@ -73,6 +73,14 @@ public struct OptionalExtensionField: 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) @@ -106,16 +114,6 @@ public struct OptionalExtensionField: ExtensionField { } } -#if DEBUG -extension OptionalExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - get { - return String(reflecting: value) - } - } -} -#endif - /// /// Repeated fields /// @@ -134,6 +132,14 @@ public struct RepeatedExtensionField: 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) @@ -161,14 +167,6 @@ public struct RepeatedExtensionField: ExtensionField { } } -#if DEBUG -extension RepeatedExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" - } -} -#endif - /// /// Packed Repeated fields /// @@ -190,6 +188,14 @@ public struct PackedExtensionField: 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) @@ -217,14 +223,6 @@ public struct PackedExtensionField: ExtensionField { } } -#if DEBUG -extension PackedExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" - } -} -#endif - /// /// Enum extensions /// @@ -243,6 +241,14 @@ public struct OptionalEnumExtensionField: 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) @@ -278,16 +284,6 @@ public struct OptionalEnumExtensionField: ExtensionField where E.RawVal } } -#if DEBUG -extension OptionalEnumExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - get { - return String(reflecting: value) - } - } -} -#endif - /// /// Repeated Enum fields /// @@ -306,6 +302,14 @@ public struct RepeatedEnumExtensionField: 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) @@ -335,14 +339,6 @@ public struct RepeatedEnumExtensionField: 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 /// @@ -364,6 +360,14 @@ public struct PackedEnumExtensionField: 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) @@ -393,14 +397,6 @@ public struct PackedEnumExtensionField: ExtensionField where E.RawValue } } -#if DEBUG -extension PackedEnumExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" - } -} -#endif - // // ========== Message ========== // @@ -420,6 +416,14 @@ public struct OptionalMessageExtensionField: 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) @@ -458,16 +462,6 @@ public struct OptionalMessageExtensionField: } } -#if DEBUG -extension OptionalMessageExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - get { - return String(reflecting: value) - } - } -} -#endif - public struct RepeatedMessageExtensionField: ExtensionField { public typealias BaseType = M @@ -484,6 +478,14 @@ public struct RepeatedMessageExtensionField: 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 { @@ -518,14 +520,6 @@ public struct RepeatedMessageExtensionField: } } -#if DEBUG -extension RepeatedMessageExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - return "[" + value.map{String(reflecting: $0)}.joined(separator: ",") + "]" - } -} -#endif - // // ======== Groups within Messages ======== // @@ -548,6 +542,14 @@ public struct OptionalGroupExtensionField: 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) @@ -586,16 +588,6 @@ public struct OptionalGroupExtensionField: } } -#if DEBUG -extension OptionalGroupExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - get { - return value.debugDescription - } - } -} -#endif - public struct RepeatedGroupExtensionField: ExtensionField { public typealias BaseType = G @@ -612,6 +604,14 @@ public struct RepeatedGroupExtensionField: 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) @@ -643,11 +643,3 @@ public struct RepeatedGroupExtensionField: return Internal.areAllInitialized(value) } } - -#if DEBUG -extension RepeatedGroupExtensionField: CustomDebugStringConvertible { - public var debugDescription: String { - return "[" + value.map{$0.debugDescription}.joined(separator: ",") + "]" - } -} -#endif diff --git a/Sources/SwiftProtobuf/Message.swift b/Sources/SwiftProtobuf/Message.swift index 047f8d0c4..4405dd270 100644 --- a/Sources/SwiftProtobuf/Message.swift +++ b/Sources/SwiftProtobuf/Message.swift @@ -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() @@ -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 @@ -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]( @@ -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` diff --git a/Sources/SwiftProtobuf/SimpleExtensionMap.swift b/Sources/SwiftProtobuf/SimpleExtensionMap.swift index 5c7da6015..7c83bc5b5 100644 --- a/Sources/SwiftProtobuf/SimpleExtensionMap.swift +++ b/Sources/SwiftProtobuf/SimpleExtensionMap.swift @@ -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 { @@ -111,6 +111,8 @@ extension SimpleExtensionMap: CustomDebugStringConvertible { } let d = names.joined(separator: ",") return "SimpleExtensionMap(\(d))" + #else + return String(reflecting: type(of: self)) + #endif } } -#endif