Skip to content

Commit

Permalink
Add CustomDebugStringConvertible conformance in debug-only extension …
Browse files Browse the repository at this point in the history
…for *ExtensionField
  • Loading branch information
dflems authored and thomasvl committed Sep 2, 2022
1 parent 5751fbb commit 56bc30f
Showing 1 changed file with 89 additions and 71 deletions.
160 changes: 89 additions & 71 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: _AnyExtensionField {
public protocol AnyExtensionField {
func hash(into hasher: inout Hasher)
var protobufExtension: AnyMessageExtension { get }
func isEqual(other: AnyExtensionField) -> Bool
Expand All @@ -39,12 +39,6 @@ public protocol AnyExtensionField: _AnyExtensionField {
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 @@ -80,14 +74,6 @@ 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 @@ -120,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 @@ -148,12 +144,6 @@ 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 All @@ -171,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 @@ -202,12 +200,6 @@ 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 All @@ -225,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 @@ -244,14 +244,6 @@ 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 @@ -286,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 @@ -314,12 +316,6 @@ 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 All @@ -339,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 @@ -370,12 +374,6 @@ 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 All @@ -395,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 @@ -415,14 +421,6 @@ 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 @@ -460,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 @@ -488,12 +496,6 @@ 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 All @@ -516,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 @@ -543,10 +553,6 @@ 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>
return self == o
Expand Down Expand Up @@ -580,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 @@ -601,12 +617,6 @@ 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>
return self == o
Expand All @@ -633,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

0 comments on commit 56bc30f

Please sign in to comment.