Skip to content

Commit

Permalink
style: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yiteam committed Aug 10, 2023
1 parent e3ae7ad commit a316d3f
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 22 deletions.
6 changes: 2 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
:toc:

[NOTE]
----
====
This project fork from https://github.com/shagaba/jacksync.
----
====

== Creating and applying sync patches (merge, patch, diff, sync)

Expand Down Expand Up @@ -110,7 +110,6 @@ For example:
[source,kotlin]
----
val patchProcessor = ObjectPatchProcessor(objectMapperWrapper)
val postV2 = patchProcessor.patch(originalPostV1, operations)
----

Expand Down Expand Up @@ -143,7 +142,6 @@ For example:
[source,kotlin]
----
val mergeProcessor = ObjectMergeProcessor(objectMapperWrapper)
val postV2 = mergeProcessor.merge(originalPostV1, value)
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class SimpleDiffStrategy : DiffStrategy {
invertible: Boolean,
): List<PatchOperation> {
if (sourceJsonNode.isArray && targetJsonNode.isArray) {
val commonNodes: MutableList<JsonNode> = ArrayList()
val commonNodes = mutableListOf<JsonNode>()

sourceJsonNode.iterator().forEachRemaining { e -> commonNodes.add(e) }

val targetNodes: MutableList<JsonNode> = ArrayList()
val targetNodes = mutableListOf<JsonNode>()

targetJsonNode.iterator().forEachRemaining { e -> targetNodes.add(e) }

Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/team/yi/jacksync/difflog/DiffLogUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ object DiffLogUtils {

return patchPathOperations.filter { it !is TestOperation }.let { operations ->
testOperations.map { testOperation ->
val operation = operations.firstOrNull { it.path == testOperation.path }
?: throw DiffLogException("No `PatchOperation` were found matching the `TestOperation(path=${testOperation.path})`.")
val operation = requireNotNull(operations.firstOrNull { it.path == testOperation.path }) {
val message = "No `PatchOperation` were found matching the `TestOperation(path=${testOperation.path})`."

throw DiffLogException(message)
}

val op = requireNotNull(PatchOperation.getOpName(operation))
val value = if (operation is PatchPathValueOperation) operation.value else null
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/team/yi/jacksync/utils/ChecksumUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object ChecksumUtils {
}

fun computeChecksum(byteArray: ByteArray): String {
val checksum: Checksum = CRC32()
val checksum = CRC32()
checksum.update(byteArray, 0, byteArray.size)

return java.lang.Long.toHexString(checksum.value)
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/team/yi/jacksync/utils/JacksonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ object JacksonUtils {
}.getOrDefault(false)
}

fun toJsonPointer(path: String?): JsonPointer {
requireNotNull(path) { "Path cannot be null" }
fun toJsonPointer(path: String): JsonPointer {
require(path == path.trim()) { "Path has not been trimmed: '$path'" }

return if (SEPARATOR == path.trim()) {
Expand Down
10 changes: 8 additions & 2 deletions src/test/kotlin/team/yi/jacksync/JacksyncTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ class JacksyncTest : BaseTest() {
)

// SyncData regular diff
val syncData = jacksync.diffMapper.diff(SyncObject(1L, postV1), SyncObject(2L, postV2))
val syncData = jacksync.diffMapper.diff(
SyncObject(1L, postV1),
SyncObject(2L, postV2),
)
// SyncObject clientSync
val syncPostV2 = jacksync.syncProcessor.clientSync(SyncObject(1L, postV1), syncData)
val syncPostV2 = jacksync.syncProcessor.clientSync(
SyncObject(1L, postV1),
syncData,
)

Assertions.assertEquals(syncPostV2.data, postV2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AddOperationTest : BaseTest() {
val postV1 = Post()
val postV1Node = objectMapperWrapper.valueToTree<JsonNode>(postV1)
val section = Section("section-1", null)
val sections: MutableList<Section> = ArrayList()
val sections = mutableListOf<Section>()
sections.add(section)

val addOperation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ObjectPatchProcessorTest : BaseTest() {
postV1.version = 1L

// operations
val addOperation: PatchOperation = AddOperation(
val addOperation = AddOperation(
JacksonUtils.toJsonPointer("/title"),
objectMapperWrapper.valueToTree("my test title"),
)
Expand All @@ -45,11 +45,11 @@ class ObjectPatchProcessorTest : BaseTest() {
serverPostV1.title = "my 2nd test title"

// operations
val addOperation: PatchOperation = AddOperation(
val addOperation = AddOperation(
JacksonUtils.toJsonPointer("/title"),
objectMapperWrapper.valueToTree("my 2nd test title"),
)
val replaceOperation: PatchOperation = ReplaceOperation(
val replaceOperation = ReplaceOperation(
JacksonUtils.toJsonPointer("/version"),
objectMapperWrapper.valueToTree(2),
)
Expand Down
17 changes: 12 additions & 5 deletions src/test/kotlin/team/yi/jacksync/sync/LocalSyncProcessorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class LocalSyncProcessorTest : BaseTest() {
0L,
ChecksumUtils.computeChecksum(objectMapperWrapper.writeValueAsString(targetPost)),
listOf(
AddOperation(JacksonUtils.toJsonPointer("/title"), objectMapperWrapper.valueToTree(targetPost.title)),
AddOperation(
JacksonUtils.toJsonPointer("/title"),
objectMapperWrapper.valueToTree(targetPost.title),
),
),
)

Expand Down Expand Up @@ -61,10 +64,14 @@ class LocalSyncProcessorTest : BaseTest() {
targetPost.version = 2L

// sync syncData & operations
val addOperation =
AddOperation(JacksonUtils.toJsonPointer("/title"), objectMapperWrapper.valueToTree(targetPost.title))
val replaceOperation =
ReplaceOperation(JacksonUtils.toJsonPointer("/version"), objectMapperWrapper.valueToTree(2))
val addOperation = AddOperation(
JacksonUtils.toJsonPointer("/title"),
objectMapperWrapper.valueToTree(targetPost.title),
)
val replaceOperation = ReplaceOperation(
JacksonUtils.toJsonPointer("/version"),
objectMapperWrapper.valueToTree(2),
)
val operations = listOf<PatchOperation>(addOperation, replaceOperation)

val syncData = SyncData(
Expand Down

0 comments on commit a316d3f

Please sign in to comment.