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 private properties in Kotlin data classes #14

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -2,6 +2,7 @@

import kotlin.jvm.JvmClassMappingKt;
import kotlin.reflect.KCallable;
import kotlin.reflect.jvm.KCallablesJvm;
import kotlin.reflect.jvm.ReflectJvmMapping;
import tech.ydb.yoj.databind.FieldValueType;
import tech.ydb.yoj.databind.schema.Column;
Expand All @@ -26,6 +27,7 @@ public final class KotlinDataClassComponent implements ReflectField {

public KotlinDataClassComponent(Reflector reflector, String name, KCallable<?> callable) {
this.callable = callable;
KCallablesJvm.setAccessible(this.callable, true);

this.name = name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tech.ydb.yoj.databind.schema
import org.assertj.core.api.Assertions.assertThat
import org.junit.BeforeClass
import org.junit.Test
import java.util.Map.entry

class KotlinSchemaTest {
@Test
Expand Down Expand Up @@ -69,13 +70,21 @@ class KotlinSchemaTest {
assertThat(schema!!.getField("notFlatEntity").isFlat).isFalse()
}

@Test
fun testPrivateProp() {
val ue = UberEntity(null, null, null, null, PrivatePropEntity(42))
assertThat(schema!!.flatten(ue)).containsOnly(entry("privatePropEntity_privateProp", 42))
assertThat(schema!!.newInstance(mapOf("privatePropEntity_privateProp" to 42))).isEqualTo(ue)
}

private class TestSchema<T>(entityType: Class<T>) : Schema<T>(entityType)

private data class UberEntity(
val entity1: Entity1,
val flatEntity: FlatEntity,
val twoFieldEntity: TwoFieldEntity,
val notFlatEntity: NotFlatEntity,
val entity1: Entity1?,
val flatEntity: FlatEntity?,
val twoFieldEntity: TwoFieldEntity?,
val notFlatEntity: NotFlatEntity?,
val privatePropEntity: PrivatePropEntity
)

private data class Entity1(val entity2: Entity2)
Expand All @@ -96,6 +105,10 @@ class KotlinSchemaTest {
val otherTwoFieldEntity: TwoFieldEntity,
)

private data class PrivatePropEntity(
private val privateProp: Int
)

companion object {
private var schema: Schema<UberEntity>? = null

Expand Down
Loading