-
-
Notifications
You must be signed in to change notification settings - Fork 148
feat(realtime): add predefined filters instead of regular String
#669
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
71c89be
Add RealTme filter
LucasAbijmil 1051d89
Fix typo
LucasAbijmil b53f3e7
RealtimeFilterValue & RealtimeFilter
LucasAbijmil 9ff51dd
Create proper files for FilerValue & Filter
LucasAbijmil 069d595
Add tests
LucasAbijmil 3c76a22
Clean up
LucasAbijmil 7f3887b
Add tests
LucasAbijmil 879a2c4
Renaming
LucasAbijmil f074ac2
Update Tests
LucasAbijmil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// RealtimePostgresFilter.swift | ||
// Supabase | ||
// | ||
// Created by Lucas Abijmil on 19/02/2025. | ||
// | ||
|
||
/// A filter that can be used in Realtime. | ||
public enum RealtimePostgresFilter { | ||
case eq(_ column: String, value: any RealtimePostgresFilterValue) | ||
case neq(_ column: String, value: any RealtimePostgresFilterValue) | ||
case gt(_ column: String, value: any RealtimePostgresFilterValue) | ||
case gte(_ column: String, value: any RealtimePostgresFilterValue) | ||
case lt(_ column: String, value: any RealtimePostgresFilterValue) | ||
case lte(_ column: String, value: any RealtimePostgresFilterValue) | ||
case `in`(_ column: String, values: [any RealtimePostgresFilterValue]) | ||
|
||
var value: String { | ||
switch self { | ||
case let .eq(column, value): | ||
return "\(column)=eq.\(value.rawValue)" | ||
case let .neq(column, value): | ||
return "\(column)=neq.\(value.rawValue)" | ||
case let .gt(column, value): | ||
return "\(column)=gt.\(value.rawValue)" | ||
case let .gte(column, value): | ||
return "\(column)=gte.\(value.rawValue)" | ||
case let .lt(column, value): | ||
return "\(column)=lt.\(value.rawValue)" | ||
case let .lte(column, value): | ||
return "\(column)=lte.\(value.rawValue)" | ||
case let .in(column, values): | ||
return "\(column)=in.(\(values.map(\.rawValue).joined(separator: ",")))" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// RealtimePostgresFilterValue.swift | ||
// Supabase | ||
// | ||
// Created by Lucas Abijmil on 19/02/2025. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A value that can be used to filter Realtime changes in a channel. | ||
public protocol RealtimePostgresFilterValue { | ||
var rawValue: String { get } | ||
} | ||
|
||
extension String: RealtimePostgresFilterValue { | ||
public var rawValue: String { self } | ||
} | ||
|
||
extension Int: RealtimePostgresFilterValue { | ||
public var rawValue: String { "\(self)" } | ||
} | ||
|
||
extension Double: RealtimePostgresFilterValue { | ||
public var rawValue: String { "\(self)" } | ||
} | ||
|
||
extension Bool: RealtimePostgresFilterValue { | ||
public var rawValue: String { "\(self)" } | ||
} | ||
|
||
extension UUID: RealtimePostgresFilterValue { | ||
public var rawValue: String { uuidString } | ||
} | ||
|
||
extension Date: RealtimePostgresFilterValue { | ||
public var rawValue: String { | ||
let formatter = ISO8601DateFormatter() | ||
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] | ||
return formatter.string(from: self) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// RealtimePostgresFilterTests.swift | ||
// Supabase | ||
// | ||
// Created by Lucas Abijmil on 20/02/2025. | ||
// | ||
|
||
import XCTest | ||
@testable import Realtime | ||
|
||
final class RealtimePostgresFilterTests: XCTestCase { | ||
|
||
func testEq() { | ||
let value = "value" | ||
let column = "column" | ||
let filter: RealtimePostgresFilter = .eq(column, value: value) | ||
|
||
XCTAssertEqual(filter.value, "column=eq.value") | ||
} | ||
|
||
func testNeq() { | ||
let value = "value" | ||
let column = "column" | ||
let filter: RealtimePostgresFilter = .neq(column, value: value) | ||
|
||
XCTAssertEqual(filter.value, "column=neq.value") | ||
} | ||
|
||
func testGt() { | ||
let value = "value" | ||
let column = "column" | ||
let filter: RealtimePostgresFilter = .gt(column, value: value) | ||
|
||
XCTAssertEqual(filter.value, "column=gt.value") | ||
} | ||
|
||
func testGte() { | ||
let value = "value" | ||
let column = "column" | ||
let filter: RealtimePostgresFilter = .gte(column, value: value) | ||
|
||
XCTAssertEqual(filter.value, "column=gte.value") | ||
} | ||
|
||
func testLt() { | ||
let value = "value" | ||
let column = "column" | ||
let filter: RealtimePostgresFilter = .lt(column, value: value) | ||
|
||
XCTAssertEqual(filter.value, "column=lt.value") | ||
} | ||
|
||
func testLte() { | ||
let value = "value" | ||
let column = "column" | ||
let filter: RealtimePostgresFilter = .lte(column, value: value) | ||
|
||
XCTAssertEqual(filter.value, "column=lte.value") | ||
} | ||
|
||
func testIn() { | ||
let values = ["value1", "value2"] | ||
let column = "column" | ||
let filter: RealtimePostgresFilter = .in(column, values: values) | ||
|
||
XCTAssertEqual(filter.value, "column=in.(value1,value2)") | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Tests/RealtimeTests/RealtimePostgresFilterValueTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// RealtimePostgresFilterValueTests.swift | ||
// Supabase | ||
// | ||
// Created by Lucas Abijmil on 19/02/2025. | ||
// | ||
|
||
import XCTest | ||
@testable import Realtime | ||
|
||
final class RealtimePostgresFilterValueTests: XCTestCase { | ||
func testUUID() { | ||
XCTAssertEqual( | ||
UUID(uuidString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")!.rawValue, | ||
"E621E1F8-C36C-495A-93FC-0C247A3E6E5F") | ||
} | ||
|
||
func testDate() { | ||
XCTAssertEqual( | ||
Date(timeIntervalSince1970: 1_737_465_985).rawValue, | ||
"2025-01-21T13:26:25.000Z" | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.