Skip to content
Merged
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
32 changes: 0 additions & 32 deletions Sources/MeiliSearch/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -661,38 +661,6 @@ public struct MeiliSearch {
self.settings.resetDisplayedAttributes(UID, completion)
}

// MARK: Accept New Fields

/**
Get the accept new fields field of an `Index`.

- parameter UID: The unique identifier for the `Index` to be found.
- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `Bool`
value. If the request was sucessful or `Error` if a failure occured.
*/
public func getAcceptNewFields(
UID: String,
_ completion: @escaping (Result<Bool, Swift.Error>) -> Void) {
self.settings.getAcceptNewFields(UID, completion)
}

/**
Update the accept new fields field of an `Index`.

- parameter UID: The unique identifier for the `Index` to be found.
- parameter acceptNewFields: The accept new fields option to be applied into `Index`.
- parameter completion: The completion closure used to notify when the server
completes the query request, it returns a `Result` object that contains `Update`
value. If the request was sucessful or `Error` if a failure occured.
*/
public func updateAcceptNewFields(
UID: String,
_ acceptNewFields: Bool,
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {
self.settings.updateAcceptNewFields(UID, acceptNewFields, completion)
}

// MARK: Attributes for faceting

/**
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ struct Indexes {
_ data: Data,
_ completion: (Result<Index, Swift.Error>) -> Void) {
do {
let index = try Constants.customJSONDecoder.decode(Index.self, from: data)
let index: Index = try Constants.customJSONDecoder.decode(Index.self, from: data)

completion(.success(index))
} catch {
Expand All @@ -178,7 +178,7 @@ struct Indexes {
_ data: Data,
_ completion: (Result<[Index], Swift.Error>) -> Void) {
do {
let indexes = try Constants.customJSONDecoder.decode([Index].self, from: data)
let indexes: [Index] = try Constants.customJSONDecoder.decode([Index].self, from: data)

completion(.success(indexes))
} catch {
Expand Down
39 changes: 10 additions & 29 deletions Sources/MeiliSearch/Model/Setting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,34 @@ import Foundation
/**
Each instance of MeiliSearch has three keys: a master, a private, and a public.
Each key has a given set of permissions on the API routes.
*/
*/
public struct Setting: Codable, Equatable {

// MARK: Properties

///List of ranking rules for a given `Index`.
/// List of ranking rules for a given `Index`.
public let rankingRules: [String]

///List of searchable attributes for a given `Index`.
/// List of searchable attributes for a given `Index`.
public let searchableAttributes: [String]

///List of displayed attributes for a given `Index`.
/// List of displayed attributes for a given `Index`.
public let displayedAttributes: [String]

///List of stop-words for a given `Index`.
/// List of stop-words for a given `Index`.
public let stopWords: [String]

///List of synonyms and its values for a given `Index`.
/// List of synonyms and its values for a given `Index`.
public let synonyms: [String: [String]]

///Return if a given `Index` allows new fields.
public let acceptNewFields: Bool

public init(
rankingRules: [String],
searchableAttributes: [String],
displayedAttributes: [String],
stopWords: [String],
synonyms: [String: [String]],
acceptNewFields: Bool) {
self.rankingRules = rankingRules
self.searchableAttributes = searchableAttributes
self.displayedAttributes = displayedAttributes
self.stopWords = stopWords
self.synonyms = synonyms
self.acceptNewFields = acceptNewFields
}

///Tries to decode the JSON object to Setting object.
/// Tries to decode the JSON object to Setting object.
public init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)

rankingRules = (try? values?.decodeIfPresent([String].self, forKey: .rankingRules)) ?? []
searchableAttributes = (try? values?.decodeIfPresent([String].self, forKey: .searchableAttributes)) ?? []
displayedAttributes = (try? values?.decodeIfPresent([String].self, forKey: .displayedAttributes)) ?? []
searchableAttributes = (try? values?.decodeIfPresent([String].self, forKey: .searchableAttributes)) ?? ["*"]
displayedAttributes = (try? values?.decodeIfPresent([String].self, forKey: .displayedAttributes)) ?? ["*"]
stopWords = (try? values?.decodeIfPresent([String].self, forKey: .stopWords)) ?? []
synonyms = (try? values?.decodeIfPresent([String: [String]].self, forKey: .synonyms)) ?? [:]
acceptNewFields = (try? values?.decodeIfPresent(Bool.self, forKey: .acceptNewFields)) ?? false
}

}
60 changes: 0 additions & 60 deletions Sources/MeiliSearch/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -681,66 +681,6 @@ struct Settings {

}

// MARK: Accept New Fields

func getAcceptNewFields(
_ UID: String,
_ completion: @escaping (Result<Bool, Swift.Error>) -> Void) {

self.request.get(api: "/indexes/\(UID)/settings/accept-new-fields") { result in

switch result {
case .success(let data):

guard let data: Data = data else {
completion(.failure(MeiliSearch.Error.dataNotFound))
return
}

let acceptNewFields: String = String(decoding: data, as: UTF8.self)

completion(.success(acceptNewFields == "true"))

case .failure(let error):
completion(.failure(error))
}

}

}

