Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 33 additions & 27 deletions Sources/OpenGraph/Runtime/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,65 @@

public import OpenGraphCxx
#if canImport(ObjectiveC)
public import Foundation
import Foundation
#endif

@_silgen_name("OGTypeApplyFields")
public func OGTypeApplyFields(
_ type: Any.Type,
private func OGTypeApplyFields(
of type: Any.Type,
body: (UnsafePointer<Int8>, Int, Any.Type) -> Void
)

@_silgen_name("OGTypeApplyFields2")
public func OGTypeApplyFields2(
_ type: Any.Type,
options: OGTypeApplyOptions,
private func OGTypeApplyFields2(
of type: Any.Type,
options: Metadata.ApplyOptions,
body: (UnsafePointer<Int8>, Int, Any.Type) -> Bool
) -> Bool

public func forEachField(
of type: Any.Type,
do body: (UnsafePointer<Int8>, Int, Any.Type) -> Void
) {
OGTypeApplyFields(of: type, body: body)
}

extension Metadata: Swift.Hashable, Swift.CustomStringConvertible {
@inlinable
@inline(__always)
public init(_ type: any Any.Type) {
self.init(rawValue: unsafeBitCast(type, to: UnsafePointer<_Metadata>.self))
}

@inlinable
@inline(__always)

public var type: any Any.Type {
unsafeBitCast(rawValue, to: Any.Type.self)
}

@inlinable
@inline(__always)

public var description: String {
#if canImport(ObjectiveC)
__OGTypeDescription(self) as NSString as String
#else
fatalError("Unimplemented")
#endif
}

@inlinable
@inline(__always)
/* public */func forEachField(
do body: (UnsafePointer<Int8>, Int, Any.Type) -> Void
) {
OGTypeApplyFields(type, body: body)
}

@inlinable
@inline(__always)

public func forEachField(
options: OGTypeApplyOptions,
options: ApplyOptions,
do body: (UnsafePointer<Int8>, Int, Any.Type) -> Bool
) -> Bool {
OGTypeApplyFields2(type, options: options, body: body)
OGTypeApplyFields2(of: type, options: options, body: body)
}
}

extension Signature: Swift.Equatable {
public static func == (_ lhs: Signature, _ rhs: Signature) -> Bool {
return lhs.bytes.0 == rhs.bytes.0 && lhs.bytes.1 == rhs.bytes.1
&& lhs.bytes.2 == rhs.bytes.2 && lhs.bytes.3 == rhs.bytes.3
&& lhs.bytes.4 == rhs.bytes.4 && lhs.bytes.5 == rhs.bytes.5
&& lhs.bytes.6 == rhs.bytes.6 && lhs.bytes.7 == rhs.bytes.7
&& lhs.bytes.8 == rhs.bytes.8 && lhs.bytes.9 == rhs.bytes.9
&& lhs.bytes.10 == rhs.bytes.10 && lhs.bytes.11 == rhs.bytes.11
&& lhs.bytes.12 == rhs.bytes.12 && lhs.bytes.13 == rhs.bytes.13
&& lhs.bytes.14 == rhs.bytes.14 && lhs.bytes.15 == rhs.bytes.15
&& lhs.bytes.16 == rhs.bytes.16 && lhs.bytes.17 == rhs.bytes.17
&& lhs.bytes.18 == rhs.bytes.18 && lhs.bytes.19 == rhs.bytes.19
}
}
2 changes: 1 addition & 1 deletion Sources/OpenGraphCxx/include/OpenGraph/OGTypeID.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef OG_OPTIONS(uint32_t, OGTypeApplyOptions) {
OGTypeApplyOptionsEnumerateClassFields = 1 << 0,
OGTypeApplyOptionsContinueAfterUnknownField = 1 << 1,
OGTypeApplyOptionsEnumerateEnumCases = 1 << 2,
};
} OG_SWIFT_NAME(Metadata.ApplyOptions);

#if OPENGRAPH_RELEASE >= OPENGRAPH_RELEASE_2024

Expand Down
1 change: 0 additions & 1 deletion Sources/OpenGraphShims/GraphShims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public typealias OGAttributeInfo = AGAttributeInfo
public typealias OGCachedValueOptions = AGCachedValueOptions
public typealias OGChangedValueFlags = AGChangedValueFlags
public typealias OGInputOptions = AGInputOptions
public typealias OGTypeApplyOptions = AGTypeApplyOptions
public typealias OGUniqueID = AGUniqueID
public typealias OGValue = AGValue
public typealias OGValueOptions = AGValueOptions
Expand Down
Empty file.
1 change: 0 additions & 1 deletion Tests/OpenGraphCompatibilityTests/GraphShims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public typealias OGAttributeInfo = AGAttributeInfo
public typealias OGCachedValueOptions = AGCachedValueOptions
public typealias OGChangedValueFlags = AGChangedValueFlags
public typealias OGInputOptions = AGInputOptions
public typealias OGTypeApplyOptions = AGTypeApplyOptions
public typealias OGUniqueID = AGUniqueID
public typealias OGValue = AGValue
public typealias OGValueOptions = AGValueOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ struct MetadataCompatibilityTests {
#expect(Metadata(T2.self).description == "MetadataCompatibilityTests.T2")
#expect(Metadata(T3.self).description == "MetadataCompatibilityTests.T3")
}


@available(*, deprecated, message: "To be removed")
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
func forEachField() throws {
for options in [OGTypeApplyOptions.enumerateClassFields] {
func legacyForEachField() throws {
for options in [Metadata.ApplyOptions.enumerateClassFields] {
let result = Metadata(T1.self).forEachField(options: options) { name, offset, type in
if offset == 16 {
#expect(type is Int.Type)
Expand All @@ -176,7 +177,7 @@ struct MetadataCompatibilityTests {
}
#expect(result == true)
}
for options in [OGTypeApplyOptions.continueAfterUnknownField, .enumerateEnumCases, []] {
for options in [Metadata.ApplyOptions.continueAfterUnknownField, .enumerateEnumCases, []] {
let result = Metadata(T1.self).forEachField(options: options) { name, offset, type in
if offset == 16 {
#expect(type is Int.Type)
Expand All @@ -192,7 +193,7 @@ struct MetadataCompatibilityTests {
}
#expect(result == false)
}
for options in [OGTypeApplyOptions.continueAfterUnknownField, []] {
for options in [Metadata.ApplyOptions.continueAfterUnknownField, []] {
let result = Metadata(T2.self).forEachField(options: options) { name, offset, type in
if offset == 0 {
#expect(type is Int.Type)
Expand All @@ -206,7 +207,7 @@ struct MetadataCompatibilityTests {
}
#expect(result == true)
}
for options in [OGTypeApplyOptions.enumerateClassFields, .enumerateEnumCases] {
for options in [Metadata.ApplyOptions.enumerateClassFields, .enumerateEnumCases] {
let result = Metadata(T2.self).forEachField(options: options) { name, offset, type in
if offset == 0 {
#expect(type is Int.Type)
Expand All @@ -222,11 +223,52 @@ struct MetadataCompatibilityTests {
}
#expect(result == false)
}
for options in [OGTypeApplyOptions.enumerateClassFields, .continueAfterUnknownField, .enumerateEnumCases, []] {
for options in [Metadata.ApplyOptions.enumerateClassFields, .continueAfterUnknownField, .enumerateEnumCases, []] {
let result = Metadata(T3.self).forEachField(options: options) { _, _, _ in
true
}
#expect(result == false)
}
}

@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
func globalForEachField() async throws {
forEachField(of: T1.self) { _, _, _ in
Issue.record()
}
await confirmation(expectedCount: 2) { confirmation in
var count = 0
forEachField(of: T2.self) { ptr, offset, type in
let name = String(cString: ptr)
if count == 0 {
#expect(name == "a")
#expect(offset == 0x0)
#expect(type == Int.self)
} else if count == 1 {
#expect(name == "b")
#expect(offset == 0x8)
#expect(type == Double.self)
} else {
Issue.record()
}
count += 1
confirmation()
}
#expect(count == 2)
}
forEachField(of: T3.self) { _, _, _ in
Issue.record()
}
forEachField(of: T4.self) { _, _, _ in
Issue.record()
}
}

#if OPENGRAPH_SUPPORT_2024_API
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
func signature() {
#expect(Metadata(T1.self).signature != Metadata(T2.self).signature)
#expect(Metadata(T1.self).signature == Metadata(T1.self).signature)
}
#endif
}