Skip to content

Commit

Permalink
style: run swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Jan 12, 2024
1 parent 37b68d2 commit e398853
Show file tree
Hide file tree
Showing 25 changed files with 303 additions and 280 deletions.
2 changes: 1 addition & 1 deletion Examples/Examples/Auth/GoogleSignIn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Created by Guilherme Souza on 22/12/23.
//

import SwiftUI
import AuthenticationServices
import SwiftUI

struct GoogleSignIn: View {
@Environment(\.webAuthenticationSession) var webAuthenticationSession
Expand Down
5 changes: 4 additions & 1 deletion Examples/Examples/ExamplesApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ struct ExamplesApp: App {
let supabase = SupabaseClient(
supabaseURL: Secrets.supabaseURL,
supabaseKey: Secrets.supabaseAnonKey,
options: .init(auth: .init(storage: KeychainLocalStorage(service: "supabase.gotrue.swift", accessGroup: nil)))
options: .init(auth: .init(storage: KeychainLocalStorage(
service: "supabase.gotrue.swift",
accessGroup: nil
)))
)
5 changes: 4 additions & 1 deletion Examples/UserManagement/Supabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ import Supabase
let supabase = SupabaseClient(
supabaseURL: URL(string: "https://PROJECT_ID.supabase.co")!,
supabaseKey: "YOUR_SUPABASE_ANON_KEY",
options: .init(auth: .init(storage: KeychainLocalStorage(service: "supabase.gotrue.swift", accessGroup: nil)))
options: .init(auth: .init(storage: KeychainLocalStorage(
service: "supabase.gotrue.swift",
accessGroup: nil
)))
)
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ var goTrueDependencies: [Target.Dependency] = [
]

#if !os(Windows) && !os(Linux)
dependencies += [
.package(url: "https://github.com/kishikawakatsumi/KeychainAccess", from: "4.2.2"),
]
goTrueDependencies += [
.product(name: "KeychainAccess", package: "KeychainAccess"),
]
dependencies += [
.package(url: "https://github.com/kishikawakatsumi/KeychainAccess", from: "4.2.2"),
]
goTrueDependencies += [
.product(name: "KeychainAccess", package: "KeychainAccess"),
]
#endif

let package = Package(
Expand Down
39 changes: 18 additions & 21 deletions Sources/Auth/Internal/FixedWidthInteger+Random.swift
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import Foundation

// Borrowed from the Vapor project, https://github.com/vapor/vapor/blob/main/Sources/Vapor/Utilities/Array%2BRandom.swift#L14
// Borrowed from the Vapor project,
// https://github.com/vapor/vapor/blob/main/Sources/Vapor/Utilities/Array%2BRandom.swift#L14
extension FixedWidthInteger {
internal static func random() -> Self {
return Self.random(in: .min ... .max)
}
static func random() -> Self {
random(in: .min ... .max)
}

internal static func random<T>(using generator: inout T) -> Self
where T : RandomNumberGenerator
{
return Self.random(in: .min ... .max, using: &generator)
}
static func random(using generator: inout some RandomNumberGenerator) -> Self {
random(in: .min ... .max, using: &generator)
}
}

extension Array where Element: FixedWidthInteger {
internal static func random(count: Int) -> [Element] {
var array: [Element] = .init(repeating: 0, count: count)
(0..<count).forEach { array[$0] = Element.random() }
return array
}
static func random(count: Int) -> [Element] {
var array: [Element] = .init(repeating: 0, count: count)
(0 ..< count).forEach { array[$0] = Element.random() }
return array
}

internal static func random<T>(count: Int, using generator: inout T) -> [Element]
where T: RandomNumberGenerator
{
var array: [Element] = .init(repeating: 0, count: count)
(0..<count).forEach { array[$0] = Element.random(using: &generator) }
return array
}
static func random(count: Int, using generator: inout some RandomNumberGenerator) -> [Element] {
var array: [Element] = .init(repeating: 0, count: count)
(0 ..< count).forEach { array[$0] = Element.random(using: &generator) }
return array
}
}
38 changes: 19 additions & 19 deletions Sources/Auth/Storage/KeychainLocalStorage.swift
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#if !os(Windows) && !os(Linux)
import Foundation
@preconcurrency import KeychainAccess
import Foundation
@preconcurrency import KeychainAccess

public struct KeychainLocalStorage: AuthLocalStorage {
private let keychain: Keychain
public struct KeychainLocalStorage: AuthLocalStorage {
private let keychain: Keychain

public init(service: String, accessGroup: String?) {
if let accessGroup {
keychain = Keychain(service: service, accessGroup: accessGroup)
} else {
keychain = Keychain(service: service)
public init(service: String, accessGroup: String?) {
if let accessGroup {
keychain = Keychain(service: service, accessGroup: accessGroup)
} else {
keychain = Keychain(service: service)
}
}
}

public func store(key: String, value: Data) throws {
try keychain.set(value, key: key)
}
public func store(key: String, value: Data) throws {
try keychain.set(value, key: key)
}

public func retrieve(key: String) throws -> Data? {
try keychain.getData(key)
}
public func retrieve(key: String) throws -> Data? {
try keychain.getData(key)
}

public func remove(key: String) throws {
try keychain.remove(key)
public func remove(key: String) throws {
try keychain.remove(key)
}
}
}
#endif
114 changes: 58 additions & 56 deletions Sources/Auth/Storage/WinCredLocalStorage.swift
Original file line number Diff line number Diff line change
@@ -1,82 +1,84 @@
#if os(Windows)
import Foundation
import WinSDK
import Foundation
import WinSDK

enum WinCredLocalStorageError: Error {
case windows(UInt32)
case other(Int)
}
enum WinCredLocalStorageError: Error {
case windows(UInt32)
case other(Int)
}

public struct WinCredLocalStorage: AuthLocalStorage {
private let service: String
public struct WinCredLocalStorage: AuthLocalStorage {
private let service: String

private let credentialType: DWORD
private let credentialPersistence: DWORD
private let credentialType: DWORD
private let credentialPersistence: DWORD

public init(service: String) {
self.service = service
credentialType = DWORD(CRED_TYPE_GENERIC)
credentialPersistence = DWORD(CRED_PERSIST_LOCAL_MACHINE)
}
public init(service: String) {
self.service = service
credentialType = DWORD(CRED_TYPE_GENERIC)
credentialPersistence = DWORD(CRED_PERSIST_LOCAL_MACHINE)
}

public func store(key: String, value: Data) throws {
var valueData = value
public func store(key: String, value: Data) throws {
var valueData = value

var credential: CREDENTIALW = .init()
var credential: CREDENTIALW = .init()

credential.Type = credentialType
credential.Persist = credentialPersistence
"\(service)\\\(key)".withCString(encodedAs: UTF16.self, { keyName in
credential.TargetName = UnsafeMutablePointer(mutating: keyName)
})
credential.Type = credentialType
credential.Persist = credentialPersistence
"\(service)\\\(key)".withCString(encodedAs: UTF16.self) { keyName in
credential.TargetName = UnsafeMutablePointer(mutating: keyName)
}

withUnsafeMutableBytes(of: &valueData, { data in
credential.CredentialBlobSize = DWORD(data.count)
credential.CredentialBlob = data.baseAddress!.assumingMemoryBound(to: UInt8.self)
})
withUnsafeMutableBytes(of: &valueData) { data in
credential.CredentialBlobSize = DWORD(data.count)
credential.CredentialBlob = data.baseAddress!.assumingMemoryBound(to: UInt8.self)
}

if !CredWriteW(&credential, 0) {
let lastError = GetLastError()
debugPrint("Unable to save password to credential vault, got error code \(lastError)")
if !CredWriteW(&credential, 0) {
let lastError = GetLastError()
debugPrint("Unable to save password to credential vault, got error code \(lastError)")

throw WinCredLocalStorageError.windows(lastError)
throw WinCredLocalStorageError.windows(lastError)
}
}
}

public func retrieve(key: String) throws -> Data? {
var credential: PCREDENTIALW?
public func retrieve(key: String) throws -> Data? {
var credential: PCREDENTIALW?

let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self, { $0 })
let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self) { $0 }

if !CredReadW(targetName, credentialType, 0, &credential) {
let lastError = GetLastError()
debugPrint("Unable to find entry for key in credential vault, got error code \(lastError)")
if !CredReadW(targetName, credentialType, 0, &credential) {
let lastError = GetLastError()
debugPrint("Unable to find entry for key in credential vault, got error code \(lastError)")

throw WinCredLocalStorageError.windows(lastError)
}
throw WinCredLocalStorageError.windows(lastError)
}

guard let foundCredential = credential, let blob = foundCredential.pointee.CredentialBlob else {
throw WinCredLocalStorageError.other(-1)
}
guard let foundCredential = credential,
let blob = foundCredential.pointee.CredentialBlob
else {
throw WinCredLocalStorageError.other(-1)
}

let blobSize = Int(foundCredential.pointee.CredentialBlobSize)
let pointer = blob.withMemoryRebound(to: UInt8.self, capacity: blobSize, { $0 })
let data = Data(bytes: pointer, count: blobSize)
let blobSize = Int(foundCredential.pointee.CredentialBlobSize)
let pointer = blob.withMemoryRebound(to: UInt8.self, capacity: blobSize) { $0 }
let data = Data(bytes: pointer, count: blobSize)

CredFree(foundCredential)
CredFree(foundCredential)

return data
}
return data
}

public func remove(key: String) throws {
let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self, { $0 })
public func remove(key: String) throws {
let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self) { $0 }

if !CredDeleteW(targetName, credentialType, 0) {
let lastError = GetLastError()
debugPrint("Unable to remove key from credential vault, got error code \(lastError)")
if !CredDeleteW(targetName, credentialType, 0) {
let lastError = GetLastError()
debugPrint("Unable to remove key from credential vault, got error code \(lastError)")

throw WinCredLocalStorageError.windows(lastError)
throw WinCredLocalStorageError.windows(lastError)
}
}
}
}
#endif
2 changes: 1 addition & 1 deletion Sources/Functions/FunctionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
@_spi(Internal) import _Helpers

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

let version = _Helpers.version
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgREST/PostgrestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
@_spi(Internal) import _Helpers

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

/// PostgREST client.
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgREST/PostgrestFilterBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public class PostgrestFilterBuilder: PostgrestTransformBuilder {

public func match(_ query: [String: URLQueryRepresentable]) -> PostgrestFilterBuilder {
mutableState.withValue { mutableState in
query.forEach { key, value in
for (key, value) in query {
mutableState.request.query.append(URLQueryItem(
name: key,
value: "eq.\(value.queryValue)"
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgREST/Types.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

public struct PostgrestError: Error, Codable, Sendable {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Realtime/PhoenixTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

// ----------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions Sources/Realtime/Presence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public final class Presence {
onLeave: self.caller.onLeave
)

self.pendingDiffs.forEach { diff in
for diff in self.pendingDiffs {
self.state = Presence.syncDiff(
self.state,
diff: diff,
Expand Down Expand Up @@ -305,13 +305,13 @@ public final class Presence {
var leaves: Presence.State = [:]
var joins: Presence.State = [:]

state.forEach { key, presence in
for (key, presence) in state {
if newState[key] == nil {
leaves[key] = presence
}
}

newState.forEach { key, newPresence in
for (key, newPresence) in newState {
if let currentPresence = state[key] {
let newRefs = newPresence["metas"]!.map { $0["phx_ref"] as! String }
let curRefs = currentPresence["metas"]!.map { $0["phx_ref"] as! String }
Expand Down
4 changes: 2 additions & 2 deletions Sources/Realtime/RealtimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Foundation
import ConcurrencyExtras

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

public enum SocketError: Error {
Expand Down Expand Up @@ -842,7 +842,7 @@ public class RealtimeClient: PhoenixTransportDelegate {

/// Triggers an error event to all of the connected Channels
func triggerChannelError() {
channels.forEach { channel in
for channel in channels {
// Only trigger a channel error if it is in an "opened" state
if !(channel.isErrored || channel.isLeaving || channel.isClosed) {
channel.trigger(event: ChannelEvent.error)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation
@_exported import Storage

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

let version = _Helpers.version
Expand Down
2 changes: 1 addition & 1 deletion Sources/Supabase/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
import PostgREST

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

public struct SupabaseClientOptions: Sendable {
Expand Down
Loading

0 comments on commit e398853

Please sign in to comment.