Skip to content

Commit

Permalink
Conform to CustomDebugStringConvertible for Debug only
Browse files Browse the repository at this point in the history
  • Loading branch information
dflems committed May 9, 2022
1 parent b9ec2c4 commit a6d05ac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
28 changes: 27 additions & 1 deletion Sources/SwiftProtobuf/ExtensionFields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// equality with some other extension field; but it's type-sealed
// so you can't actually access the contained value itself.
//
public protocol AnyExtensionField: CustomDebugStringConvertible {
public protocol AnyExtensionField: _AnyExtensionField {
func hash(into hasher: inout Hasher)
var protobufExtension: AnyMessageExtension { get }
func isEqual(other: AnyExtensionField) -> Bool
Expand All @@ -39,6 +39,12 @@ public protocol AnyExtensionField: CustomDebugStringConvertible {
var isInitialized: Bool { get }
}

#if DEBUG
public protocol _AnyExtensionField: CustomDebugStringConvertible {}
#else
public protocol _AnyExtensionField {}
#endif

extension AnyExtensionField {
// Default implementation for extensions fields. The message types below provide
// custom versions.
Expand Down Expand Up @@ -74,11 +80,13 @@ public struct OptionalExtensionField<T: FieldType>: ExtensionField {
self.value = value
}

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

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -140,9 +148,11 @@ public struct RepeatedExtensionField<T: FieldType>: ExtensionField {
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try T.decodeRepeated(value: &value, from: &decoder)
Expand Down Expand Up @@ -192,9 +202,11 @@ public struct PackedExtensionField<T: FieldType>: ExtensionField {
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try T.decodeRepeated(value: &value, from: &decoder)
Expand Down Expand Up @@ -232,11 +244,13 @@ public struct OptionalEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
self.value = value
}

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

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
Expand Down Expand Up @@ -300,9 +314,11 @@ public struct RepeatedEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try decoder.decodeRepeatedEnumField(value: &value)
Expand Down Expand Up @@ -354,9 +370,11 @@ public struct PackedEnumExtensionField<E: Enum>: ExtensionField where E.RawValue
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try decoder.decodeRepeatedEnumField(value: &value)
Expand Down Expand Up @@ -397,11 +415,13 @@ public struct OptionalMessageExtensionField<M: Message & Equatable>:
self.value = value
}

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

public func hash(into hasher: inout Hasher) {
value.hash(into: &hasher)
Expand Down Expand Up @@ -468,9 +488,11 @@ public struct RepeatedMessageExtensionField<M: Message & Equatable>:
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try decoder.decodeRepeatedMessageField(value: &value)
Expand Down Expand Up @@ -521,7 +543,9 @@ public struct OptionalGroupExtensionField<G: Message & Hashable>:
hasher.combine(value)
}

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

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! OptionalGroupExtensionField<G>
Expand Down Expand Up @@ -577,9 +601,11 @@ public struct RepeatedGroupExtensionField<G: Message & Hashable>:
hasher.combine(value)
}

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

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! RepeatedGroupExtensionField<G>
Expand Down
10 changes: 9 additions & 1 deletion Sources/SwiftProtobuf/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
///
/// The actual functionality is implemented either in the generated code or in
/// default implementations of the below methods and properties.
public protocol Message: CustomDebugStringConvertible, _ProtoSendable {
public protocol Message: _MessageBase {
/// Creates a new message with all of its fields initialized to their default
/// values.
init()
Expand Down Expand Up @@ -112,6 +112,12 @@ public protocol Message: CustomDebugStringConvertible, _ProtoSendable {
func isEqualTo(message: Message) -> Bool
}

#if DEBUG
public protocol _MessageBase: _ProtoSendable, CustomDebugStringConvertible {}
#else
public protocol _MessageBase: _ProtoSendable {}
#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 @@ -129,6 +135,7 @@ extension Message {
hasher = visitor.hasher
}

#if DEBUG
/// A description generated by recursively visiting all fields in the message,
/// including messages.
public var debugDescription: String {
Expand All @@ -141,6 +148,7 @@ extension Message {
let header = "\(className):\n"
return header + textFormatString()
}
#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
8 changes: 6 additions & 2 deletions Sources/SwiftProtobuf/SimpleExtensionMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


// Note: The generated code only relies on ExpressibleByArrayLiteral
public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral, CustomDebugStringConvertible {
public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral {
public typealias Element = AnyMessageExtension

// Since type objects aren't Hashable, we can't do much better than this...
Expand Down Expand Up @@ -98,6 +98,10 @@ public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral, Custo
return out
}

}

#if DEBUG
extension SimpleExtensionMap: CustomDebugStringConvertible {
public var debugDescription: String {
var names = [String]()
for (_, list) in fields {
Expand All @@ -108,5 +112,5 @@ public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral, Custo
let d = names.joined(separator: ",")
return "SimpleExtensionMap(\(d))"
}

}
#endif

0 comments on commit a6d05ac

Please sign in to comment.