Skip to content

Commit eea6b49

Browse files
gjcairoglbrntt
andauthored
Fix Metadata's description for prettier printing (#2185)
This PR changes `Metadata`'s description to include quotes around string values. From this: ``` [("some-key", some-value)] ``` to this: ``` [("some-key", "some-value")] ``` --------- Co-authored-by: George Barnett <[email protected]>
1 parent f163392 commit eea6b49

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Sources/GRPCCore/Metadata.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,13 @@ extension Metadata.Value: ExpressibleByArrayLiteral {
494494

495495
extension Metadata: CustomStringConvertible {
496496
public var description: String {
497-
String(describing: self.map({ ($0.key, $0.value) }))
497+
if self.isEmpty {
498+
return "[:]"
499+
} else {
500+
let elements = self.map { "\(String(reflecting: $0.key)): \(String(reflecting: $0.value))" }
501+
.joined(separator: ", ")
502+
return "[\(elements)]"
503+
}
498504
}
499505
}
500506

@@ -508,3 +514,14 @@ extension Metadata.Value: CustomStringConvertible {
508514
}
509515
}
510516
}
517+
518+
extension Metadata.Value: CustomDebugStringConvertible {
519+
public var debugDescription: String {
520+
switch self {
521+
case .string(let stringValue):
522+
return String(reflecting: stringValue)
523+
case .binary(let binaryValue):
524+
return String(reflecting: binaryValue)
525+
}
526+
}
527+
}

Tests/GRPCCoreTests/MetadataTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,34 @@ struct MetadataTests {
314314
)
315315
}
316316
}
317+
318+
@Suite("Description")
319+
struct Description {
320+
let metadata: Metadata = [
321+
"key1": "value1",
322+
"key2": "value2",
323+
"key-bin": .binary([1, 2, 3]),
324+
]
325+
326+
@Test("Metadata")
327+
func describeMetadata() async throws {
328+
#expect("\(self.metadata)" == #"["key1": "value1", "key2": "value2", "key-bin": [1, 2, 3]]"#)
329+
}
330+
331+
@Test("Metadata.Value")
332+
func describeMetadataValue() async throws {
333+
for (key, value) in self.metadata {
334+
switch key {
335+
case "key1":
336+
#expect("\(value)" == "value1")
337+
case "key2":
338+
#expect("\(value)" == "value2")
339+
case "key-bin":
340+
#expect("\(value)" == "[1, 2, 3]")
341+
default:
342+
Issue.record("Should not have reached this point")
343+
}
344+
}
345+
}
346+
}
317347
}

0 commit comments

Comments
 (0)