Skip to content

Commit 43739e6

Browse files
jcmoscKyle-Ye
andauthored
Metadata interface updates (#174)
Co-authored-by: Kyle <[email protected]>
1 parent 65a976c commit 43739e6

File tree

7 files changed

+84
-38
lines changed

7 files changed

+84
-38
lines changed

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/OpenGraph/Runtime/Metadata.swift

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,65 @@
77

88
public import OpenGraphCxx
99
#if canImport(ObjectiveC)
10-
public import Foundation
10+
import Foundation
1111
#endif
1212

1313
@_silgen_name("OGTypeApplyFields")
14-
public func OGTypeApplyFields(
15-
_ type: Any.Type,
14+
private func OGTypeApplyFields(
15+
of type: Any.Type,
1616
body: (UnsafePointer<Int8>, Int, Any.Type) -> Void
1717
)
1818

1919
@_silgen_name("OGTypeApplyFields2")
20-
public func OGTypeApplyFields2(
21-
_ type: Any.Type,
22-
options: OGTypeApplyOptions,
20+
private func OGTypeApplyFields2(
21+
of type: Any.Type,
22+
options: Metadata.ApplyOptions,
2323
body: (UnsafePointer<Int8>, Int, Any.Type) -> Bool
2424
) -> Bool
2525

26+
public func forEachField(
27+
of type: Any.Type,
28+
do body: (UnsafePointer<Int8>, Int, Any.Type) -> Void
29+
) {
30+
OGTypeApplyFields(of: type, body: body)
31+
}
32+
2633
extension Metadata: Swift.Hashable, Swift.CustomStringConvertible {
27-
@inlinable
28-
@inline(__always)
2934
public init(_ type: any Any.Type) {
3035
self.init(rawValue: unsafeBitCast(type, to: UnsafePointer<_Metadata>.self))
3136
}
32-
33-
@inlinable
34-
@inline(__always)
37+
3538
public var type: any Any.Type {
3639
unsafeBitCast(rawValue, to: Any.Type.self)
3740
}
38-
39-
@inlinable
40-
@inline(__always)
41+
4142
public var description: String {
4243
#if canImport(ObjectiveC)
4344
__OGTypeDescription(self) as NSString as String
4445
#else
4546
fatalError("Unimplemented")
4647
#endif
4748
}
48-
49-
@inlinable
50-
@inline(__always)
51-
/* public */func forEachField(
52-
do body: (UnsafePointer<Int8>, Int, Any.Type) -> Void
53-
) {
54-
OGTypeApplyFields(type, body: body)
55-
}
56-
57-
@inlinable
58-
@inline(__always)
49+
5950
public func forEachField(
60-
options: OGTypeApplyOptions,
51+
options: ApplyOptions,
6152
do body: (UnsafePointer<Int8>, Int, Any.Type) -> Bool
6253
) -> Bool {
63-
OGTypeApplyFields2(type, options: options, body: body)
54+
OGTypeApplyFields2(of: type, options: options, body: body)
55+
}
56+
}
57+
58+
extension Signature: Swift.Equatable {
59+
public static func == (_ lhs: Signature, _ rhs: Signature) -> Bool {
60+
return lhs.bytes.0 == rhs.bytes.0 && lhs.bytes.1 == rhs.bytes.1
61+
&& lhs.bytes.2 == rhs.bytes.2 && lhs.bytes.3 == rhs.bytes.3
62+
&& lhs.bytes.4 == rhs.bytes.4 && lhs.bytes.5 == rhs.bytes.5
63+
&& lhs.bytes.6 == rhs.bytes.6 && lhs.bytes.7 == rhs.bytes.7
64+
&& lhs.bytes.8 == rhs.bytes.8 && lhs.bytes.9 == rhs.bytes.9
65+
&& lhs.bytes.10 == rhs.bytes.10 && lhs.bytes.11 == rhs.bytes.11
66+
&& lhs.bytes.12 == rhs.bytes.12 && lhs.bytes.13 == rhs.bytes.13
67+
&& lhs.bytes.14 == rhs.bytes.14 && lhs.bytes.15 == rhs.bytes.15
68+
&& lhs.bytes.16 == rhs.bytes.16 && lhs.bytes.17 == rhs.bytes.17
69+
&& lhs.bytes.18 == rhs.bytes.18 && lhs.bytes.19 == rhs.bytes.19
6470
}
6571
}

Sources/OpenGraphCxx/include/OpenGraph/OGTypeID.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ typedef OG_OPTIONS(uint32_t, OGTypeApplyOptions) {
3737
OGTypeApplyOptionsEnumerateClassFields = 1 << 0,
3838
OGTypeApplyOptionsContinueAfterUnknownField = 1 << 1,
3939
OGTypeApplyOptionsEnumerateEnumCases = 1 << 2,
40-
};
40+
} OG_SWIFT_NAME(Metadata.ApplyOptions);
4141

4242
#if OPENGRAPH_RELEASE >= OPENGRAPH_RELEASE_2024
4343

Sources/OpenGraphShims/GraphShims.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public typealias OGAttributeInfo = AGAttributeInfo
88
public typealias OGCachedValueOptions = AGCachedValueOptions
99
public typealias OGChangedValueFlags = AGChangedValueFlags
1010
public typealias OGInputOptions = AGInputOptions
11-
public typealias OGTypeApplyOptions = AGTypeApplyOptions
1211
public typealias OGUniqueID = AGUniqueID
1312
public typealias OGValue = AGValue
1413
public typealias OGValueOptions = AGValueOptions

Tests/OpenGraphCompatibilityTests/Attribute/AttributeTestBase.swift

Whitespace-only changes.

Tests/OpenGraphCompatibilityTests/GraphShims.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public typealias OGAttributeInfo = AGAttributeInfo
88
public typealias OGCachedValueOptions = AGCachedValueOptions
99
public typealias OGChangedValueFlags = AGChangedValueFlags
1010
public typealias OGInputOptions = AGInputOptions
11-
public typealias OGTypeApplyOptions = AGTypeApplyOptions
1211
public typealias OGUniqueID = AGUniqueID
1312
public typealias OGValue = AGValue
1413
public typealias OGValueOptions = AGValueOptions

Tests/OpenGraphCompatibilityTests/Runtime/MetadataCompatibilityTests.swift

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ struct MetadataCompatibilityTests {
157157
#expect(Metadata(T2.self).description == "MetadataCompatibilityTests.T2")
158158
#expect(Metadata(T3.self).description == "MetadataCompatibilityTests.T3")
159159
}
160-
160+
161+
@available(*, deprecated, message: "To be removed")
161162
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
162-
func forEachField() throws {
163-
for options in [OGTypeApplyOptions.enumerateClassFields] {
163+
func legacyForEachField() throws {
164+
for options in [Metadata.ApplyOptions.enumerateClassFields] {
164165
let result = Metadata(T1.self).forEachField(options: options) { name, offset, type in
165166
if offset == 16 {
166167
#expect(type is Int.Type)
@@ -176,7 +177,7 @@ struct MetadataCompatibilityTests {
176177
}
177178
#expect(result == true)
178179
}
179-
for options in [OGTypeApplyOptions.continueAfterUnknownField, .enumerateEnumCases, []] {
180+
for options in [Metadata.ApplyOptions.continueAfterUnknownField, .enumerateEnumCases, []] {
180181
let result = Metadata(T1.self).forEachField(options: options) { name, offset, type in
181182
if offset == 16 {
182183
#expect(type is Int.Type)
@@ -192,7 +193,7 @@ struct MetadataCompatibilityTests {
192193
}
193194
#expect(result == false)
194195
}
195-
for options in [OGTypeApplyOptions.continueAfterUnknownField, []] {
196+
for options in [Metadata.ApplyOptions.continueAfterUnknownField, []] {
196197
let result = Metadata(T2.self).forEachField(options: options) { name, offset, type in
197198
if offset == 0 {
198199
#expect(type is Int.Type)
@@ -206,7 +207,7 @@ struct MetadataCompatibilityTests {
206207
}
207208
#expect(result == true)
208209
}
209-
for options in [OGTypeApplyOptions.enumerateClassFields, .enumerateEnumCases] {
210+
for options in [Metadata.ApplyOptions.enumerateClassFields, .enumerateEnumCases] {
210211
let result = Metadata(T2.self).forEachField(options: options) { name, offset, type in
211212
if offset == 0 {
212213
#expect(type is Int.Type)
@@ -222,11 +223,52 @@ struct MetadataCompatibilityTests {
222223
}
223224
#expect(result == false)
224225
}
225-
for options in [OGTypeApplyOptions.enumerateClassFields, .continueAfterUnknownField, .enumerateEnumCases, []] {
226+
for options in [Metadata.ApplyOptions.enumerateClassFields, .continueAfterUnknownField, .enumerateEnumCases, []] {
226227
let result = Metadata(T3.self).forEachField(options: options) { _, _, _ in
227228
true
228229
}
229230
#expect(result == false)
230231
}
231232
}
233+
234+
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
235+
func globalForEachField() async throws {
236+
forEachField(of: T1.self) { _, _, _ in
237+
Issue.record()
238+
}
239+
await confirmation(expectedCount: 2) { confirmation in
240+
var count = 0
241+
forEachField(of: T2.self) { ptr, offset, type in
242+
let name = String(cString: ptr)
243+
if count == 0 {
244+
#expect(name == "a")
245+
#expect(offset == 0x0)
246+
#expect(type == Int.self)
247+
} else if count == 1 {
248+
#expect(name == "b")
249+
#expect(offset == 0x8)
250+
#expect(type == Double.self)
251+
} else {
252+
Issue.record()
253+
}
254+
count += 1
255+
confirmation()
256+
}
257+
#expect(count == 2)
258+
}
259+
forEachField(of: T3.self) { _, _, _ in
260+
Issue.record()
261+
}
262+
forEachField(of: T4.self) { _, _, _ in
263+
Issue.record()
264+
}
265+
}
266+
267+
#if OPENGRAPH_SUPPORT_2024_API
268+
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
269+
func signature() {
270+
#expect(Metadata(T1.self).signature != Metadata(T2.self).signature)
271+
#expect(Metadata(T1.self).signature == Metadata(T1.self).signature)
272+
}
273+
#endif
232274
}

0 commit comments

Comments
 (0)