Skip to content

Commit

Permalink
fix: numericAttributesToIndex deserialization (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Aug 2, 2021
1 parent ab766b0 commit e7ed32f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 62 deletions.
33 changes: 17 additions & 16 deletions src/main/scala/algolia/AlgoliaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import algolia.dsl._
import algolia.objects._
import org.json4s.JsonAST._
import org.json4s.JsonDSL._
import org.json4s.{CustomSerializer, Formats}
import org.json4s.{CustomSerializer, FieldSerializer, Formats, JField}

import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
import java.time.{Instant, LocalDateTime, ZoneOffset, ZonedDateTime}
Expand Down Expand Up @@ -77,7 +77,6 @@ object AlgoliaDsl extends AlgoliaDsl {
org.json4s.DefaultFormats +
new SearchableAttributesSerializer +
new AttributesToIndexSerializer +
new NumericAttributesToIndexSerializer +
new RankingSerializer +
new CustomRankingSerializer +
new QueryTypeSerializer +
Expand All @@ -90,7 +89,10 @@ object AlgoliaDsl extends AlgoliaDsl {
new IgnorePluralsSerializer +
new LocalDateTimeSerializer +
new ZonedDateTimeSerializer +
new AlternativesSerializer
new AlternativesSerializer +
new FieldSerializer[IndexSettings](
deserializer = numericAttributesToIndexDeserializer
)

val searchableAttributesUnordered: Regex = """^unordered\(([\w-\\.]+)\)$""".r
val searchableAttributesAttributes: Regex =
Expand Down Expand Up @@ -148,18 +150,6 @@ object AlgoliaDsl extends AlgoliaDsl {
})
)

class NumericAttributesToIndexSerializer
extends CustomSerializer[NumericAttributesToIndex](
_ =>
({
case JString(numericAttributesToIndexEqualOnly(attr)) =>
NumericAttributesToIndex.equalOnly(attr)
}, {
case NumericAttributesToIndex.equalOnly(attr) =>
JString(s"equalOnly($attr)")
})
)

class RankingSerializer
extends CustomSerializer[Ranking](
_ =>
Expand Down Expand Up @@ -412,6 +402,18 @@ object AlgoliaDsl extends AlgoliaDsl {
})
)

def numericAttributesToIndexDeserializer: PartialFunction[JField, JField] = {
case JField("numericAttributesToIndex", JArray(s: Seq[JValue])) =>
JField(
"numericAttributesForFiltering",
s.map(e =>
e.asInstanceOf[JString].s match {
case numericAttributesToIndexEqualOnly(attr) => attr
}
)
)
}

case object forwardToSlaves extends ForwardToReplicas

case object forwardToReplicas extends ForwardToReplicas
Expand All @@ -425,5 +427,4 @@ object AlgoliaDsl extends AlgoliaDsl {
case object in extends In

case object abTests extends ABTests

}
1 change: 0 additions & 1 deletion src/main/scala/algolia/objects/IndexSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ case class IndexSettings(
paginationLimitedTo: Option[Int] = None,
/* Performance */
numericAttributesForFiltering: Option[Seq[String]] = None,
numericAttributesToIndex: Option[Seq[NumericAttributesToIndex]] = None,
allowCompressionOfIntegerArray: Option[Boolean] = None,
/* Query rules */
enableRules: Option[Boolean] = None,
Expand Down
36 changes: 0 additions & 36 deletions src/main/scala/algolia/objects/NumericAttributesToIndex.scala

This file was deleted.

44 changes: 35 additions & 9 deletions src/test/scala/algolia/dsl/IndexSettingsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ class IndexSettingsTest extends AlgoliaTest {
| "attr2"
| ]
| },
| "numericAttributesToIndex":[
| "equalOnly(att5)"
| "numericAttributesForFiltering":[
| "att5",
| "att6"
| ],
| "removeStopWords":"fr,en",
| "ranking":[
Expand All @@ -124,7 +125,8 @@ class IndexSettingsTest extends AlgoliaTest {
|}""".stripMargin

it("should deserialize json") {
inside(parse(json).extract[IndexSettings]) {
val indexSettings = parse(json).extract[IndexSettings]
inside(indexSettings) {
case i: IndexSettings =>
i.attributesToIndex should be(
Some(
Expand All @@ -144,11 +146,9 @@ class IndexSettingsTest extends AlgoliaTest {
)
)
)
i.numericAttributesToIndex should be(
i.numericAttributesForFiltering should be(
Some(
Seq(
NumericAttributesToIndex.equalOnly("att5")
)
Seq("att5", "att6")
)
)
i.ranking should be(
Expand Down Expand Up @@ -205,8 +205,7 @@ class IndexSettingsTest extends AlgoliaTest {
SearchableAttributes.unordered("att4")
)
),
numericAttributesToIndex =
Some(Seq(NumericAttributesToIndex.equalOnly("att5"))),
numericAttributesForFiltering = Some(Seq("att5", "att6")),
ranking = Some(
Seq(
Ranking.typo,
Expand Down Expand Up @@ -238,6 +237,33 @@ class IndexSettingsTest extends AlgoliaTest {
writePretty(i) should be(json)
}

it("should deserialize legacy json") {
val jsonDeserialize =
"""{
| "numericAttributesToIndex":[
| "equalOnly(att5)",
| "equalOnly(att6)"
| ]
|}""".stripMargin

val indexSettings = parse(jsonDeserialize).extract[IndexSettings]
inside(indexSettings) {
case i: IndexSettings =>
i.numericAttributesForFiltering should be(
Some(Seq("att5", "att6"))
)
}

val jsonSerialize =
"""{
| "numericAttributesForFiltering":[
| "att5",
| "att6"
| ]
|}""".stripMargin

writePretty(indexSettings) should be(jsonSerialize)
}
}

}

0 comments on commit e7ed32f

Please sign in to comment.