Skip to content

make search related container structs decodable #43

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 2 commits into from
May 2, 2025
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
2 changes: 1 addition & 1 deletion Sources/Typesense/Models/DocumentIndexParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public struct DocumentIndexParameters: Codable {
case dirtyValues = "dirty_values"
}

}
}
2 changes: 1 addition & 1 deletion Sources/Typesense/Models/MultiSearchResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation



public struct MultiSearchResult<T: Codable>: Codable {
public struct MultiSearchResult<T: Decodable>: Decodable {

public var results: [SearchResult<T>]
public var conversation: SearchResultConversation?
Expand Down
2 changes: 1 addition & 1 deletion Sources/Typesense/Models/PresetValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public enum PresetValue: Codable {
try singleCollectionSearch.encode(to: encoder)
}
}
}
}
2 changes: 1 addition & 1 deletion Sources/Typesense/Models/SearchGroupedHit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import AnyCodable

public struct SearchGroupedHit<T: Codable>: Codable {
public struct SearchGroupedHit<T: Decodable>: Decodable {

public var found: Int?
public var groupKey: [AnyCodable]
Expand Down
2 changes: 1 addition & 1 deletion Sources/Typesense/Models/SearchResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation



public struct SearchResult<T: Codable>: Codable {
public struct SearchResult<T: Decodable>: Decodable {

public var facetCounts: [FacetCounts]?
/** The number of documents found */
Expand Down
11 changes: 1 addition & 10 deletions Sources/Typesense/Models/SearchResultHit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation



public struct SearchResultHit<T: Codable>: Codable {
public struct SearchResultHit<T: Decodable>: Decodable {

/** (Deprecated) Contains highlighted portions of the search fields */
public var highlights: [SearchHighlight]?
Expand All @@ -36,15 +36,6 @@ public struct SearchResultHit<T: Codable>: Codable {
self.geoDistanceMeters = try container.decodeIfPresent([String : Int].self, forKey: SearchResultHit<T>.CodingKeys.geoDistanceMeters)
self.vectorDistance = try container.decodeIfPresent(Float.self, forKey: SearchResultHit<T>.CodingKeys.vectorDistance)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(highlights, forKey: CodingKeys.highlights)
try container.encode(document, forKey: CodingKeys.document)
try container.encode(textMatch, forKey: CodingKeys.textMatch)
try container.encode(geoDistanceMeters, forKey: CodingKeys.geoDistanceMeters)
try container.encode(vectorDistance, forKey: CodingKeys.vectorDistance)
}

public enum CodingKeys: String, CodingKey {
case highlights
Expand Down
8 changes: 4 additions & 4 deletions get-models.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ find . -name "MultiSearchParameters.swift" -exec sed -i '' 's/maxHits: OneOfMult
find . -name "MultiSearchCollectionParameters.swift" -exec sed -i '' 's/maxHits: Any\?/maxHits: String\?/g' {} \;

# Add Generics to SearchResult
find . -name "SearchResult.swift" -exec sed -i '' 's/SearchResult:/SearchResult<T: Codable>:/g' {} \;
find . -name "SearchResult.swift" -exec sed -i '' 's/SearchResult:/SearchResult<T: Decodable>:/g' {} \;
find . -name "SearchResult.swift" -exec sed -i '' 's/groupedHits: \[SearchGroupedHit\]\?/groupedHits: \[SearchGroupedHit<T>\]\?/g' {} \;
find . -name "SearchResult.swift" -exec sed -i '' 's/hits: \[SearchResultHit\]\?/hits: \[SearchResultHit<T>\]\?/g' {} \;

# Add Generics to MultiSearchResult
find . -name "MultiSearchResult.swift" -exec sed -i '' 's/results: \[SearchResult\]/results: \[SearchResult<T>\]/g' {} \;
find . -name "MultiSearchResult.swift" -exec sed -i '' 's/MultiSearchResult:/MultiSearchResult<T: Codable>:/g' {} \;
find . -name "MultiSearchResult.swift" -exec sed -i '' 's/MultiSearchResult:/MultiSearchResult<T: Decodable>:/g' {} \;

# Add Generics to SearchResultHit
find . -name "SearchResultHit.swift" -exec sed -i '' 's/SearchResultHit:/SearchResultHit<T: Codable>:/g' {} \;
find . -name "SearchResultHit.swift" -exec sed -i '' 's/SearchResultHit:/SearchResultHit<T: Decodable>:/g' {} \;

# Convert document parameter to a generic T
find . -name "SearchResultHit.swift" -exec sed -i '' 's/document: \[String:Any\]\?/document: T?/g' {} \;

# Add Generics to SearchGroupedHit
find . -name "SearchGroupedHit.swift" -exec sed -i '' 's/SearchGroupedHit:/SearchGroupedHit<T: Codable>:/g' {} \;
find . -name "SearchGroupedHit.swift" -exec sed -i '' 's/SearchGroupedHit:/SearchGroupedHit<T: Decodable>:/g' {} \;
find . -name "SearchGroupedHit.swift" -exec sed -i '' 's/hits: \[SearchResultHit\]/hits: \[SearchResultHit<T>\]/g' {} \;

# Convert matchedTokens to custom defined StringQuantum
Expand Down
Loading