Skip to content

Commit

Permalink
Standardize on a "frame" metaphor.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Feb 21, 2025
1 parent aad41a4 commit 292f6e8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
12 changes: 6 additions & 6 deletions jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/Mode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ enum class Mode {
msg("Snapshot " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
internal fun msgSnapshotMismatchBinary(expected: ByteArray, actual: ByteArray) =
msgSnapshotMismatch(expected.toQuotedPrintable(), actual.toQuotedPrintable())
internal fun msgVcrKeyMismatch(key: String, expected: String, actual: String) =
msg("VCR key $key " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
internal fun msgVcrKeyUnread(expected: Int, actual: Int) =
msg("VCR entries unread - only $actual were read out of $expected")
internal fun msgVcrKeyUnderflow(expected: Int) =
internal fun msgVcrMismatch(key: String, expected: String, actual: String) =
msg("VCR frame $key " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
internal fun msgVcrUnread(expected: Int, actual: Int) =
msg("VCR frames unread - only $actual were read out of $expected")
internal fun msgVcrUnderflow(expected: Int) =
msg(
"VCR entries exhausted - only $expected are available but you tried to read ${expected + 1}")
"VCR frames exhausted - only $expected are available but you tried to read ${expected + 1}")
private fun ByteArray.toQuotedPrintable(): String {
val sb = StringBuilder()
for (byte in this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ internal constructor(
}

private class State(val readMode: Boolean) {
var count = 0
val sequence = mutableListOf<Pair<String, SnapshotValue>>()
var currentFrame = 0
val frames = mutableListOf<Pair<String, SnapshotValue>>()
}
private val state: State

Expand All @@ -55,61 +55,66 @@ internal constructor(
check(num == idx)
++idx
val keyAfterNum = key.substring(nextClose + 1)
state.sequence.add(keyAfterNum to value)
state.frames.add(keyAfterNum to value)
}
}
}
override fun close() {
if (state.readMode) {
if (state.sequence.size != state.count) {
if (state.frames.size != state.currentFrame) {
throw Selfie.system.fs.assertFailed(
Selfie.system.mode.msgVcrKeyUnread(state.sequence.size, state.count))
Selfie.system.mode.msgVcrUnread(state.frames.size, state.currentFrame))
}
} else {
var snapshot = Snapshot.of("")
var idx = 1
for ((key, value) in state.sequence) {
for ((key, value) in state.frames) {
snapshot = snapshot.plusFacet("$OPEN$idx$CLOSE$key", value)
}
disk.writeDisk(snapshot, sub, call)
}
}
private fun nextValue(key: String): SnapshotValue {
private fun nextFrameValue(key: String): SnapshotValue {
val mode = Selfie.system.mode
val fs = Selfie.system.fs
if (state.sequence.size <= state.count) {
throw fs.assertFailed(mode.msgVcrKeyUnderflow(state.sequence.size))
if (state.frames.size <= state.currentFrame) {
throw fs.assertFailed(mode.msgVcrUnderflow(state.frames.size))
}
val expected = state.sequence[state.count++]
val expected = state.frames[state.currentFrame++]
if (expected.first != key) {
throw fs.assertFailed(
mode.msgVcrKeyMismatch("$sub[$OPEN${state.count}$CLOSE]", expected.first, key),
mode.msgVcrMismatch("$sub[$OPEN${state.currentFrame}$CLOSE]", expected.first, key),
expected.first,
key)
}
return expected.second
}
fun <V> next(key: String, roundtripValue: Roundtrip<V, String>, value: Cacheable<V>): V {
fun <V> nextFrame(key: String, roundtripValue: Roundtrip<V, String>, value: Cacheable<V>): V {
if (state.readMode) {
return roundtripValue.parse(nextValue(key).valueString())
return roundtripValue.parse(nextFrameValue(key).valueString())
} else {
val value = value.get()
state.sequence.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
state.frames.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
return value
}
}
fun next(key: String, value: Cacheable<String>): String = next(key, Roundtrip.identity(), value)
inline fun <reified V> nextJson(key: String, value: Cacheable<V>): V =
next(key, RoundtripJson.of<V>(), value)
fun <V> nextBinary(key: String, roundtripValue: Roundtrip<V, ByteArray>, value: Cacheable<V>): V {
fun nextFrame(key: String, value: Cacheable<String>): String =
nextFrame(key, Roundtrip.identity(), value)
inline fun <reified V> nextFrameJson(key: String, value: Cacheable<V>): V =
nextFrame(key, RoundtripJson.of<V>(), value)
fun <V> nextFrameBinary(
key: String,
roundtripValue: Roundtrip<V, ByteArray>,
value: Cacheable<V>
): V {
if (state.readMode) {
return roundtripValue.parse(nextValue(key).valueBinary())
return roundtripValue.parse(nextFrameValue(key).valueBinary())
} else {
val value = value.get()
state.sequence.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
state.frames.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
return value
}
}
fun <V> nextBinary(key: String, value: Cacheable<ByteArray>): ByteArray =
nextBinary(key, Roundtrip.identity(), value)
fun <V> nextFrameBinary(key: String, value: Cacheable<ByteArray>): ByteArray =
nextFrameBinary(key, Roundtrip.identity(), value)
}

0 comments on commit 292f6e8

Please sign in to comment.