Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: Nullable to Optional #1401

Closed
wants to merge 4 commits into from
Closed

Java: Nullable to Optional #1401

wants to merge 4 commits into from

Conversation

brunoborges
Copy link
Member

No description provided.

@brunoborges
Copy link
Member Author

@johnoliver any thoughts on this one?

@shawncal shawncal changed the title Nullable to Optional Java: Nullable to Optional Jun 12, 2023
@github-actions github-actions bot added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core python Pull requests for the Python Semantic Kernel labels Jun 13, 2023
@github-actions github-actions bot removed kernel.core kernel Issues or pull requests impacting the core kernel python Pull requests for the Python Semantic Kernel .NET Issue or Pull requests regarding .NET code labels Jun 13, 2023
@lemillermicrosoft lemillermicrosoft added feature branch PR targeting a feature branch java Issue or PR regarding Java code labels Jun 13, 2023
@dsgrieve
Copy link
Member

So... I'm not a big fan of this one. The litmus test for Optional is whether the API is meant to be used in a stream/fluent way. Even then, you can do without Optional. If you look at the changes you have here, you see a lot of the Optional<Blah> blah = whatever(); if (blah.isPresent()) doSomething() anti-pattern. If we do move forward with this, then the implementation should be more fluent. I think you'd be hard-pressed to change NativeSKFunction#getArgumentValue into a fluent style (you could, but you'd need to break it down into smaller functions).

@@ -135,7 +135,7 @@ public Mono<CompletionSKContext> aggregatePartitionedResultsAsync(
return results.map(
list ->
list.stream()
.map(SKContext::getResult)
.map((_context) -> _context.getResult().get())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will blow up with NoSuchElementException if getResult on one of the elements returns Optional.empty(). I think a better way to do this would be
list.stream().map(SKContext::getResult).stream().collect(Collectors.joining("\n"))).map(context::update);

public String get(String key) {
return variables.get(key);
@Nonnull
public Optional<String> get(String key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think APIs that are of the "get the value of this key" variety should return null if the key is not found. Returning null from this Map-like method call is expected behavior.

// If this is bound to input get the input value
if (inputArgs.contains(parameter)) {
String input = context.getVariables().get(ContextVariables.MAIN_KEY);
if (input != null && !input.isEmpty()) {
Optional<String> input = context.getVariables().get(ContextVariables.MAIN_KEY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You lost the !input.isEmpty() check.

arg = input;
}
}

if (arg == null || arg.isEmpty()) {
if (arg.isPresent() == false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again with isEmpty

}
}
}

if ((arg == null || arg.isEmpty()) && variableName.matches("arg\\d")) {
if (arg.isPresent() == false && variableName.matches("arg\\d")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional#isEmpty() is the same as Optional#isPresent() == false.
Your missing the 'arg.isEmpty()' (as in String#isEmpty()) check here.

@@ -270,10 +270,10 @@ private static String getArgumentValue(
+ " or @SKFunctionInputAttribute.");
}

if (NO_DEFAULT_VALUE.equals(arg)) {
if (NO_DEFAULT_VALUE.equals(arg.get())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't an empty optional at this point mean 'no default value'?

it -> {
return it.getResult();
});
return result.map(it -> it.getResult().get());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~Probably want result.map(it::getResult).orElseGet(Mono::empty)~ Yeah... my suggestion isn't right. Still, though, get()` will blow up.

Maybe return result.map(SKContext::getResult).filter(Optional::isPresent).map(Optional::get);

@brunoborges
Copy link
Member Author

Thanks @dsgrieve for the review. I am ready to give up on this proposal as the benefits are definitely not greater than the disadvantages (as experienced by the introduction of bugs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature branch PR targeting a feature branch java Issue or PR regarding Java code
Projects
No open projects
Status: Sprint: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants