Providing settings for codec

Here is a basic interface for settings for our codec implementation:

data class ScaleCodecSettings<Data: Any>(
    val dataContainer: ScaleDataContainer<Data>,
    val adapterProvider: ScaleCodecAdapterProvider
)

If you want to override default behavior, feel free to instantiate this class by providing your custom dataContainer and adapterProvider.

However, we do have default settings functions which we use:

fun default() = ScaleCodecSettings(
    dataContainer = ScaleByteArrayDataContainer(),
    adapterProvider = DefaultScaleCodecAdapterProvider()
)

fun hex() = ScaleCodecSettings(
    dataContainer = ScaleHexDataContainer(),
    adapterProvider = DefaultScaleCodecAdapterProvider()
)

They're used respectively to operate with ByteArray and Strings which contain hex values.

So if you expect to interact with a custom data representation, feel free to create your factory method by injecting custom data container and providing our default scale codec adapter provider.

Looking at hex data container, it's very simple. Just provide the behavior how to convert this to and back from ByteArray.

class ScaleHexDataContainer: ScaleDataContainer<String> {
    override val type get() = String::class
    override fun fromByteArray(byteArray: ByteArray) = byteArray.hex.encode()
    override fun toByteArray(data: String) = data.hex.decode()
}

Last updated