Skip to content

Commit b9d85bc

Browse files
committed
Prettier AnyComp debug print on status page
1 parent 8395e64 commit b9d85bc

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Sources/allonet2/PlaceContents+Codable.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Logging
99
import PotentCodables
10+
import Foundation
1011

1112
extension PlaceContents: Equatable
1213
{
@@ -92,7 +93,20 @@ public struct AnyComponent: Codable, Equatable
9293

9394
func indentedDescription(_ prefix: String) -> String
9495
{
95-
return decodedIfAvailable()?.indentedDescription(prefix) ?? "\(prefix)AnyComponent<\(componentTypeId)>: \(anyValue.description)"
96+
if let decoded = decodedIfAvailable() {
97+
return decoded.indentedDescription(prefix)
98+
} else {
99+
var desc = "\(prefix)[\(componentTypeId)]:"
100+
let encoder = JSONEncoder()
101+
encoder.outputFormatting = .prettyPrinted
102+
guard
103+
let data = try? encoder.encode(self.anyValue),
104+
let string = String(data: data, encoding: .utf8)
105+
else { return desc }
106+
let lines = string.split(separator: "\n").dropFirst().dropLast()
107+
desc += "\n\(prefix)\t" + lines.joined(separator: "\n\(prefix)\t")
108+
return desc
109+
}
96110
}
97111

98112
// MARK: Codable

0 commit comments

Comments
 (0)