Kotlin: lambda run alternative scenario - kotlin

I have userDto, contains programs, which contains actual field. Actual program can be only one. I need to get it. Than, I run this:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?
Okay, but I want to foresee case, when findLast returns null, & throw exception. Please, advice, how to do it?
UPD:
ProgramType.valueOf(userDto.programs
.sortedBy { it.created }
.findLast { it.actual }
//check here
!!.programType!!).percentage

You are pretty close actually :)! What you could do is:
userDto.programs.sortedBy { it.created }.findLast { it.actual } ?: throw RuntimeException()
Or if you're trying to actually avoid throwing an error(couldn't really tell with the way question is asked), you could just do an error check like this:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?.let{
//rest of your code goes here
}
Hope this helps, cheers!

Related

C#-like case when in kotlin?

I come from a C# background and one really nice thing they offer is a way to add conditionals to a switch statement
switch(type)
{
case "edit":
// do something...
break;
case "delete" when user.isAdmin:
// do something...
break;
}
I'm curious if Kotlin has such an ability with their equivalent when? I'm not seeing that it's possible but I'm also still getting the hang of Kotlin so maybe there is a way.
I figured it out, guess I should have just asked Android studio XD
for those wondering, heres how:
when {
type == "edit" -> {
// do sometihng...
}
type == "delete" && user.isAdmin -> {
// do sometihng...
}
}
hope this helped someone else.

Implement retry logic with Mutiny

I'm just learning Mutiny and I need to implement retry logic.
I have this code:
fun main() {
getResult()
.onFailure().invoke { t -> println("Got error: $t") }
.onFailure().retry().atMost(2)
.subscribe().with(
{ result -> println(result) },
{ t -> t.printStackTrace() }
)
}
fun getResult(): Uni<String?> {
println("Preparing result...")
return Uni.createFrom().failure(Exception("Some error happened"))
}
So, the getResult() is a function that may misbehave and needs to be called multiple times on failure.
When I run this program, this is what's happening:
Preparing result...
Got error: java.lang.Exception: Some error happened
Got error: java.lang.Exception: Some error happened
Got error: java.lang.Exception: Some error happened
java.lang.Exception: Some error happened
at MainKt.getResult(Main.kt:16)
at MainKt.main(Main.kt:4)
Obiously, the getResult() function is called only once, while the onFailure() stages actually executed three times.
Is there anything that Mutiny could help me to execute getResult() function on each failure? I sure can implement this with a simple loop, but I feel like Mutiny should already have something like this.
Unfortunately, I didn't find anything suitable in the docs.
Your Uni in getResult is created with an "immediate" item, which is cached and never computed again.
Use Uni.createFrom().failure(() -> Exception("Some error happened"))
In this case, it's a supplier, so it won't be cached but called on every attempt.
So, the right solution for this is actually using the Uni.deferred() method like this:
fun main() {
Uni.createFrom().deferred { getResult() }
.onFailure().invoke { t -> println("Got error: $t") }
.onFailure().retry().atMost(2)
.subscribe().with(
{ result -> println(result) },
{ t -> t.printStackTrace() }
)
}
Thanks to Boris the Spider, who suggested to use the deferred(), and to Clement, who clarified its use with null values.
Initially, I misinterpreted the deferred() documentation thinking it's not allowed to return a null value, but actually it's OK for a Supplier to return a Uni of null:
Uni.createFrom.deferred { Uni.createFrom().nullItem() }
What the docs really are prohibiting is returning a null instead of a Uni:
Uni.createFrom().deferred { null }

Is Mono's share().block() non-blocking?

I am in the middle of learning Spring WebFlux. I am using a REST call using below code to parse the response:
private void parseJsonResponse(String folderId) throws IOException {
Mono<ObjectNode> theresponseMono = webClient.get()
.uri("/some/uri")
.retrieve().bodyToMono(ObjectNode.class);
ObjectNode node = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.readValue(theresponseMono.share().block().toString(), ObjectNode.class);
//handle node object here.....
}
Question: Is theresponseMono.share().block() non-blocking here? If not, what can be done to make is completely non-blocking. I am looking for the relevant documentation on this as I want to learn it and not just looking for a yes or no. To summarize, I need to retrieve theresponseMono as non-blocking. Any guidance or any official documentation/link on this please? Thanks.
EDIT:
This is what I am trying to achieve:
Mono<ObjectNode> theresponseMono = webClient.get()
.uri("/some/uri")
.retrieve().bodyToMono(ObjectNode.class).flatMap(node -> {
if (node.get("list").get("entries").isArray()) {
for (JsonNode jsonNode : node.get("list").get("entries")) {
System.out.println(jsonNode);
}
}
});
Somehow I am not able to map using flatMap. What is missing here?
No since here you are blocking it. Right way would be to do
private Mono<ObjectNode> parseJsonResponse(String folderId) {
return webClient.get()
.uri("/some/rui")
.retrieve().bodyToMono(ObjectNode.class)
.flatMap(node-> {
// do your logic here
})
}
I would say everything what is in mono/flux must stay in mono/flux :) Anytime you call block its blocking your thread.

Kotlin discord jda cannot get message by id

I am trying to get the message by entering the id of the message. I saw this being done in an example but it used the old discordjda, when I tried now I get this wierd error and i dont know how to solve it. I tried casting it to different values with no succes
e.channel.history.getMessageById(userMessageFunction.b) {
m ->
if (e.message.timeCreated.isAfter(m.creationTime)) {
userMessageFunction.c.accept(e.message)
messageInteractivesQueue.remove(interactive)
}
}
This is the error
In your code, that brace after userMessageFunction.b is consideredas lambda expression, so your code is same as this code.
e.channel.history.getMessageById(userMessageFunction.b, m -> {
if (e.message.timeCreated.isAfter(m.creationTime)) {
userMessageFunction.c.accept(e.message)
messageInteractivesQueue.remove(interactive)
}
})
If you want to get message, use channel#retrieveMessageById.
So you can change your code like this.
e.channel.retrieveMessageById(userMessageFunction.b).queue {
if (e.message.timeCreated.isAfter(it.creationTime)) {
userMessageFunction.c.accept(e.message)
messageInteractivesQueue.remove(interactive)
}
}
Or this
e.channel.history.getMessageById(userMessageFunction.b).let {
if (e.message.timeCreated.isAfter(it.creationTime)) {
userMessageFunction.c.accept(e.message)
messageInteractivesQueue.remove(interactive)
}
}

Yii trace - proper usage

Unit testing and xdebug usage aside, I wish to have a way to throw some browser message is a value is not expected to be present.
Let's say: $className = 45;
If we have:
public function setMainClass($className) {
if (is_string($className)) {
$this->_mainClass = $className;
} else {
echo Yii::trace(CVarDumper::dumpAsString($className),'vardump');
}
}
We will get this output to the browser on development stage.
It's great.
I'm not sure however, if this is a proper way of use Yii::trace of if I'm miss using it.
Please advice.
It is not necessary to echo the call Yii::trace() (it returns void so the echo does nothing). The other recommendation is that you might consider changing category to resemble a path alias as discussed in the documentation. For example-
} else {
Yii::trace(CVarDumper::dumpAsString($className), 'application.models.MyGreatModel');
}