-
Notifications
You must be signed in to change notification settings - Fork 788
/
QueryOps.scala
251 lines (215 loc) · 9.74 KB
/
QueryOps.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright 2013 http4s.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.http4s
trait QueryOps {
protected type Self <: QueryOps
protected val query: Query
protected def self: Self
protected def replaceQuery(query: Query): Self
/** alias for containsQueryParam */
def ?[K: QueryParamKeyLike](name: K): Boolean =
_containsQueryParam(QueryParamKeyLike[K].getKey(name))
/** alias for setQueryParams */
def =?[T: QueryParamEncoder](q: Map[String, List[T]]): Self =
setQueryParams(q)
/** alias for withQueryParam */
def +?[T: QueryParam]: Self =
_withQueryParam(QueryParam[T].key, Nil)
/** alias for withQueryParam */
def +*?[T: QueryParamKeyLike: QueryParamEncoder](value: T): Self =
_withQueryParam(QueryParamKeyLike[T].getKey(value), QueryParamEncoder[T].encode(value) :: Nil)
/** alias for withQueryParam */
def +*?[T: QueryParam: QueryParamEncoder](values: collection.Seq[T]): Self =
_withQueryParam(QueryParam[T].key, values.map(QueryParamEncoder[T].encode))
/** alias for withQueryParam */
def +?[K: QueryParamKeyLike, T: QueryParamEncoder](param: (K, T)): Self =
++?((param._1, param._2 :: Nil))
/** alias for withQueryParam */
def +?[K: QueryParamKeyLike](name: K): Self =
_withQueryParam(QueryParamKeyLike[K].getKey(name), Nil)
/** alias for withQueryParam
*
* {{{
* scala> import org.http4s.implicits._
* scala> uri"www.scala.com".++?("key" -> List("value1", "value2", "value3"))
* res1: Uri = www.scala.com?key=value1&key=value2&key=value3
* }}}
*/
def ++?[K: QueryParamKeyLike, T: QueryParamEncoder](param: (K, collection.Seq[T])): Self =
_withQueryParam(
QueryParamKeyLike[K].getKey(param._1),
param._2.map(QueryParamEncoder[T].encode),
)
/** alias for withOptionQueryParam */
def +??[K: QueryParamKeyLike, T: QueryParamEncoder](param: (K, Option[T])): Self =
_withOptionQueryParam(
QueryParamKeyLike[K].getKey(param._1),
param._2.map(QueryParamEncoder[T].encode),
)
/** alias for withOptionQueryParam */
def +??[T: QueryParam: QueryParamEncoder](value: Option[T]): Self =
_withOptionQueryParam(QueryParam[T].key, value.map(QueryParamEncoder[T].encode))
/** alias for removeQueryParam */
def -?[T](implicit key: QueryParam[T]): Self =
_removeQueryParam(key.key)
/** alias for removeQueryParam */
def -?[K: QueryParamKeyLike](key: K): Self =
_removeQueryParam(QueryParamKeyLike[K].getKey(key))
/** Checks if a specified parameter exists in the [[Query]]. A parameter
* without a name can be checked with an empty string.
*/
def containsQueryParam[T](implicit key: QueryParam[T]): Boolean =
_containsQueryParam(key.key)
def containsQueryParam[K: QueryParamKeyLike](key: K): Boolean =
_containsQueryParam(QueryParamKeyLike[K].getKey(key))
private def _containsQueryParam(name: QueryParameterKey): Boolean =
if (query.isEmpty) false
else query.exists { case (k, _) => k == name.value }
/** Creates maybe a new `Self` without the specified parameter in query.
* If no parameter with the given `key` exists then `this` will be
* returned.
*/
def removeQueryParam[K: QueryParamKeyLike](key: K): Self =
_removeQueryParam(QueryParamKeyLike[K].getKey(key))
private def _removeQueryParam(name: QueryParameterKey): Self =
if (query.isEmpty) self
else {
val newQuery = query.filterNot { case (n, _) => n == name.value }
replaceQuery(newQuery)
}
/** Creates maybe a new `Self` with the specified parameters. The entire
* [[Query]] will be replaced with the given one.
*/
def setQueryParams[K: QueryParamKeyLike, T: QueryParamEncoder](
params: Map[K, collection.Seq[T]]
): Self = {
val penc = QueryParamKeyLike[K]
val venc = QueryParamEncoder[T]
val vec = params.foldLeft(query.toVector) {
case (m, (k, Seq())) => m :+ (penc.getKey(k).value -> None)
case (m, (k, vs)) =>
vs.foldLeft(m) { case (m, v) => m :+ (penc.getKey(k).value -> Some(venc.encode(v).value)) }
}
replaceQuery(Query.fromVector(vec))
}
/** Creates a new `Self` with the specified parameter in the [[Query]].
* If a parameter with the given `QueryParam.key` already exists the values will be
* replaced with an empty list.
*/
def withQueryParam[T: QueryParam]: Self =
_withQueryParam(QueryParam[T].key, Nil)
/** Creates a new `Self` with the specified parameter in the [[Query]].
* If a parameter with the given `key` already exists the values will be
* replaced with an empty list.
*/
def withQueryParam[K: QueryParamKeyLike](key: K): Self =
_withQueryParam(QueryParamKeyLike[K].getKey(key), Nil)
/** Creates maybe a new `Self` with the specified parameter in the [[Query]].
* If a parameter with the given `key` already exists the values will be
* replaced. If the parameter to be added equal the existing entry the same
* instance of `Self` will be returned.
*/
def withQueryParam[T: QueryParamEncoder, K: QueryParamKeyLike](key: K, value: T): Self =
_withQueryParam(QueryParamKeyLike[K].getKey(key), QueryParamEncoder[T].encode(value) :: Nil)
/** Creates maybe a new `Self` with the specified parameters in the [[Query]].
* If a parameter with the given `key` already exists the values will be
* replaced.
*/
def withQueryParam[T: QueryParamEncoder, K: QueryParamKeyLike](
key: K,
values: collection.Seq[T],
): Self =
_withQueryParam(QueryParamKeyLike[K].getKey(key), values.map(QueryParamEncoder[T].encode))
/** Creates maybe a new `Self` with all the specified parameters in the
* [[Query]]. If any of the given parameters' keys already exists, the
* value(s) will be replaced. Parameters from the input map are added
* left-to-right, so if a parameter with a given key is specified more than
* once, it will be self-overwriting.
*/
def withQueryParams[T: QueryParamEncoder, K: QueryParamKeyLike](params: Map[K, T]): Self =
params.foldLeft(self) { case (s, (k, v)) =>
replaceQuery(Query.fromVector(s.withQueryParam(k, v).query.toVector))
}
/** Creates maybe a new `Self` with all the specified parameters in the
* [[Query]]. If any of the given parameters' keys already exists, the
* value(s) will be replaced. Parameters from the input map are added
* left-to-right, so if a parameter with a given key is specified more than
* once, it will be self-overwriting.
*/
def withMultiValueQueryParams[T: QueryParamEncoder, K: QueryParamKeyLike](
params: Map[K, collection.Seq[T]]
): Self =
params.foldLeft(self) { case (s, (k, v)) =>
replaceQuery(Query.fromVector(s.withQueryParam(k, v).query.toVector))
}
private def _withQueryParam(
name: QueryParameterKey,
values: collection.Seq[QueryParameterValue],
): Self = {
val q = if (query == Query.blank) Query.empty else query
val baseQuery = q.toVector.filter(_._1 != name.value)
val vec =
if (values.isEmpty) baseQuery :+ (name.value -> None)
else
values.toList.foldLeft(baseQuery) { case (vec, v) =>
vec :+ (name.value -> Some(v.value))
}
replaceQuery(Query.fromVector(vec))
}
/*
/**
* Creates maybe a new `Self` with the specified parameter in the [[Query]].
* If the value is empty the same instance of `Self` will be returned.
* If a parameter with the given `key` already exists the values will be
* replaced.
*/
def withMaybeQueryParam[T: QueryParamEncoder, K: QueryParamKeyLike](key: K, value: Maybe[T]): Self =
_withMaybeQueryParam(QueryParamKeyLike[K].getKey(key), value map QueryParamEncoder[T].encode)
/**
* Creates maybe a new `Self` with the specified parameter in the [[Query]].
* If the value is empty or if the parameter to be added equal the existing
* entry the same instance of `Self` will be returned.
* If a parameter with the given `name` already exists the values will be
* replaced.
*/
def withMaybeQueryParam[T: QueryParam: QueryParamEncoder](value: Maybe[T]): Self =
_withMaybeQueryParam(QueryParam[T].key, value map QueryParamEncoder[T].encode)
*/
/** Creates maybe a new `Self` with the specified parameter in the [[Query]].
* If the value is empty or if the parameter to be added equal the existing
* entry the same instance of `Self` will be returned.
* If a parameter with the given `key` already exists the values will be
* replaced.
*/
def withOptionQueryParam[T: QueryParamEncoder, K: QueryParamKeyLike](
key: K,
value: Option[T],
): Self =
_withOptionQueryParam(QueryParamKeyLike[K].getKey(key), value.map(QueryParamEncoder[T].encode))
/** Creates maybe a new `Self` with the specified parameter in the [[Query]].
* If the value is empty or if the parameter to be added equal the existing
* entry the same instance of `Self` will be returned.
* If a parameter with the given `name` already exists the values will be
* replaced.
*/
def withOptionQueryParam[T: QueryParam: QueryParamEncoder](value: Option[T]): Self =
_withOptionQueryParam(QueryParam[T].key, value.map(QueryParamEncoder[T].encode))
private def _withOptionQueryParam(
name: QueryParameterKey,
value: Option[QueryParameterValue],
): Self =
value.fold(self)(v => _withQueryParam(name, v :: Nil))
}