Skip to content

Swift 5.1 Property Wrapper: Expressed #976

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 4 commits into
base: master
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
21 changes: 21 additions & 0 deletions Sources/SQLite/Typed/Expressed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

#if swift(>=5.1)
/// A type that Expression a property marked with an attribute.
///```swift
///@Expressed("id") var id = UUID().string
///```
///
@propertyWrapper public struct Expressed<Value> where Value: SQLite.Value {
///The property for which this instance exposes a Expression.
public let projectedValue:Expression<Value>

public var wrappedValue: Value
///Creates a publisher with the provided initial value.
public init(wrappedValue: Value, _ key: String, bindings: [Binding?] = []) {
projectedValue = Expression<Value>(key, bindings)
self.wrappedValue = wrappedValue
}
public var setter:Setter { projectedValue <- wrappedValue }
}
#endif
27 changes: 26 additions & 1 deletion Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,32 @@ extension QueryType {
query.expression
]).expression)
}

#if swift(>=5.1)
func insert<V>(_ value: Expressed<V>, _ more: Expressed<V>...) -> Insert where V: Value {

insert([value.setter] + more.map{$0.setter})
}
//
// MARK: INSERT
public func insert<V>(_ values: [Expressed<V>]) -> Insert {
return insert(nil, values)
}

public func insert<V>(or onConflict: OnConflict, _ values: Expressed<V>...) -> Insert {
return insert(or: onConflict, values)
}

public func insert<V>(or onConflict: OnConflict, _ values: [Expressed<V>]) -> Insert {
return insert(onConflict, values)
}

fileprivate func insert<V>(_ or: OnConflict?, _ expresseds: [Expressed<V>]) -> Insert {
let values:[Setter] = expresseds.map{ expressed in
expressed.setter
}
return insert(or, values)
}
#endif
// MARK: UPDATE

public func update(_ values: Setter...) -> Update {
Expand Down
5 changes: 5 additions & 0 deletions Tests/SQLiteTests/ExpressedTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import XCTest
import SQLite

class ExpressedTests : XCTestCase {
}