Skip to content

Commit

Permalink
Refactor ObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed May 4, 2023
1 parent 30f2de4 commit 55a7e1b
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 47 deletions.
4 changes: 2 additions & 2 deletions elasticmagic/api/elasticmagic.api
Original file line number Diff line number Diff line change
Expand Up @@ -4613,14 +4613,14 @@ public abstract class dev/evo/elasticmagic/types/NumberType : dev/evo/elasticmag

public class dev/evo/elasticmagic/types/ObjectType : dev/evo/elasticmagic/types/FieldType {
public fun <init> ()V
public fun deserialize (Ljava/lang/Object;)Ldev/evo/elasticmagic/doc/BaseDocSource;
public synthetic fun deserialize (Ljava/lang/Object;)Ljava/lang/Object;
public fun deserialize (Ljava/lang/Object;)Ljava/lang/Void;
public synthetic fun deserializeTerm (Ljava/lang/Object;)Ljava/lang/Object;
public fun deserializeTerm (Ljava/lang/Object;)Ljava/lang/Void;
public fun getName ()Ljava/lang/String;
public fun getTermType ()Lkotlin/reflect/KClass;
public fun serialize (Ldev/evo/elasticmagic/doc/BaseDocSource;)Ljava/util/Map;
public synthetic fun serialize (Ljava/lang/Object;)Ljava/lang/Object;
public fun serialize (Ljava/lang/Void;)Ljava/util/Map;
public synthetic fun serializeTerm (Ljava/lang/Object;)Ljava/lang/Object;
public fun serializeTerm (Ljava/lang/Void;)Ljava/lang/Void;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ abstract class BaseDocument : FieldSet() {

fun <T: SubDocument> `object`(
name: String?,
factory: (DocSourceField) -> T,
factory: (ObjectBoundField) -> T,
enabled: Boolean? = null,
dynamic: Dynamic? = null,
params: Params = Params(),
Expand All @@ -545,7 +545,7 @@ abstract class BaseDocument : FieldSet() {
}

fun <T: SubDocument> `object`(
factory: (DocSourceField) -> T,
factory: (ObjectBoundField) -> T,
enabled: Boolean? = null,
dynamic: Dynamic? = null,
params: Params = Params(),
Expand All @@ -555,7 +555,7 @@ abstract class BaseDocument : FieldSet() {

fun <T: SubDocument> obj(
name: String?,
factory: (DocSourceField) -> T,
factory: (ObjectBoundField) -> T,
enabled: Boolean? = null,
dynamic: Dynamic? = null,
params: Params = Params(),
Expand All @@ -564,7 +564,7 @@ abstract class BaseDocument : FieldSet() {
}

fun <T: SubDocument> obj(
factory: (DocSourceField) -> T,
factory: (ObjectBoundField) -> T,
enabled: Boolean? = null,
dynamic: Dynamic? = null,
params: Params = Params(),
Expand All @@ -574,7 +574,7 @@ abstract class BaseDocument : FieldSet() {

fun <T: SubDocument> nested(
name: String?,
factory: (DocSourceField) -> T,
factory: (ObjectBoundField) -> T,
dynamic: Dynamic? = null,
params: Params = Params()
): SubDocument.UnboundSubDocument<T> {
Expand All @@ -587,43 +587,43 @@ abstract class BaseDocument : FieldSet() {
}

fun <T: SubDocument> nested(
factory: (DocSourceField) -> T,
factory: (ObjectBoundField) -> T,
dynamic: Dynamic? = null,
params: Params = Params()
): SubDocument.UnboundSubDocument<T> {
return nested(null, factory, dynamic, params)
}
}

typealias DocSourceField = BoundField<BaseDocSource, Nothing>
typealias ObjectBoundField = BoundField<Nothing, Nothing>

/**
* Represents Elasticsearch sub-document.
*/
@Suppress("UnnecessaryAbstractClass")
abstract class SubDocument(
private val field: DocSourceField,
private val field: ObjectBoundField,
dynamic: Dynamic? = null,
) : BaseDocument(), FieldOperations<Nothing> {
val options: MappingOptions = MappingOptions(
dynamic = dynamic,
)

fun getBoundField(): DocSourceField = field
fun getBoundField(): ObjectBoundField = field

override fun getFieldName(): String = field.getFieldName()

override fun getQualifiedFieldName(): String = field.getQualifiedFieldName()

override fun getFieldType(): FieldType<BaseDocSource, Nothing> = field.getFieldType()
override fun getFieldType(): FieldType<Nothing, Nothing> = field.getFieldType()

fun getParent(): FieldSet = field.getParent()

class UnboundSubDocument<T: SubDocument>(
private val name: String?,
internal val type: ObjectType,
internal val params: Params,
internal val subDocumentFactory: (DocSourceField) -> T,
internal val subDocumentFactory: (ObjectBoundField) -> T,
) {
operator fun provideDelegate(
thisRef: BaseDocument, prop: KProperty<*>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,19 +547,19 @@ object JoinType : FieldType<Join, String> {
*
* See: <https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html>
*/
open class ObjectType : FieldType<BaseDocSource, Nothing> {
open class ObjectType : FieldType<Nothing, Nothing> {
override val name = "object"
override val termType = Nothing::class

override fun serialize(v: BaseDocSource): Map<String, Any?> {
return v.toSource()
override fun serialize(v: Nothing): Map<String, Any?> {
throw IllegalStateException("Unreachable: ${this::class}::serialize")
}

override fun serializeTerm(v: Nothing): Nothing {
throw IllegalStateException("Unreachable: ${this::class}::serializeTerm")
}

override fun deserialize(v: Any): BaseDocSource {
override fun deserialize(v: Any): Nothing {
throw IllegalStateException("Unreachable: ${this::class}::deserialize")
}

Expand All @@ -582,14 +582,14 @@ class NestedType : ObjectType() {
* Used by [dev.evo.elasticmagic.doc.DocSource].
*/
internal class SourceType<V: BaseDocSource>(
val type: FieldType<BaseDocSource, Nothing>,
val type: FieldType<Nothing, Nothing>,
private val sourceFactory: () -> V
) : FieldType<V, Nothing> {
override val name = type.name
override val termType = Nothing::class

override fun serialize(v: V): Any {
return type.serialize(v)
return v.toSource()
}

override fun deserialize(v: Any): V {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.evo.elasticmagic.compile
import dev.evo.elasticmagic.BaseTest
import dev.evo.elasticmagic.Params
import dev.evo.elasticmagic.doc.BoundField
import dev.evo.elasticmagic.doc.DocSourceField
import dev.evo.elasticmagic.doc.ObjectBoundField
import dev.evo.elasticmagic.doc.Document
import dev.evo.elasticmagic.doc.Dynamic
import dev.evo.elasticmagic.types.KeywordType
Expand Down Expand Up @@ -104,11 +104,11 @@ class MappingCompilerTests : BaseTest() {

@Test
fun subDocument() {
class OpinionDoc(field: DocSourceField) : SubDocument(field) {
class OpinionDoc(field: ObjectBoundField) : SubDocument(field) {
val count by int()
}

class CompanyDoc(field: DocSourceField) : SubDocument(field) {
class CompanyDoc(field: ObjectBoundField) : SubDocument(field) {
val name by text(analyzer = "standard")
val opinion by obj(
::OpinionDoc, dynamic = Dynamic.TRUE, params = Params("enabled" to false)
Expand Down Expand Up @@ -292,7 +292,7 @@ class MappingCompilerTests : BaseTest() {

@Test
fun dynamicTemplates_subDocument() {
class UserDoc(field: DocSourceField) : SubDocument(field) {
class UserDoc(field: ObjectBoundField) : SubDocument(field) {
val id by keyword()
val firstName by text("first_name", analyzer="en")
val lastName by text("last_name", analyzer="en")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import dev.evo.elasticmagic.aggs.DateHistogramAgg
import dev.evo.elasticmagic.aggs.FixedInterval
import dev.evo.elasticmagic.aggs.MinAgg
import dev.evo.elasticmagic.aggs.TermsAgg
import dev.evo.elasticmagic.doc.BaseDocSource
import dev.evo.elasticmagic.doc.BoundField
import dev.evo.elasticmagic.doc.BoundRuntimeField
import dev.evo.elasticmagic.doc.Document
import dev.evo.elasticmagic.doc.RootFieldSet
import dev.evo.elasticmagic.types.SimpleFieldType
import dev.evo.elasticmagic.doc.SubDocument
import dev.evo.elasticmagic.doc.datetime
import dev.evo.elasticmagic.doc.ObjectBoundField
import dev.evo.elasticmagic.doc.SubDocument
import dev.evo.elasticmagic.query.Bool
import dev.evo.elasticmagic.query.DisMax
import dev.evo.elasticmagic.query.FieldFormat
Expand Down Expand Up @@ -271,11 +271,11 @@ class SearchQueryCompilerTests : BaseCompilerTest<SearchQueryCompiler>(::SearchQ

@Test
fun filter() = testWithCompiler {
class OpinionDoc(field: BoundField<BaseDocSource, Nothing>) : SubDocument(field) {
class OpinionDoc(field: ObjectBoundField) : SubDocument(field) {
val count by int()
}

class CompanyDoc(field: BoundField<BaseDocSource, Nothing>) : SubDocument(field) {
class CompanyDoc(field: ObjectBoundField) : SubDocument(field) {
val name by text()
val opinion by obj(::OpinionDoc)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import kotlin.test.Test

class DocSourceTests {
object OrderDoc : Document() {
class UserOpinionDoc(field: DocSourceField) : SubDocument(field) {
class UserOpinionDoc(field: ObjectBoundField) : SubDocument(field) {
val rating by float("rank")
}

class UserDoc(field: DocSourceField) : SubDocument(field) {
class UserDoc(field: ObjectBoundField) : SubDocument(field) {
val id by int()
val opinion by obj(::UserOpinionDoc)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class DocumentTests {
val sort by keyword()
}

class UserDoc(field: DocSourceField) : SubDocument(field) {
class UserDoc(field: ObjectBoundField) : SubDocument(field) {
val firstName by text("first_name")
val lastName by text("last_name")
}
Expand Down Expand Up @@ -307,11 +307,11 @@ class DocumentTests {

@Test
fun testSubDocument() {
class OpinionDoc(field: DocSourceField) : SubDocument(field) {
class OpinionDoc(field: ObjectBoundField) : SubDocument(field) {
val count by int()
}

class CompanyDoc(field: DocSourceField) : SubDocument(field) {
class CompanyDoc(field: ObjectBoundField) : SubDocument(field) {
val name by text()
val opinion by obj(::OpinionDoc)
}
Expand Down Expand Up @@ -342,7 +342,7 @@ class DocumentTests {

@Test
fun testSubDocument_preventDoubleInitialization() {
class CompanyDoc(field: DocSourceField) : SubDocument(field)
class CompanyDoc(field: ObjectBoundField) : SubDocument(field)

val myDoc = object : Document() {
val company by obj(::CompanyDoc)
Expand All @@ -364,7 +364,7 @@ class DocumentTests {
val sort by keyword()
}

class OpinionUserDoc(field: DocSourceField) : SubDocument(field) {
class OpinionUserDoc(field: ObjectBoundField) : SubDocument(field) {
val title by text().subFields(::OpinionNameFields)
val phone by text()
}
Expand All @@ -380,7 +380,7 @@ class DocumentTests {
val autocomplete by text()
}

class AnswerUserDoc(field: DocSourceField) : SubDocument(field) {
class AnswerUserDoc(field: ObjectBoundField) : SubDocument(field) {
val title by text().subFields(::AnswerNameFields)
val companyId by int()
}
Expand Down Expand Up @@ -502,7 +502,7 @@ class DocumentTests {

@Test
fun testMergeDocuments_mergeObjectWithNested() {
class OpinionDoc(field: DocSourceField) : SubDocument(field) {
class OpinionDoc(field: ObjectBoundField) : SubDocument(field) {
val stars by float()
}

Expand Down Expand Up @@ -553,7 +553,7 @@ class DocumentTests {

@Test
fun testMergeDocuments_subDocumentsWithDifferentFieldParams() {
class OpinionDoc(field: DocSourceField) : SubDocument(field) {
class OpinionDoc(field: ObjectBoundField) : SubDocument(field) {
val stars by float()
}

Expand Down Expand Up @@ -693,12 +693,12 @@ class DocumentTests {

@Test
fun testMergeDocuments_differentDynamicTemplates() {
class UserOpinionDoc(field: DocSourceField) : SubDocument(field) {
class UserOpinionDoc(field: ObjectBoundField) : SubDocument(field) {
val count by int()
val rank by float()
}

class CompanyOpinionDoc(field: DocSourceField) : SubDocument(field) {
class CompanyOpinionDoc(field: ObjectBoundField) : SubDocument(field) {
val count by int()
val rank by float()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package dev.evo.elasticmagic.query
import dev.evo.elasticmagic.BaseTest
import dev.evo.elasticmagic.compile.ElasticsearchFeatures
import dev.evo.elasticmagic.compile.SearchQueryCompiler
import dev.evo.elasticmagic.doc.*
import dev.evo.elasticmagic.doc.date
import dev.evo.elasticmagic.doc.Document
import dev.evo.elasticmagic.doc.ObjectBoundField
import dev.evo.elasticmagic.doc.SubDocument
import dev.evo.elasticmagic.serde.Serializer
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
Expand Down Expand Up @@ -35,13 +38,12 @@ abstract class BaseExpressionTest : BaseTest() {
clone shouldBe expression
}


protected class YearDoc(field: BoundField<BaseDocSource, Nothing>) : SubDocument(field) {
protected class YearDoc(field: ObjectBoundField) : SubDocument(field) {
val year by int()
val join by join(relations = mapOf("year" to listOf("movie")))
}

protected class StarDoc(field: BoundField<BaseDocSource, Nothing>) : SubDocument(field) {
protected class StarDoc(field: ObjectBoundField) : SubDocument(field) {
val name by text()
val rank by float()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import dev.evo.elasticmagic.aggs.NestedAgg
import dev.evo.elasticmagic.aggs.SingleBucketAggResult
import dev.evo.elasticmagic.aggs.TermsAgg
import dev.evo.elasticmagic.aggs.TermsAggResult
import dev.evo.elasticmagic.doc.BaseDocSource
import dev.evo.elasticmagic.doc.BoundField
import dev.evo.elasticmagic.doc.DocSource
import dev.evo.elasticmagic.doc.Document
import dev.evo.elasticmagic.doc.ObjectBoundField
import dev.evo.elasticmagic.bulk.DocSourceAndMeta
import dev.evo.elasticmagic.bulk.withActionMeta
import dev.evo.elasticmagic.doc.DynDocSource
Expand Down Expand Up @@ -54,14 +53,14 @@ enum class OrderStatus(val id: Int) : ToValue<Int> {
}

object OrderDoc : Document() {
class User(field: BoundField<BaseDocSource, Nothing>) : SubDocument(field) {
class User(field: ObjectBoundField) : SubDocument(field) {
val id by int()
val name by text()
val phone by keyword()
val rating by float()
}

class CartItem(field: BoundField<BaseDocSource, Nothing>) : SubDocument(field) {
class CartItem(field: ObjectBoundField) : SubDocument(field) {
val productId by long("product_id")
val productName by text("product_name")
val productPrice by float("product_price")
Expand Down

0 comments on commit 55a7e1b

Please sign in to comment.