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

KeySet::lookup(String) returns Key instead of null on unsuccessful search [BUG JNA binding] #3772

Closed
tucek opened this issue Apr 13, 2021 · 3 comments · Fixed by #3825
Closed
Assignees
Milestone

Comments

@tucek
Copy link
Contributor

tucek commented Apr 13, 2021

Steps to Reproduce the Problem

KeySet set = KeySet.create();
if (set.lookup("user:/unsetkey") != null) {
  throw new AssertionError();
}

Expected Result

null should be returned.

Actual Result

AssertionError is thrown

System Information

  • Elektra Version: 0.9.5
  • Operating System: Ubuntu focal on wsl2

Proposed solution

KeySet::lookup(...) should return Optional<Key> and would return Optional.empty() if Elektra.INSTANCE.ksLookup(ByName)? returns null.

Workaround for 0.9.5

KeySet set = KeySet.create();
// check whether Key pointer is actually null
if (set.lookup("user:/unsetkey").get() != null) {
  throw new AssertionError();
}
@tucek tucek added this to the 0.9.6 milestone Apr 13, 2021
@tucek tucek self-assigned this Apr 13, 2021
@mpranj
Copy link
Member

mpranj commented Apr 13, 2021

Thank you for reporting and working on this.

I am not a user of the Java bindings, so take this with a grain of salt.
From what I see this is not a bug. Since the Key class wraps JNA pointers you can not compare the Key object to null. You have to call get() (which is your workaround) or even simply use the existing isNull() function.

if (!set.lookup("user:/unsetkey").isNull()) {
  throw new AssertionError();
}

That being said, improvements to the Java bindings/API are welcome and a consistent use of optionals could make sense. 👍

@tucek
Copy link
Contributor Author

tucek commented Apr 13, 2021

At least the current behavior contradicts the documentation of the function: @return Key if search successful, null otherwise - Therefore i would consider this a bug.

Apart from that i would consider returning a Key object which has to be checked for referencing a null pointer (and therefore being completely useless), a rather crude approach for a Java API.

I will update the Java API to use Optionals accordingly. I will also consider removing the Key::isNull and require Key to have a non-null pointer reference.

@mpranj
Copy link
Member

mpranj commented Apr 13, 2021

At least the current behavior contradicts the documentation of the function: @return Key if search successful, null otherwise - Therefore i would consider this a bug.

You're right, I overlooked the docu. According to the docu a fix would be to just return null instead of new Key(null). That is irrelevant though, if you want to change the API.

@tucek tucek linked a pull request May 13, 2021 that will close this issue
20 tasks
@tucek tucek mentioned this issue May 13, 2021
20 tasks
@tucek tucek added this to Stage in Java bindings overhaul via automation Aug 9, 2021
@tucek tucek moved this from Stage to Done in Java bindings overhaul Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants