-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from uditha-atukorala/feature/codable
Codable protocol implementation for Ids
- Loading branch information
Showing
9 changed files
with
333 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum XidError: Error, Equatable { | ||
case decodeValidationFailure | ||
case invalidId | ||
case invalidIdStringLength(have: Int, want: Int) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,27 @@ | ||
import Foundation | ||
|
||
private var xid = Xid() | ||
|
||
public func NewXid() -> Id { | ||
xid.next() | ||
} | ||
|
||
public func NewXid() -> String { | ||
String(describing: xid.next()) | ||
} | ||
|
||
public func NewXid(bytes: Data) throws -> Id { | ||
if bytes.count != 12 { | ||
throw XidError.invalidId | ||
} | ||
|
||
return Id(bytes: bytes) | ||
} | ||
|
||
public func NewXid(from: Data) throws -> Id { | ||
try Id(from: from) | ||
} | ||
|
||
public func NewXid(from: String) throws -> Id { | ||
try Id(from: from) | ||
} |
Oops, something went wrong.