Skip to content

Commit 9792105

Browse files
authored
Improve metadata debug functionality with better field handling (#182)
1 parent 989275d commit 9792105

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

Sources/OpenAttributeGraphShims/Metadata+Debug.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension Metadata {
4242
switch kind {
4343
case .enum:
4444
write(&result, string: "enum \(type) {", level: level)
45-
_ = forEachField(options: [.enumerateEnumCases]) { name, offset, type in // anything contains ._4 will work here
45+
_ = forEachField(options: [.enumerateEnumCases]) { name, offset, type in
4646
let fieldName = String(cString: name)
4747
write(&result, string: "case \(fieldName)(\(type)) // offset = \(offset.hex)", level: level+1)
4848
if recursive {
@@ -52,7 +52,7 @@ extension Metadata {
5252
}
5353
write(&result, string: "}", level: level)
5454
case .optional:
55-
_ = forEachField(options: [.enumerateEnumCases]) { name, offset, type in // anything contains ._4 will work here
55+
_ = forEachField(options: [.enumerateEnumCases]) { name, offset, type in
5656
let fieldName = String(cString: name)
5757
write(&result, string: "case \(fieldName)(\(type)) // offset = \(offset.hex)", level: level+1)
5858
if recursive {
@@ -62,7 +62,7 @@ extension Metadata {
6262
}
6363
case .struct:
6464
write(&result, string: "struct \(type) {", level: level)
65-
_ = forEachField(options: []) { name, offset, type in // only [] and [._2] will work here
65+
_ = forEachField(options: [.continueAfterUnknownField]) { name, offset, type in
6666
let fieldName = String(cString: name)
6767
write(&result, string: "var \(fieldName): \(type) // offset = \(offset.hex)", level: level+1)
6868
if recursive {
@@ -74,7 +74,7 @@ extension Metadata {
7474
case .tuple: break
7575
case .class:
7676
write(&result, string: "class \(type) {", level: level)
77-
_ = forEachField(options: [.enumerateClassFields]) { name, offset, type in // anything contains ._1 will work here
77+
_ = forEachField(options: [.enumerateClassFields, .continueAfterUnknownField]) { name, offset, type in
7878
let fieldName = String(cString: name)
7979

8080
write(&result, string: "var \(fieldName): \(type) // offset = \(offset.hex)", level: level+1)

Tests/OpenAttributeGraphShimsTests/MetadataDebugTests.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
@_spi(Debug) import OpenAttributeGraphShims
66
import Testing
77

8+
#if canImport(UIKit)
9+
import UIKit
10+
private typealias PlatformView = UIView
11+
#elseif canImport(AppKit)
12+
import AppKit
13+
private typealias PlatformView = NSView
14+
#endif
15+
816
@Suite(.disabled(if: !attributeGraphEnabled, "forEachField is not implemented for OAG"))
917
struct MetadataDebugTests {
1018
struct Demo1 {
@@ -16,7 +24,7 @@ struct MetadataDebugTests {
1624
var a: Int = .zero
1725
var b: Double = .zero
1826
}
19-
27+
2028
@Test
2129
func layout() {
2230
#expect(Metadata(Demo1.self).layoutDescription == #"""
@@ -35,5 +43,25 @@ struct MetadataDebugTests {
3543
3644
"""#)
3745
}
38-
46+
47+
#if canImport(UIKit) || canImport(AppKit)
48+
@Test
49+
func unknownFields() {
50+
class DemoView: PlatformView {
51+
var a: Int = .zero
52+
var b: Double = .zero
53+
}
54+
let layoutDescription = Metadata(DemoView.self).layoutDescription.split(separator: "\n").map(String.init)
55+
#expect(layoutDescription[0] == "class DemoView {")
56+
#expect(layoutDescription[1].contains("var a: Int // offset = "))
57+
#expect(layoutDescription[2].contains("var b: Double // offset = "))
58+
#expect(layoutDescription[3] == "}")
59+
}
60+
#endif
61+
}
62+
63+
extension Int {
64+
fileprivate var hex: String {
65+
"0x\(String(format:"%X", self))"
66+
}
3967
}

0 commit comments

Comments
 (0)