@@ -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
@@ -34,7 +34,6 @@ public struct SearchParameters: Codable, Equatable {
3434 public let filters : String ?
3535
3636 /// Select which attribute has to be filtered, useful when you need to narrow down the results of the filter.
37- //TODO: Migrate to FacetFilter object
3837 public let facetFilters : [ [ String ] ] ?
3938
4039 /// Retrieve the count of matching terms for each facets.
@@ -47,11 +46,11 @@ public struct SearchParameters: Codable, Equatable {
4746
4847 init (
4948 query: String ,
50- offset: Int = 0 ,
51- limit: Int = 20 ,
49+ offset: Int = Default . offset . rawValue ,
50+ limit: Int = Default . limit . rawValue ,
5251 attributesToRetrieve: [ String ] ? = nil ,
5352 attributesToCrop: [ String ] = [ ] ,
54- cropLength: Int = 200 ,
53+ cropLength: Int = Default . cropLength . rawValue ,
5554 attributesToHighlight: [ String ] = [ ] ,
5655 filters: String ? = nil ,
5756 facetFilters: [ [ String ] ] ? = nil ,
@@ -82,15 +81,70 @@ public struct SearchParameters: Codable, Equatable {
8281 SearchParameters ( query: value)
8382 }
8483
85- private func commaRepresentation( _ array: [ String ] ) -> String {
86- array. joined ( separator: " , " )
84+ // MARK: Codable Keys
85+
86+ enum CodingKeys : String , CodingKey {
87+ case query = " q "
88+ case offset
89+ case limit
90+ case attributesToRetrieve
91+ case attributesToCrop
92+ case cropLength
93+ case attributesToHighlight
94+ case filters
95+ case facetFilters
96+ case facetsDistribution
97+ case matches
8798 }
8899
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
100+ // MARK: Default value for keys
101+
102+ fileprivate enum Default : Int {
103+ case offset = 0
104+ case limit = 20
105+ case cropLength = 200
106+ }
107+
108+ }
109+
110+ extension SearchParameters {
111+
112+ // MARK: Codable
113+
114+ /// Encodes the `SearchParameters` to a JSON payload, removing any non necessary implicit parameter.
115+ public func encode( to encoder: Encoder ) throws {
116+ var container = encoder. container ( keyedBy: CodingKeys . self)
117+ try container. encode ( query, forKey: . query)
118+ if limit != Default . limit. rawValue {
119+ try container. encode ( limit, forKey: . limit)
120+ }
121+ if offset != Default . offset. rawValue {
122+ try container. encode ( offset, forKey: . offset)
123+ }
124+ if let attributesToRetrieve: [ String ] = self . attributesToRetrieve, !attributesToRetrieve. isEmpty {
125+ try container. encode ( attributesToRetrieve, forKey: . attributesToRetrieve)
126+ }
127+ if !attributesToCrop. isEmpty {
128+ try container. encode ( attributesToCrop, forKey: . attributesToCrop)
129+ }
130+ if cropLength != Default . cropLength. rawValue {
131+ try container. encode ( cropLength, forKey: . cropLength)
132+ }
133+ if !attributesToHighlight. isEmpty {
134+ try container. encode ( attributesToHighlight, forKey: . attributesToHighlight)
135+ }
136+ if let filters: String = self . filters, !filters. isEmpty {
137+ try container. encode ( filters, forKey: . filters)
138+ }
139+ if let facetFilters: [ [ String ] ] = self . facetFilters {
140+ try container. encode ( facetFilters, forKey: . facetFilters)
141+ }
142+ if let facetsDistribution = self . facetsDistribution, !facetsDistribution. isEmpty {
143+ try container. encode ( facetsDistribution, forKey: . facetsDistribution)
144+ }
145+ if matches {
146+ try container. encode ( matches, forKey: . matches)
147+ }
94148 }
95149
96150}
0 commit comments