Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

SnapshotReader #33

@Trickybrain

Description

@Trickybrain

To take step in #7

SnapshotReader Test

class SnapshotReaderTest {
@Test
fun facet() {
val reader =
SnapshotReader(
SnapshotValueReader.of(
"""
╔═ Apple ═╗
Apple
╔═ Apple[color] ═╗
green
╔═ Apple[crisp] ═╗
yes
╔═ Orange ═╗
Orange
"""
.trimIndent()))
reader.peekKey() shouldBe "Apple"
reader.peekKey() shouldBe "Apple"
reader.nextSnapshot() shouldBe
Snapshot.of("Apple").plusFacet("color", "green").plusFacet("crisp", "yes")
reader.peekKey() shouldBe "Orange"
reader.peekKey() shouldBe "Orange"
reader.nextSnapshot() shouldBe Snapshot.of("Orange")
reader.peekKey() shouldBe null
}
@Test
fun binary() {
val reader =
SnapshotReader(
SnapshotValueReader.of(
"""
╔═ Apple ═╗
Apple
╔═ Apple[color] ═╗ base64 length 3 bytes
c2Fk
╔═ Apple[crisp] ═╗
yes
╔═ Orange ═╗ base64 length 3 bytes
c2Fk
"""
.trimIndent()))
reader.peekKey() shouldBe "Apple"
reader.peekKey() shouldBe "Apple"
reader.nextSnapshot() shouldBe
Snapshot.of("Apple").plusFacet("color", "sad".toByteArray()).plusFacet("crisp", "yes")
reader.peekKey() shouldBe "Orange"
reader.peekKey() shouldBe "Orange"
reader.nextSnapshot() shouldBe Snapshot.of("sad".toByteArray())
reader.peekKey() shouldBe null
}
}

SnapshotReader

class SnapshotReader(val valueReader: SnapshotValueReader) {
fun peekKey(): String? {
val next = valueReader.peekKey() ?: return null
if (next == SnapshotFile.END_OF_FILE) {
return null
}
require(next.indexOf('[') == -1) {
"Missing root snapshot, square brackets not allowed: '$next'"
}
return next
}
fun nextSnapshot(): Snapshot {
val rootName = peekKey()
var snapshot = Snapshot.of(valueReader.nextValue())
while (true) {
val nextKey = valueReader.peekKey() ?: return snapshot
val facetIdx = nextKey.indexOf('[')
if (facetIdx == -1 || (facetIdx == 0 && nextKey == SnapshotFile.END_OF_FILE)) {
return snapshot
}
val facetRoot = nextKey.substring(0, facetIdx)
require(facetRoot == rootName) {
"Expected '$nextKey' to come after '$facetRoot', not '$rootName'"
}
val facetEndIdx = nextKey.indexOf(']', facetIdx + 1)
require(facetEndIdx != -1) { "Missing ] in $nextKey" }
val facetName = nextKey.substring(facetIdx + 1, facetEndIdx)
snapshot = snapshot.plusFacet(facetName, valueReader.nextValue())
}
}
fun skipSnapshot() {
val rootName = peekKey()!!
valueReader.skipValue()
while (peekKey()?.startsWith("$rootName[") == true) {
valueReader.skipValue()
}
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions