@@ -12,12 +12,12 @@ public struct SearchParameters: Codable, Equatable {
1212 /// Query string (mandatory).
1313 public let query : String
1414
15- /// Number of documents to skip.
16- public let offset : Int
17-
1815 /// Number of documents to take.
1916 public let limit : Int
2017
18+ /// Number of documents to skip.
19+ public let offset : Int
20+
2121 /// Document attributes to show.
2222 public let attributesToRetrieve : [ String ] ?
2323
@@ -82,15 +82,57 @@ public struct SearchParameters: Codable, Equatable {
8282 SearchParameters ( query: value)
8383 }
8484
85- private func commaRepresentation( _ array: [ String ] ) -> String {
86- array. joined ( separator: " , " )
85+ enum CodingKeys : String , CodingKey {
86+ case query = " q "
87+ case offset
88+ case limit
89+ case attributesToRetrieve
90+ case attributesToCrop
91+ case cropLength
92+ case attributesToHighlight
93+ case filters
94+ case facetFilters
95+ case facetsDistribution
96+ case matches
8797 }
8898
89- private func commaRepresentationEscaped( _ array: [ String ] ) -> String {
90- var value : String = " [ "
91- value += array. map ( { string in " \" \( string) \" " } ) . joined ( separator: " , " )
92- value += " ] "
93- return value
99+ }
100+
101+ extension SearchParameters {
102+
103+ public func encode( to encoder: Encoder ) throws {
104+ var container = encoder. container ( keyedBy: CodingKeys . self)
105+ try container. encode ( query, forKey: . query)
106+ if limit != 20 {
107+ try container. encode ( limit, forKey: . limit)
108+ }
109+ if offset != 0 {
110+ try container. encode ( offset, forKey: . offset)
111+ }
112+ if let attributesToRetrieve: [ String ] = self . attributesToRetrieve, !attributesToRetrieve. isEmpty {
113+ try container. encode ( attributesToRetrieve, forKey: . attributesToRetrieve)
114+ }
115+ if !attributesToCrop. isEmpty {
116+ try container. encode ( attributesToCrop, forKey: . attributesToCrop)
117+ }
118+ if cropLength != 200 {
119+ try container. encode ( cropLength, forKey: . cropLength)
120+ }
121+ if !attributesToHighlight. isEmpty {
122+ try container. encode ( attributesToHighlight, forKey: . attributesToHighlight)
123+ }
124+ if let filters: String = self . filters, !filters. isEmpty {
125+ try container. encode ( filters, forKey: . filters)
126+ }
127+ if let facetFilters: [ [ String ] ] = self . facetFilters {
128+ try container. encode ( facetFilters, forKey: . facetFilters)
129+ }
130+ if let facetsDistribution = self . facetsDistribution, !facetsDistribution. isEmpty {
131+ try container. encode ( facetsDistribution, forKey: . facetsDistribution)
132+ }
133+ if matches {
134+ try container. encode ( matches, forKey: . matches)
135+ }
94136 }
95137
96138}
0 commit comments