Skip to content

Commit cdefade

Browse files
committed
Add Kotlin value classes sample.
Closes #670
1 parent ffadb79 commit cdefade

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.people
17+
18+
/**
19+
* @author Mark Paluch
20+
*/
21+
@JvmInline
22+
value class EmailAddress(val address: String) {
23+
24+
}

mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/Person.kt

+12-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ import org.springframework.data.annotation.PersistenceCreator
2323
*
2424
* @author Mark Paluch
2525
*/
26-
data class Person @PersistenceCreator constructor(@Id val id: String?, val firstname: String? = "Walter", val lastname: String = "") {
27-
constructor(firstname: String?, lastname: String) : this(null, firstname, lastname);
26+
data class Person @PersistenceCreator constructor(
27+
@Id val id: String?,
28+
val firstname: String? = "Walter",
29+
val lastname: String = "",
30+
val email: EmailAddress?
31+
) {
32+
constructor(firstname: String?, lastname: String) : this(
33+
null,
34+
firstname,
35+
lastname,
36+
null
37+
);
2838
}

mongodb/kotlin/src/main/kotlin/example/springdata/mongodb/people/PersonRepository.kt

+2
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ interface PersonRepository : CrudRepository<Person, String> {
3838
* Query method requiring a result. Throws [org.springframework.dao.EmptyResultDataAccessException] if no result is found.
3939
*/
4040
fun findOneByFirstname(firstname: String): Person
41+
42+
fun findOneByEmail(emailAddress: EmailAddress): Person
4143
}

mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/RepositoryTests.kt

+17
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ class RepositoryTests {
6868
assertThat(walter.lastname).isEqualTo("White")
6969
}
7070

71+
@Test
72+
fun `should find one person by email`() {
73+
74+
repository.save(
75+
Person(
76+
null,
77+
"Walter",
78+
"White",
79+
EmailAddress("[email protected]")
80+
)
81+
)
82+
83+
val walter = repository.findOneByEmail(EmailAddress("[email protected]"))
84+
85+
assertThat(walter.email?.address).isEqualTo("[email protected]")
86+
}
87+
7188
@Test
7289
fun `should return null if no person found`() {
7390

0 commit comments

Comments
 (0)