func updateAcceptNewFields(
_ UID: String,
_ acceptNewFields: Bool,
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {

let acceptNewFieldsString: String = acceptNewFields ? "true" : "false"
guard let data: Data = acceptNewFieldsString.data(using: .ascii) else {
completion(.failure(MeiliSearch.Error.invalidJSON))
return
}

self.request.post(api: "/indexes/\(UID)/settings/accept-new-fields", data) { result in

switch result {
case .success(let data):

do {
let decoder: JSONDecoder = JSONDecoder()
let update: Update = try decoder.decode(Update.self, from: data)
completion(.success(update))
} catch {
completion(.failure(error))
}

case .failure(let error):
completion(.failure(error))
}

}

}

// MARK: Attributes for faceting

func getAttributesForFaceting(
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Stats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Stats {
}

do {
let stat = try Constants.customJSONDecoder.decode(Stat.self, from: data)
let stat: Stat = try Constants.customJSONDecoder.decode(Stat.self, from: data)

completion(.success(stat))
} catch {
Expand Down Expand Up @@ -55,7 +55,7 @@ struct Stats {
}

do {
let allStats = try Constants.customJSONDecoder.decode(AllStats.self, from: data)
let allStats: AllStats = try Constants.customJSONDecoder.decode(AllStats.self, from: data)

completion(.success(allStats))
} catch {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct System {
}

do {
let vesion = try Constants.customJSONDecoder.decode(Version.self, from: data)
let vesion: Version = try Constants.customJSONDecoder.decode(Version.self, from: data)

completion(.success(vesion))
} catch {
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Updates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct Updates {
}

do {
let result = try Constants.customJSONDecoder.decode(Update.Result.self, from: data)
let result: Update.Result = try Constants.customJSONDecoder.decode(Update.Result.self, from: data)

completion(.success(result))
} catch {
Expand Down Expand Up @@ -61,7 +61,7 @@ struct Updates {
}

do {
let result = try Constants.customJSONDecoder.decode([Update.Result].self, from: data)
let result: [Update.Result] = try Constants.customJSONDecoder.decode([Update.Result].self, from: data)

completion(.success(result))
} catch {
Expand Down
12 changes: 6 additions & 6 deletions Tests/MeiliSearchUnitTests/IndexesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class IndexesTests: XCTestCase {

let jsonData = jsonString.data(using: .utf8)!

let stubIndex = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)
let stubIndex: Index = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)

session.pushData(jsonString)

Expand Down Expand Up @@ -67,7 +67,7 @@ class IndexesTests: XCTestCase {

let jsonData = jsonString.data(using: .utf8)!

let stubIndex = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)
let stubIndex: Index = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)

session.pushData(jsonString)

Expand Down Expand Up @@ -113,7 +113,7 @@ class IndexesTests: XCTestCase {

let jsonData = getJsonString.data(using: .utf8)!

let stubIndex = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)
let stubIndex: Index = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)

session.pushData(getJsonString)

Expand Down Expand Up @@ -153,7 +153,7 @@ class IndexesTests: XCTestCase {

let jsonData = jsonString.data(using: .utf8)!

let stubIndex = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)
let stubIndex: Index = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)

session.pushData(jsonString)

Expand Down Expand Up @@ -195,7 +195,7 @@ class IndexesTests: XCTestCase {

let jsonData = jsonString.data(using: .utf8)!

let stubIndexes = try! Constants.customJSONDecoder.decode([Index].self, from: jsonData)
let stubIndexes: [Index] = try! Constants.customJSONDecoder.decode([Index].self, from: jsonData)

session.pushData(jsonString)

Expand Down Expand Up @@ -234,7 +234,7 @@ class IndexesTests: XCTestCase {

let jsonData = jsonString.data(using: .utf8)!

let stubIndex = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)
let stubIndex: Index = try! Constants.customJSONDecoder.decode(Index.self, from: jsonData)

session.pushData(jsonString)

Expand Down
4 changes: 2 additions & 2 deletions Tests/MeiliSearchUnitTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SearchTests: XCTestCase {

let data = jsonString.data(using: .utf8)!

let stubSearchResult = try! Constants.customJSONDecoder.decode(SearchResult<Movie>.self, from: data)
let stubSearchResult: SearchResult<Movie> = try! Constants.customJSONDecoder.decode(SearchResult<Movie>.self, from: data)

session.pushData(jsonString)

Expand Down Expand Up @@ -119,7 +119,7 @@ class SearchTests: XCTestCase {

let data = jsonString.data(using: .utf8)!

let stubSearchResult = try! Constants.customJSONDecoder.decode(SearchResult<Movie>.self, from: data)
let stubSearchResult: SearchResult<Movie> = try! Constants.customJSONDecoder.decode(SearchResult<Movie>.self, from: data)

session.pushData(jsonString)

Expand Down
Loading