Skip to content

Commit

Permalink
Add Kotlin value classes sample.
Browse files Browse the repository at this point in the history
Closes #670
  • Loading branch information
mp911de committed Oct 25, 2023
1 parent ffadb79 commit cdefade
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ interface PersonRepository : CrudRepository<Person, String> {
* 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]")
)
)

val walter = repository.findOneByEmail(EmailAddress("[email protected]"))

assertThat(walter.email?.address).isEqualTo("[email protected]")
}

@Test
fun `should return null if no person found`() {

Expand Down

0 comments on commit cdefade

Please sign in to comment.