From 3e3fbd97eb88753687bd5ebf0a9798f6b0b3e040 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 2 May 2024 17:18:43 +0600 Subject: [PATCH] diff/applyDiff --- .gitignore | 3 ++ CHANGELOG.md | 5 +++ .../micro_utils/repos/diff/CRUDRepoDiff.kt | 20 +++++++++++ .../repos/diff/KeyValueRepoDiff.kt | 33 +++++++++++++++++++ .../repos/diff/KeyValuesRepoDiff.kt | 33 +++++++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/CRUDRepoDiff.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/KeyValueRepoDiff.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/KeyValuesRepoDiff.kt diff --git a/.gitignore b/.gitignore index 90dd34230e7..9d766410e18 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ local.properties kotlin-js-store publishing.sh + +local.* +local/ diff --git a/CHANGELOG.md b/CHANGELOG.md index a8a8490cd08..f078fb94c11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 0.20.49 +* `Repos`: + * `Common`: + * All `Repo`s get `diff` extensions + * `KeyValueRepo` and `KeyValuesRepo` get `applyDiff` extension + ## 0.20.48 * `Versions`: diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/CRUDRepoDiff.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/CRUDRepoDiff.kt new file mode 100644 index 00000000000..f0ab647577b --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/CRUDRepoDiff.kt @@ -0,0 +1,20 @@ +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.CRUDRepo +import dev.inmo.micro_utils.repos.ReadCRUDRepo +import dev.inmo.micro_utils.repos.unset + +suspend fun ReadCRUDRepo.diff(other: Map): MapDiff { + return getAll().diff(other) +} + +suspend fun Map.diff(other: ReadCRUDRepo): MapDiff { + return diff(other.getAll()) +} + +suspend fun MutableMap.applyDiff(other: ReadCRUDRepo) { + applyDiff(diff(other)) +} \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/KeyValueRepoDiff.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/KeyValueRepoDiff.kt new file mode 100644 index 00000000000..58a88d83943 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/KeyValueRepoDiff.kt @@ -0,0 +1,33 @@ +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)) +} \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/KeyValuesRepoDiff.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/KeyValuesRepoDiff.kt new file mode 100644 index 00000000000..9fd09f73c53 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/diff/KeyValuesRepoDiff.kt @@ -0,0 +1,33 @@ +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.* + +suspend fun ReadKeyValuesRepo.diff(other: Map>): MapDiff> { + return getAll().diff(other) +} + +suspend fun Map>.diff(other: ReadKeyValuesRepo): MapDiff> { + return diff(other.getAll()) +} + +suspend fun KeyValuesRepo.applyDiff(diff: MapDiff>) { + diff.removed.forEach { + clear(it.key) + } + set( + diff.changed.map { (k, oldNew) -> + k to oldNew.second + }.toMap() + diff.added + ) +} + +suspend fun KeyValuesRepo.applyDiff(other: Map>) { + applyDiff(diff(other)) +} + +suspend fun MutableMap>.applyDiff(other: ReadKeyValuesRepo) { + applyDiff(diff(other)) +} \ No newline at end of file