Skip to content

Commit fe06807

Browse files
jseibertpaulofaria
authored andcommitted
Adopt Reflection 0.15.0
1 parent 881e8f9 commit fe06807

25 files changed

+144
-151
lines changed

Sources/GraphQL/Reflection/Advance.swift

100755100644
File mode changed.

Sources/GraphQL/Reflection/Any+Extensions.swift

100755100644
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,32 @@
66
//
77
//
88

9-
public protocol AnyExtensions {}
9+
protocol AnyExtensions {}
1010

11-
public extension AnyExtensions {
11+
extension AnyExtensions {
1212

1313
static func construct(constructor: (Property.Description) throws -> Any) throws -> Any {
14-
return try GraphQL.construct(self, constructor: constructor)
14+
return try GraphQL.constructGenericType(self, constructor: constructor)
1515
}
1616

17-
static func construct(dictionary: [String: Any]) throws -> Any {
18-
return try GraphQL.construct(self, dictionary: dictionary)
17+
static func isValueTypeOrSubtype(_ value: Any) -> Bool {
18+
return value is Self
1919
}
2020

21-
func write(to pointer: UnsafeMutableRawPointer) {
22-
pointer.assumingMemoryBound(to: type(of: self)).initialize(to: self)
21+
static func value(from storage: UnsafeRawPointer) -> Any {
22+
return storage.assumingMemoryBound(to: self).pointee
23+
}
24+
25+
static func write(_ value: Any, to storage: UnsafeMutableRawPointer) throws {
26+
guard let this = value as? Self else {
27+
throw ReflectionError.valueIsNotType(value: value, type: self)
28+
}
29+
storage.assumingMemoryBound(to: self).initialize(to: this)
2330
}
2431

2532
}
2633

27-
public func extensions(of type: Any.Type) -> AnyExtensions.Type {
34+
func extensions(of type: Any.Type) -> AnyExtensions.Type {
2835
struct Extensions : AnyExtensions {}
2936
var extensions: AnyExtensions.Type = Extensions.self
3037
withUnsafePointer(to: &extensions) { pointer in
@@ -33,7 +40,7 @@ public func extensions(of type: Any.Type) -> AnyExtensions.Type {
3340
return extensions
3441
}
3542

36-
public func extensions(of value: Any) -> AnyExtensions {
43+
func extensions(of value: Any) -> AnyExtensions {
3744
struct Extensions : AnyExtensions {}
3845
var extensions: AnyExtensions = Extensions()
3946
withUnsafePointer(to: &extensions) { pointer in

Sources/GraphQL/Reflection/AnyExistentialContainer.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

Sources/GraphQL/Reflection/Array+Extensions.swift

100755100644
File mode changed.

Sources/GraphQL/Reflection/Buffer.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.

Sources/GraphQL/Reflection/Construct.swift

100755100644
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/// Create a struct with a constructor method. Return a value of `property.type` for each property.
22
public func construct<T>(_ type: T.Type = T.self, constructor: (Property.Description) throws -> Any) throws -> T {
3+
return try constructGenericType(constructor: constructor)
4+
}
5+
6+
func constructGenericType<T>(_ type: T.Type = T.self, constructor: (Property.Description) throws -> Any) throws -> T {
37
if Metadata(type: T.self)?.kind == .struct {
48
return try constructValueType(constructor)
59
} else {
@@ -22,22 +26,29 @@ private func constructValueType<T>(_ constructor: (Property.Description) throws
2226
}
2327

2428
private func constructType(storage: UnsafeMutableRawPointer, values: inout [Any], properties: [Property.Description], constructor: (Property.Description) throws -> Any) throws {
29+
var errors = [Error]()
2530
for property in properties {
26-
let value = try constructor(property)
27-
guard GraphQL.value(value, is: property.type) else { throw ReflectionError.valueIsNotType(value: value, type: property.type) }
28-
values.append(value)
29-
extensions(of: value).write(to: storage.advanced(by: property.offset))
31+
do {
32+
let value = try constructor(property)
33+
values.append(value)
34+
try property.write(value, to: storage)
35+
} catch {
36+
errors.append(error)
37+
}
38+
}
39+
if errors.count > 0 {
40+
throw ConstructionErrors(errors: errors)
3041
}
3142
}
3243

3344
/// Create a struct from a dictionary.
3445
public func construct<T>(_ type: T.Type = T.self, dictionary: [String: Any]) throws -> T {
35-
return try construct(constructor: constructorForDictionary(dictionary))
46+
return try constructGenericType(constructor: constructorForDictionary(dictionary))
3647
}
3748

3849
/// Create a struct from a dictionary.
3950
public func construct(_ type: Any.Type, dictionary: [String: Any]) throws -> Any {
40-
return try extensions(of: type).construct(dictionary: dictionary)
51+
return try construct(type, constructor: constructorForDictionary(dictionary))
4152
}
4253

4354
private func constructorForDictionary(_ dictionary: [String: Any]) -> (Property.Description) throws -> Any {

Sources/GraphQL/Reflection/Get.swift

100755100644
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/// Get value for key from instance
22
public func get(_ key: String, from instance: Any) throws -> Any {
3-
for property in try properties(instance) {
4-
if property.key == key {
5-
return property.value
6-
}
3+
guard let value = try properties(instance).first(where: { $0.key == key })?.value else {
4+
throw ReflectionError.instanceHasNoKey(type: type(of: instance), key: key)
75
}
8-
throw ReflectionError.instanceHasNoKey(type: type(of: instance), key: key)
6+
return value
97
}
108

119
/// Get value for key from instance as type `T`

Sources/GraphQL/Reflection/Identity.swift

100755100644
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
/// Tests if `value` is `type` or a subclass of `type`
22
public func value(_ value: Any, is type: Any.Type) -> Bool {
3-
if Swift.type(of: value) == type {
4-
return true
5-
}
6-
guard var subclass = Metadata.Class(type: Swift.type(of: value)), let superclass = Metadata.Class(type: type) else {
7-
return false
8-
}
9-
while let parentClass = subclass.superclass {
10-
if parentClass == superclass {
11-
return true
12-
}
13-
subclass = parentClass
14-
}
15-
return false
3+
return extensions(of: type).isValueTypeOrSubtype(value)
164
}
175

186
/// Tests equality of any two existential types

Sources/GraphQL/Reflection/MemoryProperties.swift

100755100644
File mode changed.

Sources/GraphQL/Reflection/Metadata+Class.swift

100755100644
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ extension Metadata {
1212
guard let superclass = pointer.pointee.superclass else { return nil }
1313
return Metadata.Class(type: superclass)
1414
}
15+
16+
func properties() throws -> [Property.Description] {
17+
let properties = try fetchAndSaveProperties(nominalType: self, hashedType: HashedType(pointer))
18+
guard let superclass = superclass, String(describing: unsafeBitCast(superclass.pointer, to: Any.Type.self)) != "SwiftObject" else {
19+
return properties
20+
}
21+
return try superclass.properties() + properties
22+
}
1523

1624
}
1725
}

0 commit comments

Comments
 (0)