Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package izumi.sick.eba.cursor

import scala.scalajs.js.annotation.{JSExportAll, JSExportTopLevel}
import izumi.sick.model.Ref

import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js
import scala.scalajs.js.JSConverters.*

@JSExportTopLevel("TopCursor")
@JSExportAll
class TopCursorJs(cursor: TopCursor) extends SickCursorJs(cursor.asInstanceOf[SickCursor]) {

@JSExport
def query(request: String): ObjectCursorJs = {
new ObjectCursorJs(cursor.query(request))
}

@JSExport
def getValues: js.Map[String, ObjectCursorJs] = {
cursor.getValues.view.mapValues(new ObjectCursorJs(_)).toMap.toJSMap
}

@JSExport
def readKey(index: Int): ObjectCursorJs = {
new ObjectCursorJs(cursor.readKey(index))
}

protected[eba] def ref: Ref = cursor.ref
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package izumi.sick.eba.reader

import izumi.sick.eba.cursor.{ObjectCursorJs, TopCursorJs}
import izumi.sick.model.RefKind

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExportAll, JSExportTopLevel}

@JSExportTopLevel("EBAReader")
@JSExportAll
class EBAReaderJs(reader: IncrementalEBAReader, rootId: String) {
def query(request: String): js.Any = {
val cursor = new TopCursorJs(reader.getCursor(rootId)).query(request)
resolveCursorRef(cursor)
}

private def resolveCursorRef(cursor: ObjectCursorJs): js.Any = {
cursor.ref.kind match {
case RefKind.TNul => cursor.asNul
case RefKind.TBit => cursor.asBool
case RefKind.TByte => cursor.asByte
case RefKind.TShort => cursor.asShort
case RefKind.TInt => cursor.asInt
case RefKind.TLng => cursor.asDouble
case RefKind.TBigInt => cursor.asBigInt
case RefKind.TFlt => cursor.asFloat
case RefKind.TDbl => cursor.asDouble
case RefKind.TStr => cursor.asString
case _ => js.undefined
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.circe.Json
import izumi.sick.SICK
import izumi.sick.eba.SICKSettings
import izumi.sick.eba.cursor.TopCursorJs
import izumi.sick.eba.reader.{EagerEBAReader, IncrementalEBAReader}
import izumi.sick.eba.reader.{EBAReaderJs, EagerEBAReader, IncrementalEBAReader}
import izumi.sick.eba.writer.EBAWriter
import izumi.sick.model.{SICKWriterParameters, TableWriteStrategy}
import izumi.sick.sickcirce.CirceTraverser.*
Expand Down Expand Up @@ -98,4 +98,17 @@ object SickJsAPI {
val ebaReader = IncrementalEBAReader.openBytes(uint8ArrayToBytes(uint8Array), eagerOffsets = false)
new TopCursorJs(ebaReader.getCursor(rootId))
}

/**
* Alternative method for navigating the sick structure, which has query method with jq like requests
*
* `{ data: { a: 2, b: { c: [1, 2, 3] } } }`
* `const reader = ebaReaderFromUint8Array(uint8Array, "data")`
* `reader.query("b.c.[1]")`
*/
@JSExportTopLevel("ebaReaderFromUint8Array")
def ebaReaderFromUint8Array(uint8Array: Uint8Array, rootId: String): EBAReaderJs = {
val ebaReader = IncrementalEBAReader.openBytes(uint8ArrayToBytes(uint8Array), eagerOffsets = false)
new EBAReaderJs(ebaReader, rootId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class JsApiTest extends AnyWordSpec {
assert(cursor.downField("b").asInt.toOption.contains(2))
assert(cursor.downField("c").asInt.toOption.contains(3))
}

locally {
val uint8Array = SickJsAPI.encodeJSONStringsToSickUint8Array(js.Dictionary("data" -> """{ "a": 1 , "b": { "c": 2, "d": [3, 4, 5] } }"""))
val reader = SickJsAPI.ebaReaderFromUint8Array(uint8Array, "data")
assert(reader.query("a").asInstanceOf[Int] == 1)
assert(reader.query("b.c").asInstanceOf[Int] == 2)
assert(reader.query("b.d.[1]").asInstanceOf[Int] == 4)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class IncrementalEBAReader(
case currentQuery0 :: next0 =>
val (currentQuery, next) = handleBracketsWithoutDot(currentQuery0, next0)
if (currentQuery.startsWith("[") && currentQuery.endsWith("]")) {
val index = currentQuery.substring(1, currentQuery.length - 2)
val index = currentQuery.substring(1, currentQuery.length - 1)
val iindex = index.toInt

val resolvedArr = readArrayElementRef(ref, iindex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,7 @@ class SickCursorTest extends AnyWordSpec {
assert(cursor.query("data.person").getValues.get("name").exists(_.asString.contains("Alice")))

assert(cursor.query("data.person").readKey(1).asInt.contains(30))

assert(cursor.query("data.numbers.[1]").asString.contains("two"))
}
}
4 changes: 4 additions & 0 deletions json-sick-scala/npm-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Accepts dictionary where keys are root names and values are Uint8Arrays containi

Accepts an instance of `Uint8Array` and the rootId, returns a cursor to navigate through the structure.

### `ebaReaderFromUint8Array(uint8Array: Uint8Array, rootId: string): EBAReader`

Accepts an instance of `Uint8Array` and the rootId, returns a reader, which has query method, with jq like requests.

## License

BSD-2-Clause
Expand Down
17 changes: 16 additions & 1 deletion json-sick-scala/npm-template/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
encodeObjsToSickUint8Array,
encodeJSONStringsToSickUint8Array,
encodeJSONBytesToSickUint8Array,
sickCursorFromUint8Array
sickCursorFromUint8Array,
ebaReaderFromUint8Array
} from "./json-sick-2.13-fullOpt.js";

test("Encode/Decode obj test", t => {
Expand Down Expand Up @@ -63,4 +64,18 @@ test("Sick Cursors test", t => {

t.is(cursor.downField("arr").downArray.right.value.asString, "b");
t.is(cursor.downField("arr").downArray.downIndex(2).asString, "c");
})

test("EBAReader test", t => {
const encoded = encodeObjToSickUint8Array("data",
{
object: {
life: 42,
array: ["a", "b", "c", "d", "e"]
}
});
const reader = ebaReaderFromUint8Array(encoded, "data");

t.is(reader.query("object.life"), 42);
t.is(reader.query("object.array.[2]"), "c");
})