Skip to content

Commit

Permalink
Fix FirestoreRangeFilter and refactor FirestoreQueryFilter (#54)
Browse files Browse the repository at this point in the history
* Update Firebase version into 9.6.0

* Fix FirestoreRangeFilter and refactor FirestoreQueryFilter
  • Loading branch information
fummicc1 authored Nov 10, 2022
1 parent 2b63abc commit 622fd0d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
40 changes: 20 additions & 20 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,71 @@
"repositoryURL": "https://github.com/firebase/abseil-cpp-SwiftPM.git",
"state": {
"branch": null,
"revision": "d302de612e3d57c6f4afaf087da18fba8eac72a7",
"version": "0.20220203.1"
"revision": "583de9bd60f66b40e78d08599cc92036c2e7e4e1",
"version": "0.20220203.2"
}
},
{
"package": "BoringSSL-GRPC",
"repositoryURL": "https://github.com/firebase/boringssl-SwiftPM.git",
"state": {
"branch": null,
"revision": "79db6516894a932d0ddaff3b05b9da1e4f6c4069",
"version": "0.9.0"
"revision": "dd3eda2b05a3f459fc3073695ad1b28659066eab",
"version": "0.9.1"
}
},
{
"package": "Firebase",
"repositoryURL": "https://github.com/firebase/firebase-ios-sdk",
"state": {
"branch": null,
"revision": "2eb177effe7baf1f13ad0c5f4eb8c71a98429fb5",
"version": "9.1.0"
"revision": "7e80c25b51c2ffa238879b07fbfc5baa54bb3050",
"version": "9.6.0"
}
},
{
"package": "GoogleAppMeasurement",
"repositoryURL": "https://github.com/google/GoogleAppMeasurement.git",
"state": {
"branch": null,
"revision": "192cce3e0486aecfdb61102a9c694c78dc89dc48",
"version": "9.1.0"
"revision": "c1cfde8067668027b23a42c29d11c246152fe046",
"version": "9.6.0"
}
},
{
"package": "GoogleDataTransport",
"repositoryURL": "https://github.com/google/GoogleDataTransport.git",
"state": {
"branch": null,
"revision": "b905c49326b72211531ed9d7baa02d724828a8dc",
"version": "9.1.4"
"revision": "5056b15c5acbb90cd214fe4d6138bdf5a740e5a8",
"version": "9.2.0"
}
},
{
"package": "GoogleUtilities",
"repositoryURL": "https://github.com/google/GoogleUtilities.git",
"state": {
"branch": null,
"revision": "f4abe56ce62a779e64b525eb133c8fc2a84bbc1f",
"version": "7.7.1"
"revision": "22907832079d808e82f1182b21af58ef3880666f",
"version": "7.8.0"
}
},
{
"package": "gRPC",
"repositoryURL": "https://github.com/grpc/grpc-ios.git",
"state": {
"branch": null,
"revision": "2af4f6e9c2b18beae228f50b1198c641be859d2b",
"version": "1.44.2-grpc"
"revision": "8440b914756e0d26d4f4d054a1c1581daedfc5b6",
"version": "1.44.3-grpc"
}
},
{
"package": "GTMSessionFetcher",
"repositoryURL": "https://github.com/google/gtm-session-fetcher.git",
"state": {
"branch": null,
"revision": "19605024d59eaefdb1f6a2cb11ebe75df4421126",
"version": "2.0.0"
"revision": "d4289da23e978f37c344ea6a386e5546e2466294",
"version": "2.1.0"
}
},
{
Expand All @@ -96,17 +96,17 @@
"repositoryURL": "https://github.com/google/promises.git",
"state": {
"branch": null,
"revision": "46c1e6b5ac09d8f82c991061c659f67e573d425d",
"version": "2.1.0"
"revision": "3e4e743631e86c8c70dbc6efdc7beaa6e90fd3bb",
"version": "2.1.1"
}
},
{
"package": "SwiftProtobuf",
"repositoryURL": "https://github.com/apple/swift-protobuf.git",
"state": {
"branch": null,
"revision": "e1499bc69b9040b29184f7f2996f7bab467c1639",
"version": "1.19.0"
"revision": "b8230909dedc640294d7324d37f4c91ad3dcf177",
"version": "1.20.1"
}
}
]
Expand Down
35 changes: 20 additions & 15 deletions Sources/Firestore/FirestoreClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ typealias FirestoreFilterModel = FirestoreQueryFilter

public protocol FirestoreQueryFilter {
var fieldPath: String? { get }
var value: Any? { get }

func build(from: Query) -> Query
func build<Model: FirestoreModel>(type: Model.Type) -> Query
Expand Down Expand Up @@ -73,28 +72,34 @@ public struct DefaultFirestoreQueryOrder: FirestoreQueryOrder {
@available(*, deprecated, renamed: "FirestoreRangeFilter")
typealias FirestoreFilterRangeModel = FirestoreRangeFilter

public struct FirestoreRangeFilter: FirestoreQueryFilter {
public struct FirestoreRangeFilter<Value: Comparable>: FirestoreQueryFilter {
public var fieldPath: String?
public var value: Any?

public init(fieldPath: String?, value: Any?) {
public var minValue: Value
private var maxValue: Value

public init(fieldPath: String? = nil, minValue: Value, maxValue: Value) {
self.fieldPath = fieldPath
self.value = value
self.minValue = minValue
self.maxValue = maxValue
}

public func build(from: Query) -> Query {
guard let fieldPath = fieldPath, let value = value as? [Any] else {
guard let fieldPath = fieldPath, minValue > maxValue else {
return from
}
return from.whereField(fieldPath, in: value)
return from
.whereField(fieldPath, isGreaterThan: minValue)
.whereField(fieldPath, isLessThan: maxValue)
}

public func build<Model>(type: Model.Type) -> Query where Model : FirestoreModel {
let from = Firestore.firestore().collection(type.collectionName)
guard let fieldPath = fieldPath, let value = value as? [Any] else {
guard let fieldPath = fieldPath, minValue > maxValue else {
return from
}
return from.whereField(fieldPath, in: value)
return from
.whereField(fieldPath, isGreaterThan: minValue)
.whereField(fieldPath, isLessThan: maxValue)
}
}

Expand Down Expand Up @@ -130,15 +135,15 @@ public struct FirestoreEqualFilter: FirestoreQueryFilter {
public struct FirestoreContainFilter: FirestoreQueryFilter {

public var fieldPath: String?
public var value: Any?
public var value: [Any]

public init(fieldPath: String?, value: Any?) {
public init(fieldPath: String?, value: [Any]) {
self.fieldPath = fieldPath
self.value = value
}

public func build(from: Query) -> Query {
guard let fieldPath = fieldPath, let value = value else {
guard let fieldPath = fieldPath, !value.isEmpty else {
assertionFailure("Invalid Data")
return from
}
Expand All @@ -147,7 +152,7 @@ public struct FirestoreContainFilter: FirestoreQueryFilter {

public func build<Model>(type: Model.Type) -> Query where Model : FirestoreModel {
let from = Firestore.firestore().collection(type.collectionName)
guard let fieldPath = fieldPath, let value = value else {
guard let fieldPath = fieldPath, !value.isEmpty else {
return from
}
return from.whereField(fieldPath, arrayContains: value)
Expand Down

0 comments on commit 622fd0d

Please sign in to comment.