Skip to content

Commit 782e79f

Browse files
committed
PR changes
1 parent 17a140f commit 782e79f

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

Sources/GRPCCore/Metadata.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,20 +494,29 @@ extension Metadata.Value: ExpressibleByArrayLiteral {
494494

495495
extension Metadata: CustomStringConvertible {
496496
public var description: String {
497-
var description = "["
498-
description = self.dropLast().reduce(into: description) { partialResult, nextPair in
499-
partialResult += "\"\(nextPair.key)\": \(nextPair.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)]"
500503
}
501-
if let lastElement = self.last {
502-
description += "\"\(lastElement.key)\": \(lastElement.value)"
503-
}
504-
description += "]"
505-
return description
506504
}
507505
}
508506

509507
extension Metadata.Value: CustomStringConvertible {
510508
public var description: String {
509+
switch self {
510+
case .string(let stringValue):
511+
return stringValue
512+
case .binary(let binaryValue):
513+
return String(describing: binaryValue)
514+
}
515+
}
516+
}
517+
518+
extension Metadata.Value: CustomDebugStringConvertible {
519+
public var debugDescription: String {
511520
switch self {
512521
case .string(let stringValue):
513522
return "\"\(stringValue)\""

Tests/GRPCCoreTests/MetadataTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,21 @@ struct MetadataTests {
324324
"key2": "value2",
325325
"key-bin": .binary([1, 2, 3]),
326326
]
327+
327328
#expect("\(metadata)" == #"["key1": "value1", "key2": "value2", "key-bin": [1, 2, 3]]"#)
329+
330+
for (key, value) in metadata {
331+
switch key {
332+
case "key1":
333+
#expect("\(value)" == "value1")
334+
case "key2":
335+
#expect("\(value)" == "value2")
336+
case "key-bin":
337+
#expect("\(value)" == "[1, 2, 3]")
338+
default:
339+
Issue.record("Should not have reached this point")
340+
}
341+
}
328342
}
329343
}
330344
}

0 commit comments

Comments
 (0)