Swiftpack.co - jrsaruo/SwiftIdentifier as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by jrsaruo.
jrsaruo/SwiftIdentifier 1.0.0
A lightweight library that helps to define ID types.
⭐️ 1
🕓 1 week ago
.package(url: "https://github.com/jrsaruo/SwiftIdentifier.git", from: "1.0.0")

SwiftIdentifier

A lightweight library that helps to define ID types.

Requirements

  • Swift 5.4+

Features

What problems does SwiftIdentifier solve?

Suppose you have User and Book types, each of which has an ID of type Int:

struct User {
    let id: Int
}

struct Book {
    let id: Int
}

and have a userIDHandler closure:

let userIDHandler: (Int) -> Void = { ... }

There are two main problems here.

  • userIDHandler accepts a Book ID, leading to silent bugs.
  • The argument type of userIDHandler does not fully convey intent.

Solution with SwiftIdentifier

You can easily define different ID types for each type!

struct User {
    typealias ID = Identifier<Self, Int>
    let id: ID
}
struct Book {
    typealias ID = Identifier<Self, Int>
    let id: ID
}

let user = User(id: 10)
let book = Book(id: 500)

// The argument type become clearer!
let userIDHandler: (User.ID) -> Void = { ... }

userIDHandler(user.id) // OK
userIDHandler(book.id) // Compile error!

Other features

  • You can collaborate nicely with Codable.

    struct User: Codable {
        typealias ID = Identifier<Self, Int>
        let id: ID
        let name: String
    }
    
    // Compatible JSON
    """
    {
        "id": 100,
        "name": "John"
    }
    """
    

Using SwiftIdentifier in your project

To use the SwiftIdentifier library in a SwiftPM project, add the following line to the dependencies in your Package.swift file:

.package(url: "https://github.com/jrsaruo/SwiftIdentifier", from: "1.0.0"),

and add SwiftIdentifier as a dependency for your target:

.target(name: "<target>", dependencies: [
    .product(name: "SwiftIdentifier", package: "SwiftIdentifier"),
    // other dependencies
]),

Finally, add import SwiftIdentifier in your source code.

GitHub

link
Stars: 1
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Release 1.0.0
1 week ago

What's Changed

🎉 New Features

🧰 Maintenance

Other Changes

New Contributors

Full Changelog: https://github.com/jrsaruo/SwiftIdentifier/commits/1.0.0

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics