Skip to content

Commit 9864bc0

Browse files
author
Oleg
committed
Add instance methods to JsonPointer for platform without extensions
1 parent 282d9d6 commit 9864bc0

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/commonMain/kotlin/com/github/optimumcode/json/pointer/JsonPointer.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,46 @@ public sealed class JsonPointer(
2020
internal val next: JsonPointer? = null,
2121
) {
2222

23+
/**
24+
* Creates a new [JsonPointer] that points to an [index] in the array.
25+
*
26+
* Example:
27+
* ```kotlin
28+
* val pointer = JsonPointer("/test").atIndex(0) // "/test/0"
29+
* ```
30+
*/
31+
public fun atIndex(index: Int): JsonPointer =
32+
JsonPointer(
33+
buildString {
34+
val pointer = this@JsonPointer.toString()
35+
append(pointer)
36+
if (!pointer.endsWith(SEPARATOR)) {
37+
append(SEPARATOR)
38+
}
39+
append(index)
40+
},
41+
)
42+
43+
/**
44+
* Creates a new [JsonPointer] that points to a [property] passed as a parameter.
45+
*
46+
* Example:
47+
* ```kotlin
48+
* val pointer = JsonPointer.ROOT.atProperty("prop1").atProperty("prop2") // "/prop1/prop2"
49+
* ```
50+
*/
51+
public fun atProperty(property: String): JsonPointer =
52+
JsonPointer(
53+
buildString {
54+
val pointer = this@JsonPointer.toString()
55+
append(pointer)
56+
if (!pointer.endsWith(SEPARATOR)) {
57+
append(SEPARATOR)
58+
}
59+
append(property)
60+
},
61+
)
62+
2363
override fun toString(): String {
2464
return if (pathOffset <= 0) {
2565
fullPath

src/commonMain/kotlin/com/github/optimumcode/json/pointer/extensions.kt

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,7 @@ import kotlin.jvm.JvmName
1818
* val index = pointer[0] // "/test/0"
1919
* ```
2020
*/
21-
public operator fun JsonPointer.get(index: Int): JsonPointer =
22-
JsonPointer(
23-
buildString {
24-
val pointer = this@get.toString()
25-
append(pointer)
26-
if (!pointer.endsWith(JsonPointer.SEPARATOR)) {
27-
append(JsonPointer.SEPARATOR)
28-
}
29-
append(index)
30-
},
31-
)
21+
public operator fun JsonPointer.get(index: Int): JsonPointer = atIndex(index)
3222

3323
/**
3424
* Creates a new [JsonPointer] that points to a [property] passed as a parameter.
@@ -39,17 +29,7 @@ public operator fun JsonPointer.get(index: Int): JsonPointer =
3929
* val pointer = JsonPointer.ROOT / "prop1" / "prop2" // "/prop1/prop2"
4030
* ```
4131
*/
42-
public operator fun JsonPointer.div(property: String): JsonPointer =
43-
JsonPointer(
44-
buildString {
45-
val pointer = this@div.toString()
46-
append(pointer)
47-
if (!pointer.endsWith(JsonPointer.SEPARATOR)) {
48-
append(JsonPointer.SEPARATOR)
49-
}
50-
append(property)
51-
},
52-
)
32+
public operator fun JsonPointer.div(property: String): JsonPointer = atProperty(property)
5333

5434
/**
5535
* Appends [otherPointer] to the current [JsonPointer].

0 commit comments

Comments
 (0)