Skip to content

Commit 295d975

Browse files
committed
Swift 5: public public, index(of:) => firstIndex(of:)
Seems to compile fine now.
1 parent 2983e91 commit 295d975

24 files changed

Lines changed: 205 additions & 164 deletions

Sources/ZeeQL/Access/ActiveRecord.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ public protocol DatabaseBoundObject {
4141

4242
public extension ActiveRecordType { // default imp
4343

44-
public func value(forKey k: String) -> Any? {
44+
func value(forKey k: String) -> Any? {
4545
// first check extra properties
46-
if let v = values[k] {
47-
return v
48-
}
46+
if let v = values[k] { return v }
4947

5048
// then fallback to KVC
5149
if let v = KeyValueCoding.defaultValue(forKey: k, inObject: self) {
@@ -58,7 +56,7 @@ public extension ActiveRecordType { // default imp
5856

5957
// MARK: - Convenience Subscripts
6058

61-
public subscript(key: String) -> Any? {
59+
subscript(key: String) -> Any? {
6260
set {
6361
do {
6462
if let v = newValue { // hm, necessary?
@@ -78,12 +76,12 @@ public extension ActiveRecordType { // default imp
7876
}
7977
}
8078

81-
public subscript(int key: String) -> Int? {
79+
subscript(int key: String) -> Int? {
8280
guard let v = self[key] else { return nil }
8381
if let i = v as? Int { return i }
8482
return Int("\(v)")
8583
}
86-
public subscript(string key: String) -> String? {
84+
subscript(string key: String) -> String? {
8785
guard let v = self[key] else { return nil }
8886
if let i = v as? String { return i }
8987
return "\(v)"
@@ -272,12 +270,15 @@ open class ActiveRecord : ActiveRecordType, SmartDescription {
272270
}
273271
}
274272
public func removeObject(_ o: AnyObject, fromPropertyWithKey key: String) {
275-
if var list = values[key] as? [ AnyObject ] {
276-
if let idx = list.index(where: { $0 === o }) {
277-
list.remove(at: idx)
278-
values[key] = [ list ]
279-
}
280-
}
273+
guard var list = values[key] as? [ AnyObject ] else { return }
274+
#if swift(>=5)
275+
guard let idx = list.firstIndex(where: { $0 === o }) else { return }
276+
#else
277+
guard let idx = list.index(where: { $0 === o }) else { return }
278+
#endif
279+
280+
list.remove(at: idx)
281+
values[key] = [ list ]
281282
}
282283

283284

Sources/ZeeQL/Access/Adaptor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public extension Adaptor {
9292

9393
public extension Adaptor { // AdaptorQueryType
9494

95-
public func querySQL(_ sql: String, _ optAttrs : [ Attribute ]? = nil,
96-
cb: ( AdaptorRecord ) throws -> Void) throws
95+
func querySQL(_ sql: String, _ optAttrs : [ Attribute ]? = nil,
96+
cb: ( AdaptorRecord ) throws -> Void) throws
9797
{
9898
let ch = try openChannelFromPool()
9999
defer { releaseChannel(ch) }
@@ -102,7 +102,7 @@ public extension Adaptor { // AdaptorQueryType
102102
}
103103

104104
@discardableResult
105-
public func performSQL(_ sql: String) throws -> Int {
105+
func performSQL(_ sql: String) throws -> Int {
106106
let ch = try openChannelFromPool()
107107
defer { releaseChannel(ch) }
108108

Sources/ZeeQL/Access/AdaptorChannel.swift

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ public extension AdaptorChannel {
301301
* - qualifier: the qualifier used to select the rows to be locked
302302
* - snapshot: a set of keys/values specifying a row to be locked
303303
*/
304-
public func lockRowComparingAttributes(_ attrs : [ Attribute ]?,
305-
_ entity : Entity,
306-
_ qualifier : Qualifier?,
307-
_ snapshot : AdaptorRow?) throws
304+
func lockRowComparingAttributes(_ attrs : [ Attribute ]?,
305+
_ entity : Entity,
306+
_ qualifier : Qualifier?,
307+
_ snapshot : AdaptorRow?) throws
308308
-> Bool
309309
{
310310
let q = snapshot != nil ? qualifierToMatchAllValues(snapshot!) : nil
@@ -339,11 +339,11 @@ public extension AdaptorChannel {
339339
* - lock: whether the SELECT should include a HOLD LOCK
340340
* - e: the entity (usually the table) to be fetched
341341
*/
342-
public func selectAttributes(_ attrs : [ Attribute ]?,
343-
_ fs : FetchSpecification?,
344-
lock : Bool,
345-
_ e : Entity?,
346-
result : ( AdaptorRecord ) throws -> Void) throws
342+
func selectAttributes(_ attrs : [ Attribute ]?,
343+
_ fs : FetchSpecification?,
344+
lock : Bool,
345+
_ e : Entity?,
346+
result : ( AdaptorRecord ) throws -> Void) throws
347347
{
348348
/* This is called by the DatabaseChannel
349349
* selectObjectsWithFetchSpecification(fs)
@@ -444,7 +444,6 @@ public extension AdaptorChannel {
444444
* - entity: the entity which should be updated
445445
* - returns: number of affected rows or -1 on error
446446
*/
447-
public
448447
func updateValuesInRowsDescribedByQualifier(_ values : [ String: Any? ],
449448
_ qualifier : Qualifier,
450449
_ entity : Entity) throws
@@ -455,8 +454,8 @@ public extension AdaptorChannel {
455454
return try evaluateUpdateExpression(expr)
456455
}
457456

458-
public func deleteRowsDescribedByQualifier(_ q: Qualifier, _ e: Entity) throws
459-
-> Int
457+
func deleteRowsDescribedByQualifier(_ q: Qualifier, _ e: Entity) throws
458+
-> Int
460459
{
461460
let expr = expressionFactory.deleteStatementWithQualifier(q, e)
462461
return try evaluateUpdateExpression(expr)
@@ -471,9 +470,8 @@ public extension AdaptorChannel {
471470
* - entity: the entity which contains the row
472471
* - returns: true if exactly one row was deleted, false otherwise
473472
*/
474-
public func deleteRowDescribedByQualifier(_ qualifier : Qualifier,
475-
_ entity : Entity) throws
476-
-> Bool
473+
func deleteRowDescribedByQualifier(_ qualifier: Qualifier, _ entity: Entity)
474+
throws -> Bool
477475
{
478476
return try deleteRowsDescribedByQualifier(qualifier, entity) == 1
479477
}
@@ -495,16 +493,14 @@ public extension AdaptorChannel {
495493
* entity are being refetched. Requires the entity!
496494
* - returns: the record, potentially refetched and updated
497495
*/
498-
public func insertRow(_ row: AdaptorRow, _ entity: Entity?,
499-
refetchAll: Bool)
500-
throws -> AdaptorRow
496+
func insertRow(_ row: AdaptorRow, _ entity: Entity?, refetchAll: Bool)
497+
throws -> AdaptorRow
501498
{
502499
return try defaultInsertRow(row, entity, refetchAll: refetchAll)
503500
}
504501

505-
public func defaultInsertRow(_ row: AdaptorRow, _ entity: Entity?,
506-
refetchAll: Bool)
507-
throws -> AdaptorRow
502+
func defaultInsertRow(_ row: AdaptorRow, _ entity: Entity?, refetchAll: Bool)
503+
throws -> AdaptorRow
508504
{
509505
// So that we can reuse the default implementation ...
510506
if refetchAll && entity == nil {
@@ -545,8 +541,8 @@ public extension AdaptorChannel {
545541
* - entity: optionally an entity representing the table
546542
* - returns: the record, potentially refetched and updated
547543
*/
548-
public func insertRow(_ row: AdaptorRow, _ entity: Entity? = nil) throws
549-
-> AdaptorRow
544+
func insertRow(_ row: AdaptorRow, _ entity: Entity? = nil)
545+
throws -> AdaptorRow
550546
{
551547
return try insertRow(row, entity, refetchAll: entity != nil)
552548
}

Sources/ZeeQL/Access/AdaptorModelFetch.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public extension AdaptorModelFetch { // default imp
4343

4444
// MARK: - Model fetch
4545

46-
public func fetchModel() throws -> Model {
46+
func fetchModel() throws -> Model {
4747
// simple version first. We may be able to do better :-)
4848
// TODO: include in Entity whether it was a view
4949
// Note: Looks like we cannot UNION PRAGMA table_info statements.
@@ -72,9 +72,7 @@ public extension AdaptorModelFetch { // default imp
7272
* Check whether the schema changed since the tag which is being passed in.
7373
* If it didn't change, return nil, if it did change, return the new tag.
7474
*/
75-
public func didChangeSinceModelTag(_ tag: ModelTag?) throws
76-
-> ModelTag?
77-
{
75+
func didChangeSinceModelTag(_ tag: ModelTag?) throws -> ModelTag? {
7876
// the idea behind this is that sometimes there may be a faster way to
7977
// compare partial tags.
8078
guard let tag = tag else { return try fetchModelTag() }
@@ -89,8 +87,8 @@ public extension AdaptorModelFetch { // default imp
8987
return try describeModelWithTableNames(names, tagged: true)
9088
}
9189

92-
public func describeModelWithTableNames(_ names : [ String ],
93-
tagged : Bool = true) throws -> Model
90+
func describeModelWithTableNames(_ names : [ String ],
91+
tagged : Bool = true) throws -> Model
9492
{
9593
let didOpenTX = !channel.isTransactionInProgress
9694
if didOpenTX { try channel.begin() }

Sources/ZeeQL/Access/AdaptorQueryType.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public extension AdaptorQueryType {
4545
*
4646
* TODO: document use of attributes.
4747
*/
48-
public func querySQL(_ sql: String, _ optAttrs: [ Attribute ]?) throws
49-
-> [ AdaptorRecord ]
48+
func querySQL(_ sql: String, _ optAttrs: [ Attribute ]?)
49+
throws -> [ AdaptorRecord ]
5050
{
5151
var results = [ AdaptorRecord ]()
5252
try querySQL(sql, optAttrs) { results.append($0) }
@@ -56,7 +56,7 @@ public extension AdaptorQueryType {
5656
/**
5757
* Execute the raw SQL and return the results as an array of `AdaptorRecord`s.
5858
*/
59-
public func querySQL(_ sql: String) throws -> [ AdaptorRecord ] {
59+
func querySQL(_ sql: String) throws -> [ AdaptorRecord ] {
6060
var results = [ AdaptorRecord ]()
6161
try querySQL(sql, nil) { results.append($0) }
6262
return results
@@ -65,9 +65,7 @@ public extension AdaptorQueryType {
6565
/**
6666
* Execute the raw SQL and call the result callback for every record returned.
6767
*/
68-
public func querySQL(_ sql: String, cb: ( AdaptorRecord ) throws -> Void)
69-
throws
70-
{
68+
func querySQL(_ sql: String, cb: ( AdaptorRecord ) throws -> Void) throws {
7169
try querySQL(sql, nil, cb: cb)
7270
}
7371
}

Sources/ZeeQL/Access/AdaptorRecord.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ open class AdaptorRecord : SwiftObject, SmartDescription {
2020
public var isEmpty : Bool { return values.isEmpty }
2121

2222
public subscript(name: String) -> Any? {
23-
guard let idx = schema.attributeNames.index(of: name) else { return nil }
23+
#if swift(>=5)
24+
guard let idx = schema.attributeNames.firstIndex(of: name) else {
25+
return nil
26+
}
27+
#else
28+
guard let idx = schema.attributeNames.index(of: name) else { return nil }
29+
#endif
2430
guard idx < values.count else { return nil }
2531
return values[idx]
2632
}

Sources/ZeeQL/Access/AdaptorRecordSchema.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ public protocol AdaptorRecordSchema : class, SmartDescription {
1818

1919
public extension AdaptorRecordSchema {
2020

21-
public var descriptionPrefix : String {
22-
return "pschema"
23-
}
21+
var descriptionPrefix : String { return "pschema" }
2422

25-
public func appendToDescription(_ ms: inout String) {
23+
func appendToDescription(_ ms: inout String) {
2624
if let attrs = attributes {
2725
ms += " attrs="
2826
ms += attrs.map { $0.name }.joined(separator: ",")
@@ -82,7 +80,13 @@ public final class AdaptorRecordSchemaWithNames : AdaptorRecordSchema {
8280

8381
@discardableResult
8482
public func switchKey(_ oldKey: String, to newKey: String) -> Bool {
85-
guard let index = attributeNames.index(of: oldKey) else { return false }
83+
#if swift(>=5)
84+
guard let index = attributeNames.firstIndex(of: oldKey) else {
85+
return false
86+
}
87+
#else
88+
guard let index = attributeNames.index(of: oldKey) else { return false }
89+
#endif
8690
attributeNames[index] = newKey
8791
return true
8892
}

Sources/ZeeQL/Access/Attribute.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,25 @@ public extension Attribute { // default imp
7272

7373
// MARK: - Property
7474

75-
public var relationshipPath : String? { // for flattened properties
75+
var relationshipPath : String? { // for flattened properties
7676
return nil
7777
}
7878

7979
// MARK: - SQLValue
8080

81-
public func valueFor(SQLExpression context: SQLExpression) -> String {
81+
func valueFor(SQLExpression context: SQLExpression) -> String {
8282
return context.sqlStringFor(schemaObjectName: columnName ?? name)
8383
}
8484

8585
// MARK: - ExpressionEvaluation
8686

87-
public func valueFor(object: Any?) -> Any? {
87+
func valueFor(object: Any?) -> Any? {
8888
return KeyValueCoding.value(forKeyPath: name, inObject: object)
8989
}
9090

9191
// MARK: - Description
9292

93-
public func appendToDescription(_ ms: inout String) {
93+
func appendToDescription(_ ms: inout String) {
9494
if isPattern { ms += " pattern" }
9595

9696
ms += " \(name)"
@@ -374,7 +374,7 @@ public protocol AttributeValue {
374374

375375
public extension AttributeValue {
376376
static var isOptional : Bool { return false }
377-
public static func shouldUseBindVariable(for attribute: Attribute) -> Bool {
377+
static func shouldUseBindVariable(for attribute: Attribute) -> Bool {
378378
return false
379379
}
380380

Sources/ZeeQL/Access/Codable/CodableModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public extension Model {
1111
// This has all those dupes, because we want to capture the full static type.
1212
// It is a little stupid, but hey, type safety!
1313

14-
public typealias CodableOptions = CodableModelDecoder.Options
14+
typealias CodableOptions = CodableModelDecoder.Options
1515

1616
// MARK: - Decodable
1717

Sources/ZeeQL/Access/Codable/EntityPropertyReflectionContainer.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@
540540
* var addresses : [ Address ]
541541
*
542542
*/
543-
internal struct EntityCollectionPropertyReflectionContainer<EntityType: Decodable>
544-
: UnkeyedDecodingContainer
543+
struct EntityCollectionPropertyReflectionContainer<EntityType: Decodable>
544+
: UnkeyedDecodingContainer
545545
{
546546
// This has to live in a different file due to the Xcode 9 compile error
547547
let log : ZeeQLLogger
@@ -640,14 +640,28 @@
640640
defer {
641641
// unregister if something failed
642642
if !didWorkOut, let rs = rsToRemove {
643-
if let idx = sourceEntity.relationships.index(where: { $0 === rs } )
644-
{
643+
#if swift(>=5)
644+
let optIdx = sourceEntity.relationships
645+
.firstIndex(where: { $0 === rs } )
646+
#else
647+
let optIdx = sourceEntity.relationships
648+
.index(where: { $0 === rs } )
649+
#endif
650+
if let idx = optIdx {
645651
sourceEntity.relationships.remove(at: idx)
646-
if let cpn = sourceEntity.classPropertyNames,
647-
let idx = cpn.index(where: { $0 == name } )
648-
{
649-
sourceEntity.classPropertyNames?.remove(at: idx)
650-
}
652+
#if swift(>=5)
653+
if let cpn = sourceEntity.classPropertyNames,
654+
let idx = cpn.firstIndex(where: { $0 == name } )
655+
{
656+
sourceEntity.classPropertyNames?.remove(at: idx)
657+
}
658+
#else
659+
if let cpn = sourceEntity.classPropertyNames,
660+
let idx = cpn.index(where: { $0 == name } )
661+
{
662+
sourceEntity.classPropertyNames?.remove(at: idx)
663+
}
664+
#endif
651665
}
652666
}
653667
}

0 commit comments

Comments
 (0)