Skip to content

Commit

Permalink
feat: UUID extended to conform to Identifier (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcritz authored and djones6 committed Sep 24, 2019
1 parent 5211127 commit 47d30e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/KituraContracts/Contracts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,23 @@ extension Bool: Identifier {
}
}

extension UUID: Identifier {
/// Creates a UUID identifier from a given string representation.
/// - Throws: An `IdentifierError.invalidValue` if the given string cannot be converted to a UUID.
public init(value: String) throws {
if let id = UUID(uuidString: value) {
self = id
} else {
throw IdentifierError.invalidValue
}
}

/// The string representation of the identifier.
public var value: String {
return self.uuidString
}
}

/**
An enum containing the ordering information
### Usage Example: ###
Expand Down
19 changes: 19 additions & 0 deletions Tests/KituraContractsTests/KituraContractsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ class KituraContractsTests: XCTestCase {
XCTAssertEqual(123456, intIdentifier)
}

// Test UUID conforms to Identifier
func testUUIDIdentifier() {
let uuidStr = "12345678-1234-4000-ABCD-BA9876543210"
guard let identifier: Identifier = try? UUID(value: uuidStr) else {
XCTFail("Failed to create a UUID identifier!")
return
}
XCTAssertEqual(uuidStr, identifier.value)

guard let uuidIdentifier = identifier as? UUID else {
XCTFail("Failed to cast to concrete type: UUID")
return
}
XCTAssertEqual(UUID(uuidString: uuidStr), uuidIdentifier)

let bogusUUIDStr = "NOPE"
XCTAssertThrowsError(try UUID(value: bogusUUIDStr), "Failed to throw with bogus UUID value")
}

// func testTypeComputation() {
// XCTAssertEqual(User.type, "User")
// XCTAssertEqual(User.typeLowerCased, "user")
Expand Down

0 comments on commit 47d30e9

Please sign in to comment.