Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Elastic primitive for UUID #183

Merged
merged 5 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package zio.elasticsearch
import zio.json.ast.Json
import zio.json.ast.Json.{Num, Str}

import java.util.UUID

object ElasticPrimitive {
sealed trait ElasticPrimitive[A] {
def toJson(value: A): Json
Expand Down Expand Up @@ -48,6 +50,10 @@ object ElasticPrimitive {
def toJson(value: String): Json = Str(value)
}

implicit object ElasticUUID extends ElasticPrimitive[UUID] {
def toJson(value: UUID): Json = Str(value.toString)
}

final implicit class ElasticPrimitiveOps[A](private val value: A) extends AnyVal {
def toJson(implicit EP: ElasticPrimitive[A]): Json = EP.toJson(value)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package zio.elasticsearch

import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.query.Match
import zio.test.Assertion.equalTo
import zio.test._

import java.util.UUID

object ElasticPrimitivesSpec extends ZIOSpecDefault {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
object ElasticPrimitivesSpec extends ZIOSpecDefault {
object ElasticPrimitiveSpec extends ZIOSpecDefault {


private val fieldName = "fieldName"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private val fieldName = "fieldName"
private val FieldName = "fieldName"


override def spec: Spec[TestEnvironment, Any] =
suite("Elastic Primitives")(
test("successfully create matches query with String") {
assert(matches(fieldName, "string"))(equalTo(Match[Any, String](fieldName, "string", boost = None)))
},
test("successfully create matches query with BigDecimal") {
assert(matches(fieldName, BigDecimal(1)))(
equalTo(Match[Any, BigDecimal](fieldName, BigDecimal(1), boost = None))
)
},
test("successfully create matches query with Boolean") {
assert(matches(fieldName, true))(equalTo(Match[Any, Boolean](fieldName, true, boost = None)))
},
test("successfully create matches query with Double") {
assert(matches(fieldName, 1.00))(equalTo(Match[Any, Double](fieldName, 1.00, boost = None)))
},
test("successfully create matches query with Int") {
assert(matches(fieldName, 1))(equalTo(Match[Any, Int](fieldName, 1, boost = None)))
},
test("successfully create matches query with Long") {
assert(matches(fieldName, 1L))(equalTo(Match[Any, Long](fieldName, 1L, boost = None)))
},
test("successfully create matches query with UUID") {
val uuid = UUID.randomUUID()
assert(matches(fieldName, uuid))(equalTo(Match[Any, UUID](fieldName, uuid, boost = None)))
}
)
}