{"id":21095876,"url":"https://github.com/jrsaruo/swiftidentifier","last_synced_at":"2025-03-14T07:13:58.540Z","repository":{"id":52342953,"uuid":"520922344","full_name":"jrsaruo/SwiftIdentifier","owner":"jrsaruo","description":"A lightweight library that helps to define ID types.","archived":false,"fork":false,"pushed_at":"2024-07-08T17:18:40.000Z","size":26,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-21T17:50:14.676Z","etag":null,"topics":["identifiable","identifier","swift","type-safety"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jrsaruo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-03T14:50:13.000Z","updated_at":"2025-02-21T11:12:30.000Z","dependencies_parsed_at":"2024-07-08T21:00:03.635Z","dependency_job_id":"07fc9fd4-9e64-4b24-bcae-06b4352b828b","html_url":"https://github.com/jrsaruo/SwiftIdentifier","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrsaruo%2FSwiftIdentifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrsaruo%2FSwiftIdentifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrsaruo%2FSwiftIdentifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrsaruo%2FSwiftIdentifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jrsaruo","download_url":"https://codeload.github.com/jrsaruo/SwiftIdentifier/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243538134,"owners_count":20307104,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["identifiable","identifier","swift","type-safety"],"created_at":"2024-11-19T22:29:54.337Z","updated_at":"2025-03-14T07:13:58.513Z","avatar_url":"https://github.com/jrsaruo.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftIdentifier\n\nA lightweight library that helps to define ID types.\n\n## Requirements\n\n- Swift 5.9+\n\n## Features\n\n### What problems does SwiftIdentifier solve?\n\nSuppose you have `User` and `Book` types, each of which has an ID of type `Int`:\n\n```swift\nstruct User {\n    let id: Int\n}\n\nstruct Book {\n    let id: Int\n}\n```\n\nand have a `userIDHandler` closure:\n\n```swift\nlet userIDHandler: (Int) -\u003e Void = { ... }\n```\n\nThere are two main problems here.\n\n- `userIDHandler` accepts a Book ID, leading to silent bugs.\n- The argument type of `userIDHandler` does not fully convey intent.\n\n### Solution with SwiftIdentifier\n\nYou can easily define **different ID types for each type**!\n\n```swift\nstruct User {\n    typealias ID = Identifier\u003cSelf, Int\u003e\n    let id: ID\n}\nstruct Book {\n    typealias ID = Identifier\u003cSelf, Int\u003e\n    let id: ID\n}\n\nlet user = User(id: 10)\nlet book = Book(id: 500)\n\n// The argument type become clearer!\nlet userIDHandler: (User.ID) -\u003e Void = { ... }\n\nuserIDHandler(user.id) // OK\nuserIDHandler(book.id) // Compile error!\n```\n\n### Other features\n\n- You can collaborate nicely with `Codable`.\n  \n  ```swift\n  struct User: Codable {\n      typealias ID = Identifier\u003cSelf, Int\u003e\n      let id: ID\n      let name: String\n  }\n  \n  // Compatible JSON\n  \"\"\"\n  {\n      \"id\": 100,\n      \"name\": \"John\"\n  }\n  \"\"\"\n  ```\n\n## Using SwiftIdentifier in your project\n\nTo use the `SwiftIdentifier` library in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file:\n\n```swift\n.package(url: \"https://github.com/jrsaruo/SwiftIdentifier\", from: \"1.1.2\"),\n```\n\nand add `SwiftIdentifier` as a dependency for your target:\n\n```swift\n.target(name: \"\u003ctarget\u003e\", dependencies: [\n    .product(name: \"SwiftIdentifier\", package: \"SwiftIdentifier\"),\n    // other dependencies\n]),\n```\n\nFinally, add `import SwiftIdentifier` in your source code.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrsaruo%2Fswiftidentifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjrsaruo%2Fswiftidentifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrsaruo%2Fswiftidentifier/lists"}