Couchbase Lite Kotlin - Unable to import Replicator Type

I’m putting together a wrapper API for Couchbase Lite in Kotlin Multiplatform, using the Java SDK for Android and Objective-C SDK for iOS (Kotlin/Native doesn’t support direct pure Swift interop yet). I have most of the API implemented, but I’m blocked by this issue, as I am unable to use Java code in the KMM module to workaround the inaccessible enum.

@blake.meike I was going to suggest, can AbstractReplicatorConfiguration be made public to fix this issue without changing the API? This is how AbstractReplicator is able to have its ActivityLevel enum accessible. Looking at the current code, it looks like this change has actually been made already, which is great. Now I just need to get an enterprise build that includes this fix in order to fully test my Kotlin API. I’ve been in discussion to get a VF 2.8.4 build with this other fix. So maybe this route will work.

The 2.8.4 release should be GA in a month or so pending QA approval. I don’t think we’ve seen a request for VF come in as yet. So please work with the accounts team on that.

Since I’m anxious to continue testing my KMM API, I went ahead and implemented a workaround using reflection and an extension property:

enum class ReplicatorType {
    
    PUSH_AND_PULL,
    PUSH,
    PULL;

    companion object {

        private val replicatorTypeValues = Class
                .forName("com.couchbase.lite.AbstractReplicatorConfiguration\$ReplicatorType")
                .enumConstants

        internal fun from(replicatorType: Any): ReplicatorType =
                when (replicatorTypeValues.indexOf(replicatorType)) {
                    0 -> PUSH_AND_PULL
                    1 -> PUSH
                    2 -> PULL
                    else -> error("Unexpected value")
                }
    }

    internal val delegate: Any
        get() = replicatorTypeValues[ordinal]
}

@Suppress("INACCESSIBLE_TYPE", "UNCHECKED_CAST")
var ReplicatorConfiguration.replicatorTypeKt: ReplicatorType
    get() = ReplicatorType.from(replicatorType)
    set(value) {
        (ReplicatorConfiguration::setReplicatorType as (ReplicatorConfiguration, Any) -> Unit)
                .invoke(this, value.delegate)
    }

So far initial tests, it’s working great on Android. Next I’m going to try running some of your tests against the API and also test on iOS.

Delighted to see people using Kotlin.

AbstractReplicatorConfiguration is public in 2.8.4

I am currently planning an add-on library that we will (to an extent yet to be determined) support. Since I am not actually a platform user, I don’t necessarily see all the pain points. Contributions from folks like you are going to be essential to making this Couchbase Kotlin library a success.

I’d be very pleased if you would contact me (first dot last at couchbase) with any suggestions you have for the contents of the library.

That’s great to hear you’re planning Kotlin support. Would this library be a KMM module that could compile for both android/jvm and ios/native targets? That’s my goal with the wrapper API I’ve been working on. So far it’s been coming along nicely. The way you’ve implemented the Couchbase Lite SDKs with a consistent API between languages has made this mostly straightforward for anything that doesn’t touch a low-level platform dependency. At this point the ones I’ve come across are:

  • Date/NSDate (I’ve replaced with kotlinx-datetime)
  • Executor / dispatch queue (might be adaptable to coroutines)
  • InputStream/NSInputStream (might be adaptable to kotlinx-io in the future)
  • URL/NSURL (also possibly kotlinx-io, but replaced with raw strings in some cases for now)
  • Certificate/SecCertificateRef (raw ByteArray/NSData in some cases, not clear in others)

But the large majority of the API lines up very well, needing only basic type conversions in some instances.

The couchbase-lite-kotlin library has some great extensions that make the query syntax much more concise. The reactive Flow extensions are also really useful. I’ve written some additional extensions besides utilizing these to take advantage of Couchbase’s live query and document change listener abilities using Kotlin Flow.

I’ll send you an email with more details as I gather more insight with how our KMM shared module evolves.

1 Like

Thanks for sharing the input. Very insightful.At this time, the plan for initial release of the library is to compile for Android. KMM shared module support could be future consideration.