Skip to content

Commit

Permalink
adds: Alternatives in QueryRule Conditions
Browse files Browse the repository at this point in the history
Alternatives may transform into JSON object in the future.

That's why it comes with a customer serializer and
an abstract factory.

Fix #542
[changelog]
  • Loading branch information
Ant-hem committed Aug 14, 2019
1 parent fa593ab commit c977cbb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/main/scala/algolia/AlgoliaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ object AlgoliaDsl extends AlgoliaDsl {
new RemoveStopWordsSerializer +
new IgnorePluralsSerializer +
new LocalDateTimeSerializer +
new ZonedDateTimeSerializer
new ZonedDateTimeSerializer +
new AlternativesSerializer

val searchableAttributesUnordered: Regex = """^unordered\(([\w-]+)\)$""".r
val searchableAttributesAttributes: Regex =
Expand Down Expand Up @@ -316,6 +317,16 @@ object AlgoliaDsl extends AlgoliaDsl {
case RemoveStopWords.list(i) => JString(i.mkString(","))
}))

class AlternativesSerializer
extends CustomSerializer[Alternatives](_ =>
({
case JBool(true) => Alternatives.`true`
case JBool(false) => Alternatives.`false`
}, {
case Alternatives.`true` => JBool(true)
case Alternatives.`false` => JBool(false)
}))

class IgnorePluralsSerializer
extends CustomSerializer[IgnorePlurals](_ =>
({
Expand Down
42 changes: 42 additions & 0 deletions src/main/scala/algolia/objects/Alternatives.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Algolia
* http://www.algolia.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package algolia.objects

trait Alternatives {

val value: Any

}

object Alternatives {
case object `true` extends Alternatives {
override val value = true
}

case object `false` extends Alternatives {
override val value = false
}
}
5 changes: 4 additions & 1 deletion src/main/scala/algolia/objects/Rule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ case class Rule(objectID: String,
validity: Option[Iterable[TimeRange]] = None,
description: Option[String] = None)

case class Condition(pattern: String, anchoring: String, context: Option[String] = None)
case class Condition(pattern: String,
anchoring: String,
context: Option[String] = None,
alternatives: Option[Alternatives] = None)

case class Consequence(params: Option[Map[String, Any]] = None,
promote: Option[Iterable[ConsequencePromote]] = None,
Expand Down

0 comments on commit c977cbb

Please sign in to comment.