Skip to content

Commit

Permalink
hotfix: infinity-loop in @published property
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeHosonuma committed Jun 19, 2022
1 parent e35e9b4 commit a72aa8a
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Sources/Core/PrettyDescriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ struct PrettyDescriber {
let typeName = String(describing: target.self)

let propertyWrappers: [(String, String)] = [
("Published", "currentValue"),
("StateObject", "wrappedValue"),
("ObservedObject", "wrappedValue"),
("EnvironmentObject", "_store"),
Expand Down Expand Up @@ -227,6 +226,17 @@ struct PrettyDescriber {
return formatter.objectString(typeName: "@\(name)", fields: objectFields(target, debug: debug))
}

//
// @Published
//
// Note:
// Direct lookups because some data structures infinity-loop with circular references.
//
if typeName.hasPrefix("Published") {
let value = lookup(keyPath: ["storage", "publisher", "subject", "currentValue"], from: target)
return "@Published(\(__string(value)))"
}

//
// @Namespace
//
Expand Down Expand Up @@ -472,6 +482,29 @@ struct PrettyDescriber {
return nil
}

private func lookup(keyPath: [String], from: Any) -> Any? {
var target: Any = from

for key in keyPath {
if let value = lookup(key: key, from: target) {
target = value
} else {
return nil
}
}

return target
}

private func lookup(key: String, from target: Any) -> Any? {
for child in Mirror(reflecting: target).children {
if let label = child.label, label == key {
return child.value
}
}
return nil
}

private func handleError(_ f: () throws -> String) -> String {
do {
return try f()
Expand Down

0 comments on commit a72aa8a

Please sign in to comment.