Skip to content

Commit

Permalink
Added 'modifyEntries' helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Trotsenko committed Jun 19, 2022
1 parent 53a1253 commit 2d4494d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ fun KeePassDatabase.modifyEntry(
copy(group = group.modifyEntry(uuid, block))
}

fun KeePassDatabase.modifyEntries(
block: Entry.() -> Entry
) = modifyContent {
copy(group = group.modifyEntries(block))
}

fun KeePassDatabase.removeEntry(
uuid: UUID
) = modifyContent {
Expand Down Expand Up @@ -74,6 +80,27 @@ private fun Group.modifyEntry(
}
}

private fun Group.modifyEntries(
block: Entry.() -> Entry
): Group = copy(
entries = entries.map { entry ->
val newEntry = block(entry)

if (newEntry != entry) {
val now = Instant.now()
newEntry.copy(
times = entry.times?.copy(
lastAccessTime = now,
lastModificationTime = now
) ?: TimeData.create()
)
} else {
newEntry
}
},
groups = groups.map { it.modifyEntries(block) }
)

private fun Group.removeChildEntry(
uuid: UUID
): Group {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@ class KeePassDatabaseSpec : DescribeSpec({
group.name shouldBe "Hello"
}

it("Entries mass modification") {
val label = "Hello"
val database = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
passphrase = "1"
).modifyEntries {
copy(overrideUrl = label)
}

database.traverse { element ->
if (element is Entry) {
element.overrideUrl shouldBe label
}
}
}

it("Entry modification with history") {
val (_, entry) = loadDatabase(
rawData = DatabaseRes.GroupsAndEntries.DbGroupsAndEntries,
Expand Down

0 comments on commit 2d4494d

Please sign in to comment.