Skip to content

Commit

Permalink
Fixing uuid special case
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Mar 20, 2020
1 parent 12f9603 commit 36d09e1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
12 changes: 8 additions & 4 deletions Sources/LeoQL/Utils/Method Dispatching/RegisterUsage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,11 @@ func resolveArguments(for value: Any, using type: Any.Type, isNil: Bool = false)
}
if type == UUID.self {
if isNil {
return [.int(.int(0)), .int(.int(0))]
return [.int(.int(0))]
}
let bytes = withUnsafeBytes(of: value as! UUID) { $0.bindMemory(to: Int.self) }
return Array(bytes).map { .int(.int($0)) }
let pointer = UnsafeMutableRawPointer.allocate(byteCount: MemoryLayout<UUID>.size, alignment: MemoryLayout<UUID>.size)
pointer.storeBytes(of: value as! UUID, as: UUID.self)
return [.int(.pointer(pointer))]
}

let info = try typeInfo(of: type)
Expand Down Expand Up @@ -232,7 +233,7 @@ func resolveResults(for type: Any.Type, pointer: UnsafeMutableRawPointer) throws
}

// Special cases
if type == String.self || type == UUID.self {
if type == String.self {
return (
[
.int(.int(pointer.assumingMemoryBound(to: Int.self))),
Expand All @@ -241,6 +242,9 @@ func resolveResults(for type: Any.Type, pointer: UnsafeMutableRawPointer) throws
MemoryLayout<String>.size
)
}
if type == UUID.self {
return ([.int(.pointer(pointer.assumingMemoryBound(to: UnsafeMutableRawPointer.self)))], MemoryLayout<UnsafeMutableRawPointer>.size)
}

let info = try typeInfo(of: type)

Expand Down
12 changes: 8 additions & 4 deletions Sources/LeoQL/Utils/Method Dispatching/RegisterUsage.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ func resolveArguments(for value: Any, using type: Any.Type, isNil: Bool = false)
}
if type == UUID.self {
if isNil {
return [.int(.int(0)), .int(.int(0))]
return [.int(.int(0))]
}
let bytes = withUnsafeBytes(of: value as! UUID) { $0.bindMemory(to: Int.self) }
return Array(bytes).map { .int(.int($0)) }
let pointer = UnsafeMutableRawPointer.allocate(byteCount: MemoryLayout<UUID>.size, alignment: MemoryLayout<UUID>.size)
pointer.storeBytes(of: value as! UUID, as: UUID.self)
return [.int(.pointer(pointer))]
}

let info = try typeInfo(of: type)
Expand Down Expand Up @@ -183,7 +184,7 @@ func resolveResults(for type: Any.Type, pointer: UnsafeMutableRawPointer) throws
% end

// Special cases
if type == String.self || type == UUID.self {
if type == String.self {
return (
[
.int(.int(pointer.assumingMemoryBound(to: Int.self))),
Expand All @@ -192,6 +193,9 @@ func resolveResults(for type: Any.Type, pointer: UnsafeMutableRawPointer) throws
MemoryLayout<String>.size
)
}
if type == UUID.self {
return ([.int(.pointer(pointer.assumingMemoryBound(to: UnsafeMutableRawPointer.self)))], MemoryLayout<UnsafeMutableRawPointer>.size)
}

let info = try typeInfo(of: type)

Expand Down
14 changes: 12 additions & 2 deletions Sources/LeoQLTests/CallingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ class CallingTests: XCTestCase {
]
}

func testUUID() throws {
let info = try typeInfo(of: MyClass.self)
let instance = MyClass()
let method = info.methods.first { $0.methodName == "uuid" }!
let uuid = UUID()
let arguments = [uuid] as [Any]
let string = try method.call(receiver: instance, arguments: arguments) as! String
XCTAssertEqual(string, uuid.uuidString)
}

func testArray() throws {
let info = try typeInfo(of: MyClass.self)
let instance = MyClass()
Expand Down Expand Up @@ -59,8 +69,8 @@ enum MyEnum: String, CaseIterable, GraphQLEnum {
}

class MyClass: GraphQLObject {
func optionals(string: String?) -> String? {
return string.map { "hello, \($0)" }
func uuid(uuid: UUID) -> String {
return uuid.uuidString
}

func doit(cases: [MyEnum] = MyEnum.mostCases) -> String {
Expand Down

0 comments on commit 36d09e1

Please sign in to comment.