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

Destructuring accessors for SecureCellData in Kotlin #638

Merged
merged 2 commits into from
May 15, 2020

Conversation

ilammy
Copy link
Collaborator

@ilammy ilammy commented May 13, 2020

This is the final item of API updates planned in RFC 3.9.

Kotlin supports tuples and destructuring assignment which results in a much nicer APIs with multiple return values. Implement special accessor methods for SecureCellData to enable that:

// You can now write like this:
val (encrypted, authToken) = cellTP.encrypt(message, context)

// Instead of having to spell it out like this:
val result = cellTP.protect(context, message)
val encrypted = result.protectedData
val authToken = result.additionalData

This is implemented by a couple of special methods. They are available to users (i.e., IDEs will show them up in Java autocomplete) but these methods are not intended for general use. The methods also have appropriate @NotNull annotations to make Kotlin experience better.

Initially, RFC 3.9 also suggested some accessor renaming for Java:

  • getProtectedData()getEncryptedData()
  • getAdditionalData()getToken()

However, after giving it a second though, I believe that we don’t need to do this. Renaming is hard, it causes developer pain. While it is justified with new Secure Cell API that has its own improvements, here we’d be renaming for the sake of renaming. While the names are not ideal, they are not that ambiguous either. Furthermore, Android users are likely to use Kotlin in the first place so they will not have to deal with these names anymore.

Checklist

  • Change is covered by automated tests
  • The coding guidelines are followed
  • Public API has proper documentation
  • Example projects and code samples are up-to-date (no Kotlin examples)
  • Changelog is updated

Kotlin supports tuples and destructuring assignment which results in
much nicer APIs with multiple return values. Implement special accessor
methods for SecureCellData to enable that.

These methods are not for general purpose use. They are expected to be
implicitly used in Kotlin when working with Token Protect mode, that's
why they also have @NotNull annotations -- as in Token Protect mode the
encrypted data and authentication token are always available.
@NotNull annotations remove a layer of optionals, so instead of
ByteArray? results we are now dealing with just ByteArray, and
that means we can call copyOf() method directly instead of using
java.utils.Arrays.copyOf(). Replace the calls to make linter happy.
@ilammy ilammy added O-Android 🤖 Operating system: Android W-JavaThemis ☕ Wrapper: Java, Java and Kotlin API labels May 13, 2020
CHANGELOG.md Show resolved Hide resolved
@ilammy ilammy merged commit 77739f4 into cossacklabs:master May 15, 2020
@ilammy ilammy deleted the kotlin-accessors branch May 15, 2020 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-Android 🤖 Operating system: Android W-JavaThemis ☕ Wrapper: Java, Java and Kotlin API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants