From cdefadefdd62f384385f2fb8dcf986d8dc01b036 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 25 Oct 2023 14:48:51 +0200 Subject: [PATCH] Add Kotlin value classes sample. Closes #670 --- .../springdata/mongodb/people/EmailAddress.kt | 24 +++++++++++++++++++ .../springdata/mongodb/people/Person.kt | 14 +++++++++-- .../mongodb/people/PersonRepository.kt | 2 ++ .../mongodb/people/RepositoryTests.kt | 17 +++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/EmailAddress.kt diff --git a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/EmailAddress.kt b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/EmailAddress.kt new file mode 100644 index 00000000..7361e152 --- /dev/null +++ b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/EmailAddress.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.mongodb.people + +/** + * @author Mark Paluch + */ +@JvmInline +value class EmailAddress(val address: String) { + +} diff --git a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt index 4b5d6d0a..d56ebed0 100644 --- a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt +++ b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt @@ -23,6 +23,16 @@ import org.springframework.data.annotation.PersistenceCreator * * @author Mark Paluch */ -data class Person @PersistenceCreator constructor(@Id val id: String?, val firstname: String? = "Walter", val lastname: String = "") { - constructor(firstname: String?, lastname: String) : this(null, firstname, lastname); +data class Person @PersistenceCreator constructor( + @Id val id: String?, + val firstname: String? = "Walter", + val lastname: String = "", + val email: EmailAddress? +) { + constructor(firstname: String?, lastname: String) : this( + null, + firstname, + lastname, + null + ); } diff --git a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt index 0aea533b..eaa727a3 100644 --- a/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt +++ b/mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt @@ -38,4 +38,6 @@ interface PersonRepository : CrudRepository { * Query method requiring a result. Throws [org.springframework.dao.EmptyResultDataAccessException] if no result is found. */ fun findOneByFirstname(firstname: String): Person + + fun findOneByEmail(emailAddress: EmailAddress): Person } diff --git a/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt b/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt index 2b854913..de847e42 100644 --- a/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt +++ b/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt @@ -68,6 +68,23 @@ class RepositoryTests { assertThat(walter.lastname).isEqualTo("White") } + @Test + fun `should find one person by email`() { + + repository.save( + Person( + null, + "Walter", + "White", + EmailAddress("heisenberg@gmail.com") + ) + ) + + val walter = repository.findOneByEmail(EmailAddress("heisenberg@gmail.com")) + + assertThat(walter.email?.address).isEqualTo("heisenberg@gmail.com") + } + @Test fun `should return null if no person found`() {