Philipp Mayer
07/29/2020, 1:36 PMvar email: String? = "default"
set(value) {
field = setEmail(value)
}
fun setEmail(value: String?): String? {
val emailRegex = Regex("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$")
val isValidEmail = value != null && emailRegex.matches(value)
return if (isValidEmail) value else null
}
The shown field is part of a representation of an old legacy db, which might has a "wrong" email adress in it. Its set by jpa, but I would like to perform the shown operation when the field is set to filter out "wrong" emails.
The function is tested and is returning the correct result.
However, email is always set as "default". What did I do wrong here?
Thanks ahead!nschulzke
07/29/2020, 1:49 PMsetEmail
function, you never actually set the email. You need a statement such as email = value
, instead of just returning value
.
You also should be able to move the whole contents of the setEmail
function into the set(value)
function, though I'm not 100% sure JPA will use it (I'm pretty sure, though, because Kotlin names the generated get and set functions pretty predictably for precisely this reason).Philipp Mayer
07/29/2020, 1:51 PMfield = setEmail(value)
First, I had the method body in the setter, but I just moved it out of it to test the regex is working correctly.nschulzke
07/29/2020, 1:53 PMsetEmail
and passes in the email. Because you created a separate function named setEmail
, Kotlin probably didn't name your setter setEmail
, so it's that separate function that gets called, and that function never sets anything.Marshall
07/29/2020, 1:55 PMMarshall
07/29/2020, 1:56 PMPhilipp Mayer
07/29/2020, 1:56 PMclass TestEmployee {
var email: String? = "test"
private set(value) {
field = "setter"
}
}
@Test
fun `should set field correctly`() {
TestEmployee().email shouldBe "setter"
}
org.opentest4j.AssertionFailedError: expected:<"setter"> but was:<"test">
Philipp Mayer
07/29/2020, 1:58 PMnschulzke
07/29/2020, 1:58 PMclass TestEmployee {
var email: String? = "test"
private set(value) {
field = "setter"
}
}
@Test
fun `should set field correctly`() {
val emp = TestEmployee()
emp.email = "anythingYouSay"
emp.email shouldBe "setter"
}
nschulzke
07/29/2020, 1:59 PMemp.email = "something"
, your setter gets called with "something"
as the parameter named value
. In the setter above, the setter ignores the parameter, but typically you'd use it for something.nschulzke
07/29/2020, 2:00 PMmyEmployee.email = "notanemail"
the email
would just be null.nschulzke
07/29/2020, 2:05 PMPhilipp Mayer
07/29/2020, 2:06 PMprivate
. I didn't realize it until I tried to run your example and the IDE prompted me with the setter being private..nschulzke
07/29/2020, 2:07 PMPhilipp Mayer
07/29/2020, 2:07 PMPhilipp Mayer
07/29/2020, 2:08 PMMarshall
07/29/2020, 2:09 PMMarshall
07/29/2020, 2:09 PMJavier
07/29/2020, 2:20 PMsetEmail
...Philipp Mayer
07/29/2020, 2:34 PMMatteo Mirk
08/14/2020, 2:42 PMsetEmail()
function has a bad name, since it doesn’t set anything. A better name would be checkEmail
or validEmailOrNull
, for example.