All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.inmo.micro_utils.repos.diff.KeyValueRepoDiff.kt Maven / Gradle / Ivy

There is a newer version: 0.22.2
Show newest version
package dev.inmo.micro_utils.repos.diff

import dev.inmo.micro_utils.common.MapDiff
import dev.inmo.micro_utils.common.applyDiff
import dev.inmo.micro_utils.common.diff
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
import dev.inmo.micro_utils.repos.unset

suspend fun  ReadKeyValueRepo.diff(other: Map): MapDiff {
    return getAll().diff(other)
}

suspend fun  Map.diff(other: ReadKeyValueRepo): MapDiff {
    return diff(other.getAll())
}

suspend fun  KeyValueRepo.applyDiff(diff: MapDiff) {
    unset(diff.removed.map { it.key })
    set(
        diff.changed.map { (k, oldNew) ->
            k to oldNew.second
        }.toMap() + diff.added
    )
}

suspend fun  KeyValueRepo.applyDiff(other: Map) {
    applyDiff(diff(other))
}

suspend fun  MutableMap.applyDiff(other: ReadKeyValueRepo) {
    applyDiff(diff(other))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy