Skip to content

Add StructuredQueriesTagged package trait #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "c6d1e6c49f622359647bcd073a92db28e1dad8d549034617c7f827b702813be4",
"originHash" : "0609fed701a35e2ee3efbd46f681ff0361d000a8941571e234eab9bbe10f0643",
"pins" : [
{
"identity" : "combine-schedulers",
Expand Down Expand Up @@ -91,6 +91,15 @@
"version" : "600.0.1"
}
},
{
"identity" : "swift-tagged",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-tagged",
"state" : {
"revision" : "3907a9438f5b57d317001dc99f3f11b46882272b",
"version" : "0.10.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
Expand Down
15 changes: 14 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 6.0
// swift-tools-version: 6.1

import CompilerPluginSupport
import PackageDescription
Expand Down Expand Up @@ -29,11 +29,19 @@ let package = Package(
targets: ["StructuredQueriesSQLite"]
),
],
traits: [
.trait(
name: "StructuredQueriesTagged",
description: "Introduce StructuredQueries conformances to the swift-tagged package.",
enabledTraits: []
)
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.3"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.8.1"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.6.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.1"),
.package(url: "https://github.com/pointfreeco/swift-tagged", from: "0.10.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.5.2"),
.package(url: "https://github.com/swiftlang/swift-syntax", "600.0.0"..<"601.0.0"),
],
Expand All @@ -43,6 +51,11 @@ let package = Package(
dependencies: [
"StructuredQueriesSupport",
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
.product(
name: "Tagged",
package: "swift-tagged",
condition: .when(traits: ["StructuredQueriesTagged"])
),
]
),
.target(
Expand Down
137 changes: 137 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// swift-tools-version: 6.0

import CompilerPluginSupport
import PackageDescription

let package = Package(
name: "swift-structured-queries",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6),
],
products: [
.library(
name: "StructuredQueries",
targets: ["StructuredQueries"]
),
.library(
name: "StructuredQueriesCore",
targets: ["StructuredQueriesCore"]
),
.library(
name: "StructuredQueriesTestSupport",
targets: ["StructuredQueriesTestSupport"]
),
.library(
name: "_StructuredQueriesSQLite",
targets: ["StructuredQueriesSQLite"]
),
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.3"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.8.1"),
.package(url: "https://github.com/pointfreeco/swift-macro-testing", from: "0.6.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.1"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.5.2"),
.package(url: "https://github.com/swiftlang/swift-syntax", "600.0.0"..<"601.0.0"),
],
targets: [
.target(
name: "StructuredQueriesCore",
dependencies: [
"StructuredQueriesSupport",
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
]
),
.target(
name: "StructuredQueries",
dependencies: [
"StructuredQueriesCore",
"StructuredQueriesMacros",
]
),
.macro(
name: "StructuredQueriesMacros",
dependencies: [
"StructuredQueriesSupport",
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
]
),
.target(
name: "StructuredQueriesSQLite",
dependencies: [
"StructuredQueries"
]
),
.target(
name: "StructuredQueriesTestSupport",
dependencies: [
"StructuredQueriesCore",
.product(name: "CustomDump", package: "swift-custom-dump"),
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
]
),
.testTarget(
name: "StructuredQueriesMacrosTests",
dependencies: [
"StructuredQueries",
"StructuredQueriesMacros",
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
.product(name: "MacroTesting", package: "swift-macro-testing"),
]
),
.testTarget(
name: "StructuredQueriesTests",
dependencies: [
"StructuredQueries",
"StructuredQueriesSQLite",
"StructuredQueriesTestSupport",
.product(name: "CustomDump", package: "swift-custom-dump"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
]
),

.target(name: "StructuredQueriesSupport"),
],
swiftLanguageModes: [.v6]
)

let swiftSettings: [SwiftSetting] = [
.enableUpcomingFeature("MemberImportVisibility")
// .unsafeFlags([
// "-Xfrontend",
// "-warn-long-function-bodies=50",
// "-Xfrontend",
// "-warn-long-expression-type-checking=50",
// ])
]

for index in package.targets.indices {
package.targets[index].swiftSettings = swiftSettings
}

#if !os(Darwin)
package.targets.append(
.systemLibrary(
name: "StructuredQueriesSQLite3",
providers: [.apt(["libsqlite3-dev"])]
)
)

for index in package.targets.indices {
if package.targets[index].name == "StructuredQueriesSQLite" {
package.targets[index].dependencies.append("StructuredQueriesSQLite3")
}
}
#endif

#if !os(Windows)
// Add the documentation compiler plugin if possible
package.dependencies.append(
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")
)
#endif
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ it's as simple as adding it to your `Package.swift`:

``` swift
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-structured-queries", from: "0.1.0"),
.package(
url: "https://github.com/pointfreeco/swift-structured-queries",
from: "0.1.0"
),
]
```

Expand All @@ -231,6 +234,23 @@ And then adding the product to any target that needs access to the library:
.product(name: "StructuredQueries", package: "swift-structured-queries"),
```

If you are on Swift 6.1 or greater, you can enable package traits that extend the library with
support for other packages. For example, you can introduce type-safe identifiers to your tables
_via_ [Tagged](https://github.com/pointfreeco/swift-tagged) by enabling the
`StructuredQueriesTagged` trait:

```diff
dependencies: [
.package(
url: "https://github.com/pointfreeco/swift-structured-queries",
from: "0.1.0",
+ traits: [
+ "StructuredQueriesTagged",
+ ]
),
]
```

## Community

If you want to discuss this library or have a question about how to use it to solve a particular
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,51 @@ With that you can insert reminders with notes like so:
}
}

#### Tagged identifiers

The [Tagged](https://github.com/pointfreeco/swift-tagged) library provides lightweight syntax for
introducing type-safe identifiers (and more) to your models. StructuredQueries ships support for
Tagged with a `StructuredQueriesTagged` package trait, which is available starting from Swift 6.1.

To enable the trait, specify it in the Package.swift file that depends on StructuredQueries:

```diff
.package(
url: "https://github.com/pointfreeco/swift-structured-queries",
from: "0.2.0",
+ traits: ["StructuredQueriesTagged"]
),
```

This will allow you to introduce distinct `Tagged` identifiers throughout your schema:

```diff
@Table
struct RemindersList: Identifiable {
- let id: Int
+ typealias ID = Tagged<Self, Int>
+ let id: ID
// ...
}
@Table
struct Reminder: Identifiable {
- let id: Int
+ typealias ID = Tagged<Self, Int>
+ let id: ID
// ...
var remindersList: Reminder.ID
}
```

This adds a new layer of type-safety when constructing queries. Previously comparing a
`RemindersList.ID` to a `Reminder.ID` would compile just fine, even though it is a nonsensical thing
to do. But now, such a comparison is a compile time error:

```
RemindersList.leftJoin(Reminder.all) {
$0.id == $1.id // 🛑 Requires the types 'Reminder.ID' and 'RemindersList.ID' be equivalent
}

### Primary keyed tables

It is possible to tell let the `@Table` macro know which property of your data type is the primary
Expand Down
33 changes: 33 additions & 0 deletions Sources/StructuredQueriesCore/Traits/Tagged.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#if StructuredQueriesTagged
import Tagged

extension Tagged: _OptionalPromotable where RawValue: _OptionalPromotable {}

extension Tagged: QueryBindable where RawValue: QueryBindable {}

extension Tagged: QueryDecodable where RawValue: QueryDecodable {}

extension Tagged: QueryExpression where RawValue: QueryExpression {
public var queryFragment: QueryFragment {
rawValue.queryFragment
}
}

extension Tagged: QueryRepresentable where RawValue: QueryRepresentable {
public typealias QueryOutput = Tagged<Tag, RawValue.QueryOutput>

public var queryOutput: QueryOutput {
QueryOutput(rawValue: self.rawValue.queryOutput)
}

public init(queryOutput: QueryOutput) {
self.init(rawValue: RawValue(queryOutput: queryOutput.rawValue))
}
}

extension Tagged: SQLiteType where RawValue: SQLiteType {
public static var typeAffinity: SQLiteTypeAffinity {
RawValue.typeAffinity
}
}
#endif