Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 10 additions & 2 deletions Sources/GRPCCore/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,23 @@ extension Metadata.Value: ExpressibleByArrayLiteral {

extension Metadata: CustomStringConvertible {
public var description: String {
String(describing: self.map({ ($0.key, $0.value) }))
var description = "["
description = self.dropLast().reduce(into: description) { partialResult, nextPair in
partialResult += "\"\(nextPair.key)\": \(nextPair.value), "
}
if let lastElement = self.last {
description += "\"\(lastElement.key)\": \(lastElement.value)"
}
description += "]"
return description
}
}

extension Metadata.Value: CustomStringConvertible {
public var description: String {
switch self {
case .string(let stringValue):
return String(describing: stringValue)
return "\"\(stringValue)\""
case .binary(let binaryValue):
return String(describing: binaryValue)
}
Expand Down
13 changes: 13 additions & 0 deletions Tests/GRPCCoreTests/MetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,17 @@ struct MetadataTests {
)
}
}

@Suite("Description")
struct Description {
@Test("Metadata")
func describeMetadata() async throws {
let metadata: Metadata = [
"key1": "value1",
"key2": "value2",
"key-bin": .binary([1, 2, 3]),
]
#expect("\(metadata)" == #"["key1": "value1", "key2": "value2", "key-bin": [1, 2, 3]]"#)
}
}
}
Loading