Skip to content
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

Extract models into a separate Core module #437

Draft
wants to merge 1 commit into
base: main
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
75 changes: 58 additions & 17 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,64 @@

import PackageDescription

let MeiliSearch = "MeiliSearch"
let MeiliSearchCore = "MeiliSearchCore"
let MeiliSearchNIO = "MeiliSearchNIO"

var products: [PackageDescription.Product] = [
.library(name: MeiliSearch, targets: [MeiliSearch]),
.library(name: MeiliSearchCore, targets: [MeiliSearchCore])
]

var dependencies = [PackageDescription.Package.Dependency]()

var targets: [PackageDescription.Target] = [
.target(
name: MeiliSearch,
dependencies: [
.target(name: MeiliSearchCore)
]
),
.target(
name: MeiliSearchCore,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: this new target should not break the Podspec as it does a global search for all Swift files within the Sources directory, however when we do add the NIO target it will fail to compile due to the lack of the dependency.

AHC (or NIO) is not available officially on CocoaPods, so this will be a SPM-only feature. I'm okay with that but we must document it and ensure that CocoaPods does continue to work (even if only using Foundation).

dependencies: []
),
.testTarget(
name: "MeiliSearchUnitTests",
dependencies: ["MeiliSearch"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: where we reference target names, let's be consistent and use the static variable

Suggested change
dependencies: ["MeiliSearch"]
dependencies: [.target(MeiliSearch)]

),
.testTarget(
name: "MeiliSearchIntegrationTests",
dependencies: ["MeiliSearch"]
)
]

#if os(Linux) || USE_NIO
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: I like the use of USE_NIO here as it'll allow macOS clients (for working on Vapor projects locally) to build the NIO integration. When we add the NIO integration (presumably the next PR), let's document how to use this.

products.append(.library(name: MeiliSearchNIO, targets: [MeiliSearchNIO]))
dependencies.append(.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.19.0"))
targets.append(
.target(
name: MeiliSearchNIO,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

major: this PR will currently fail to build as-is due to this target path not existing. shall we delete this entire Linux section until you contribute the NIO integration in your next PR?

dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.target(name: MeiliSearchCore)
]
)
)

targets.append(
.testTarget(
name: "MeiliSearchNIOTests",
dependencies: [
.target(name: MeiliSearchNIO)
]
)
)
#endif

let package = Package(
name: "meilisearch-swift",
products: [
.library(name: "MeiliSearch", targets: ["MeiliSearch"])
],
targets: [
.target(
name: "MeiliSearch",
dependencies: []
),
.testTarget(
name: "MeiliSearchUnitTests",
dependencies: ["MeiliSearch"]
),
.testTarget(
name: "MeiliSearchIntegrationTests",
dependencies: ["MeiliSearch"]
)
]
products: products,
dependencies: dependencies,
targets: targets
)
1 change: 1 addition & 0 deletions Sources/MeiliSearch/Async/Client+async.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import MeiliSearchCore

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension MeiliSearch {
Expand Down
1 change: 1 addition & 0 deletions Sources/MeiliSearch/Async/Indexes+async.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import MeiliSearchCore

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension Indexes {
Expand Down
20 changes: 15 additions & 5 deletions Sources/MeiliSearch/Client.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import MeiliSearchCore

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

/**
Expand Down Expand Up @@ -151,8 +153,8 @@ public struct MeiliSearch {
public func waitForTask(
taskUid: Int,
options: WaitOptions? = nil,
_ completion: @escaping (Result<Task, Swift.Error>
) -> Void) {
_ completion: @escaping (Result<Task, Swift.Error>) -> Void
) {
self.tasks.waitForTask(taskUid: taskUid, options: options, completion)
}

Expand All @@ -169,8 +171,8 @@ public struct MeiliSearch {
public func waitForTask(
task: TaskInfo,
options: WaitOptions? = nil,
_ completion: @escaping (Result<Task, Swift.Error>
) -> Void) {
_ completion: @escaping (Result<Task, Swift.Error>) -> Void
) {
self.tasks.waitForTask(taskUid: task.taskUid, options: options, completion)
}

Expand Down Expand Up @@ -382,3 +384,11 @@ public struct MeiliSearch {
self.dumps.create(completion)
}
}

extension TaskInfo {
@discardableResult
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public func wait(on client: MeiliSearch, options: WaitOptions? = nil) async throws -> Task {
try await client.waitForTask(task: self, options: options)
}
}
4 changes: 3 additions & 1 deletion Sources/MeiliSearch/Documents.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import MeiliSearchCore

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

struct Documents {
Expand Down
4 changes: 3 additions & 1 deletion Sources/MeiliSearch/Dumps.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import MeiliSearchCore

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

/**
Expand Down
16 changes: 4 additions & 12 deletions Sources/MeiliSearch/Error.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import MeiliSearchCore

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
Expand All @@ -7,17 +9,7 @@ import Foundation
Represent all error types in the client.
*/
public extension MeiliSearch {
// MARK: Error
struct MSErrorResponse: Decodable, Encodable, Equatable {
/// A human-readable description of the error
public let message: String
/// The error code (https://www.meilisearch.com/docs/reference/errors/error_codes)
public let code: String
/// The error type (https://www.meilisearch.com/docs/reference/errors/overview#errors)
public let type: String
/// A link to the relevant section of the documentation
public let link: String?
}
typealias MSErrorResponse = ErrorResponse

static func errorHandler(url: URL, data: Data?, response: URLResponse?, error: Swift.Error?) throws {
// Communication Error with Meilisearch
Expand All @@ -36,7 +28,7 @@ public extension MeiliSearch {
if let unwrappedData: Data = data, let res: MeiliSearch.MSErrorResponse = try? Constants.customJSONDecoder.decode(MeiliSearch.MSErrorResponse.self, from: unwrappedData) {
throw MeiliSearch.Error.meiliSearchApiError(
message: res.message,
msErrorResponse: MeiliSearch.MSErrorResponse(
msErrorResponse: MSErrorResponse(
message: res.message,
code: res.code,
type: res.type,
Expand Down
17 changes: 2 additions & 15 deletions Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import MeiliSearchCore

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
Expand Down Expand Up @@ -1115,19 +1117,4 @@ public struct Indexes {
_ completion: @escaping (Result<Stat, Swift.Error>) -> Void) {
self.stats.stats(self.uid, completion)
}

// MARK: Codable

struct UpdateIndexPayload: Codable {
let primaryKey: String
}

struct CreateIndexPayload: Codable {
let uid: String
let primaryKey: String?
}

public struct SwapIndexPayload: Codable, Equatable {
public let indexes: [String]
}
}
2 changes: 2 additions & 0 deletions Sources/MeiliSearch/Keys.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import MeiliSearchCore

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
Expand Down
1 change: 1 addition & 0 deletions Sources/MeiliSearch/QueryParameters/CancelTasksQuery.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import MeiliSearchCore

/**
`CancelTasksQuery` class represent the options used to filter a cancel tasks call.
Expand Down
1 change: 1 addition & 0 deletions Sources/MeiliSearch/QueryParameters/DeleteTasksQuery.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import MeiliSearchCore

/**
`DeleteTasksQuery` class represent the options used to filter a delete tasks call.
Expand Down
2 changes: 2 additions & 0 deletions Sources/MeiliSearch/QueryParameters/TasksQuery.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import MeiliSearchCore

/**
`TasksQuery` class represent the options used to filter a get tasks call.
*/
Expand Down
4 changes: 3 additions & 1 deletion Sources/MeiliSearch/Request.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import MeiliSearchCore

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

/**
Expand Down
4 changes: 3 additions & 1 deletion Sources/MeiliSearch/Search.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import MeiliSearchCore

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

struct Search {
Expand Down
4 changes: 3 additions & 1 deletion Sources/MeiliSearch/Settings.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import MeiliSearchCore

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

/**
Expand Down
4 changes: 3 additions & 1 deletion Sources/MeiliSearch/Stats.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import MeiliSearchCore

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

struct Stats {
Expand Down
2 changes: 2 additions & 0 deletions Sources/MeiliSearch/System.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import MeiliSearchCore

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
Expand Down
2 changes: 2 additions & 0 deletions Sources/MeiliSearch/Tasks.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import MeiliSearchCore

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
Expand Down
22 changes: 22 additions & 0 deletions Sources/MeiliSearchCore/ErrorResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation

public struct ErrorResponse: Error, Codable, Hashable {
/// A human-readable description of the error
public let message: String

/// The error code (https://www.meilisearch.com/docs/reference/errors/error_codes)
public let code: String

/// The error type (https://www.meilisearch.com/docs/reference/errors/overview#errors)
public let type: String

/// A link to the relevant section of the documentation
public let link: String?

public init(message: String, code: String, type: String, link: String? = nil) {
self.message = message
self.code = code
self.type = type
self.link = link
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

internal struct PackageVersion {
public struct PackageVersion {
/// This is the current version of the meilisearch-swift package
private static let current = "0.15.0"
public static let current = "0.15.0"
Comment on lines -8 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why has the access level of this variable changed? where else is it used?

Comment on lines -8 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: let's make sure we update the documentation where this line of code is referenced (the path to this file)

Comment on lines -8 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: let's make sure we update the Podspec where this line of code is referenced (the path to this file)


/**
Retrieves the current version of the MeiliSearch Swift package and formats accordingly.

- returns: String containing the qualified version of this package eg. `Meilisearch Swift (v1.0.0)`
*/
static func qualifiedVersion(_ rawVersion: String? = nil) -> String {
public static func qualifiedVersion(_ rawVersion: String? = nil) -> String {
"Meilisearch Swift (v\(rawVersion ?? self.current))"
}
}
27 changes: 27 additions & 0 deletions Sources/MeiliSearchCore/Payloads.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation

public struct UpdateIndexPayload: Codable {
public let primaryKey: String

public init(primaryKey: String) {
self.primaryKey = primaryKey
}
}

public struct CreateIndexPayload: Codable {
public let uid: String
public let primaryKey: String?

public init(uid: String, primaryKey: String? = nil) {
self.uid = uid
self.primaryKey = primaryKey
}
}

public struct SwapIndexPayload: Codable, Equatable {
public let indexes: [String]

public init(indexes: [String]) {
self.indexes = indexes
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct Task: Decodable, Equatable {
public let canceledBy: Int?

/// Error information in case of failed update.
public let error: MeiliSearch.MSErrorResponse?
public let error: ErrorResponse?

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand All @@ -51,7 +51,7 @@ public struct Task: Decodable, Equatable {
self.startedAt = try container.decodeIfPresent(Date.self, forKey: .startedAt)
self.finishedAt = try container.decodeIfPresent(Date.self, forKey: .finishedAt)
self.canceledBy = try container.decodeIfPresent(Int.self, forKey: .canceledBy)
self.error = try container.decodeIfPresent(MeiliSearch.MSErrorResponse.self, forKey: .error)
self.error = try container.decodeIfPresent(ErrorResponse.self, forKey: .error)

// we ignore errors thrown by `superDecoder` to handle cases where no details are provided by the API
// for example when the type is `snapshotCreation`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public extension Task {

struct TaskIndexSwapDetails: Decodable, Equatable {
/// Object containing the payload for the indexSwap task
public let swaps: [Indexes.SwapIndexPayload]
public let swaps: [SwapIndexPayload]
}

struct TaskCancellationDetails: Decodable, Equatable {
Expand Down
Loading