-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add neo4j version parser #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
common/src/main/kotlin/org/neo4j/connectors/kafka/configuration/Neo4jVersion.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * Copyright (c) "Neo4j" | ||
| * Neo4j Sweden AB [https://neo4j.com] | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.neo4j.connectors.kafka.configuration | ||
|
|
||
| import java.util.* | ||
| import kotlin.math.sign | ||
|
|
||
| class Neo4jVersion( | ||
| private val major: Int, | ||
| private val minor: Int, | ||
| private val patch: Int = Int.MAX_VALUE | ||
| ) : Comparable<Neo4jVersion> { | ||
|
|
||
| override fun compareTo(other: Neo4jVersion): Int { | ||
| if (major != other.major) { | ||
| return signum(major - other.major) | ||
| } | ||
| if (minor != other.minor) { | ||
| return signum(minor - other.minor) | ||
| } | ||
| return signum(patch - other.patch) | ||
| } | ||
|
|
||
| override fun equals(other: Any?): Boolean { | ||
| if (other !is Neo4jVersion) { | ||
| return false | ||
| } | ||
| return major == other.major && minor == other.minor && patch == other.patch | ||
| } | ||
|
|
||
| override fun hashCode(): Int { | ||
| return Objects.hash(major, minor, patch) | ||
| } | ||
|
|
||
| override fun toString(): String { | ||
| if (patch == Int.MAX_VALUE) { | ||
| return String.format("%d.%d", major, minor) | ||
| } | ||
| return String.format("%d.%d.%d", major, minor, patch) | ||
| } | ||
|
|
||
| companion object { | ||
| val v5 = Neo4jVersion(5, 0, 0) | ||
| val v4_4 = Neo4jVersion(4, 4, 0) | ||
|
|
||
| fun of(version: String): Neo4jVersion { | ||
| var major = -1 | ||
| var minor = -1 | ||
| var patch = -1 | ||
| var buffer = "" | ||
| for (c in version.toCharArray()) { | ||
| if (c != '.') { | ||
| buffer += c | ||
| continue | ||
| } | ||
| if (major == -1) { | ||
| major = buffer.toInt(10) | ||
| } else if (minor == -1) { | ||
| minor = parseMinor(buffer) | ||
| } else { | ||
| throw invalidVersion(version) | ||
| } | ||
| buffer = "" | ||
| } | ||
| if (buffer.isNotEmpty()) { | ||
| if (minor == -1) { | ||
| minor = parseMinor(buffer) | ||
| } else { | ||
| patch = buffer.toInt(10) | ||
| } | ||
| } | ||
| if (major == -1 || minor == -1) { | ||
| throw invalidVersion(version) | ||
| } | ||
| if (patch == -1) { | ||
| return Neo4jVersion(major, minor) | ||
| } | ||
| return Neo4jVersion(major, minor, patch) | ||
| } | ||
|
|
||
| private fun parseMinor(buffer: String): Int { | ||
| return buffer.replace("-aura", "").toInt(10) | ||
| } | ||
|
|
||
| private fun signum(result: Int): Int { | ||
| return sign(result.toDouble()).toInt() | ||
| } | ||
|
|
||
| private fun invalidVersion(version: String): IllegalArgumentException { | ||
| return IllegalArgumentException(String.format("Invalid Neo4j version: %s", version)) | ||
| } | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
common/src/test/kotlin/org/neo4j/connectors/kafka/configuration/Neo4jVersionTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright (c) "Neo4j" | ||
| * Neo4j Sweden AB [https://neo4j.com] | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.neo4j.connectors.kafka.configuration | ||
|
|
||
| import kotlin.test.assertEquals | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.assertThrows | ||
|
|
||
| class Neo4jVersionTest { | ||
|
|
||
| @Test | ||
| fun `should parse version string`() { | ||
| assertEquals(Neo4jVersion.of("4.4"), Neo4jVersion(4, 4)) | ||
| assertEquals(Neo4jVersion.of("4.4-aura"), Neo4jVersion(4, 4)) | ||
| assertEquals(Neo4jVersion.of("4.4.13"), Neo4jVersion(4, 4, 13)) | ||
| assertEquals(Neo4jVersion.of("2025.01"), Neo4jVersion(2025, 1)) | ||
| assertEquals(Neo4jVersion.of("2025.01.0"), Neo4jVersion(2025, 1, 0)) | ||
| assertEquals(Neo4jVersion.of("2025.01-aura"), Neo4jVersion(2025, 1)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should fail invalid version format`() { | ||
| assertThrows<IllegalArgumentException> { Neo4jVersion.of("") } | ||
| assertThrows<IllegalArgumentException> { Neo4jVersion.of("5") } | ||
| assertThrows<IllegalArgumentException> { Neo4jVersion.of("5.5.3.1") } | ||
| assertThrows<NumberFormatException> { Neo4jVersion.of("..") } | ||
| assertThrows<NumberFormatException> { Neo4jVersion.of("5..3") } | ||
| assertThrows<NumberFormatException> { Neo4jVersion.of(".2025.6") } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.