Skip to content
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

Conform to CustomDebugStringConvertible for Debug only #1246

Merged
merged 3 commits into from
Sep 2, 2022
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
134 changes: 89 additions & 45 deletions 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 {
func hash(into hasher: inout Hasher)
var protobufExtension: AnyMessageExtension { get }
func isEqual(other: AnyExtensionField) -> Bool
Expand Down Expand Up @@ -74,12 +74,6 @@ public struct OptionalExtensionField<T: FieldType>: ExtensionField {
self.value = value
}

public var debugDescription: String {
get {
return String(reflecting: value)
}
}

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

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

///
/// Repeated fields
///
Expand Down Expand Up @@ -140,10 +144,6 @@ public struct RepeatedExtensionField<T: FieldType>: ExtensionField {
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try T.decodeRepeated(value: &value, from: &decoder)
}
Expand All @@ -161,6 +161,14 @@ 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 Down Expand Up @@ -192,10 +200,6 @@ public struct PackedExtensionField<T: FieldType>: ExtensionField {
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try T.decodeRepeated(value: &value, from: &decoder)
}
Expand All @@ -213,6 +217,14 @@ 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 @@ -232,12 +244,6 @@ public struct OptionalEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
self.value = value
}

public var debugDescription: String {
get {
return String(reflecting: value)
}
}

public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
Expand Down Expand Up @@ -272,6 +278,16 @@ 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 Down Expand Up @@ -300,10 +316,6 @@ public struct RepeatedEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try decoder.decodeRepeatedEnumField(value: &value)
}
Expand All @@ -323,6 +335,14 @@ 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 Down Expand Up @@ -354,10 +374,6 @@ public struct PackedEnumExtensionField<E: Enum>: ExtensionField where E.RawValue
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try decoder.decodeRepeatedEnumField(value: &value)
}
Expand All @@ -377,6 +393,14 @@ 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 @@ -397,12 +421,6 @@ public struct OptionalMessageExtensionField<M: Message & Equatable>:
self.value = value
}

public var debugDescription: String {
get {
return String(reflecting: value)
}
}

public func hash(into hasher: inout Hasher) {
value.hash(into: &hasher)
}
Expand Down Expand Up @@ -440,6 +458,16 @@ 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 Down Expand Up @@ -468,10 +496,6 @@ public struct RepeatedMessageExtensionField<M: Message & Equatable>:
return self == o
}

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

public mutating func decodeExtensionField<D: Decoder>(decoder: inout D) throws {
try decoder.decodeRepeatedMessageField(value: &value)
}
Expand All @@ -494,6 +518,14 @@ 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 Down Expand Up @@ -521,8 +553,6 @@ public struct OptionalGroupExtensionField<G: Message & Hashable>:
hasher.combine(value)
}

public var debugDescription: String { get {return value.debugDescription} }

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! OptionalGroupExtensionField<G>
return self == o
Expand Down Expand Up @@ -556,6 +586,16 @@ 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 @@ -577,10 +617,6 @@ public struct RepeatedGroupExtensionField<G: Message & Hashable>:
hasher.combine(value)
}

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

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! RepeatedGroupExtensionField<G>
return self == o
Expand All @@ -607,3 +643,11 @@ 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
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
20 changes: 20 additions & 0 deletions Tests/SwiftProtobufTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,26 @@ extension PBTestHelpers where MessageTestType: SwiftProtobuf.Message & Equatable
// Yay! It failed!
}
}

func assertDebugDescription(_ expected: String, file: XCTestFileArgType = #file, line: UInt = #line, configure: (inout MessageTestType) -> ()) {
// `assertDebugDescription` is a no-op in release as `debugDescription` is unavailable.
#if DEBUG
var m = MessageTestType()
configure(&m)
let actual = m.debugDescription
XCTAssertEqual(actual, expected, file: file, line: line)
#endif
}
}

extension XCTestCase {
func assertDebugDescription(_ expected: String, _ m: SwiftProtobuf.Message, fmt: String? = nil, file: XCTestFileArgType = #file, line: UInt = #line) {
// `assertDebugDescription` is a no-op in release as `debugDescription` is unavailable.
#if DEBUG
let actual = m.debugDescription
XCTAssertEqual(actual, expected, fmt ?? "debugDescription did not match", file: file, line: line)
#endif
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an assertDebugDescription() variant where you can pass in an existing SwiftProtobuf.Message. Both of the functions no-op for non-debug builds.

}

/// Protocol to help write visitor for testing. It provides default implementaions
Expand Down
Loading