Skip to content
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

[CHORE] Add ZplParameters Wrapper #21

Merged
merged 7 commits into from
Sep 25, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build/
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea/misc.xml
*.iws
*.iml
*.ipr
Expand Down
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/BarCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal data class BarCode(
}

override val command: CharSequence = "^B1"
override val parameters: Map<CharSequence, Any?> = addParameters(
override val parameters: ZplParameters = zplParameters(
"o" to orientation.code,
"c" to checkDigit.toString(),
"h" to height,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/FieldBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal data class FieldBlock(
}

override val command: CharSequence = "^FB"
override val parameters: Map<CharSequence, Any?> = addParameters(
override val parameters: ZplParameters = zplParameters(
"w" to width,
"l" to maxLines,
"s" to lineSpacing,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/FieldData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.sainsburys.k2zpl.builder.ZplBuilder

internal data class FieldData(val data: String) : ZplCommand {
override val command: CharSequence = "^FD"
override val parameters: Map<CharSequence, Any?> = addParameters("d" to data)
override val parameters: ZplParameters = zplParameters("d" to data)

override fun build(stringBuilder: StringBuilder): StringBuilder {
return stringBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal data class FieldOrigin(
}

override val command: CharSequence = "^FO"
override val parameters: Map<CharSequence, Any?> = addParameters(
override val parameters: ZplParameters = zplParameters(
"x" to x,
"y" to y,
"j" to justification
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sainsburys/k2zpl/command/Font.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal data class Font(
}

override val command: CharSequence = "^A${font}"
override val parameters: Map<CharSequence, Any?> =
addParameters("o" to orientation, "h" to height, "w" to width)
override val parameters: ZplParameters =
zplParameters("o" to orientation, "h" to height, "w" to width)
}


Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sainsburys/k2zpl/command/GraphicBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ internal data class GraphicBox(
}

override val command: CharSequence = "^GB"
override val parameters: Map<CharSequence, Any?> =
addParameters(
override val parameters: ZplParameters =
zplParameters(
"w" to width,
"h" to height,
"t" to thickness, "c" to color.code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal data class GraphicField(
}

override val command: CharSequence = "^GF"
override val parameters: Map<CharSequence, Any?> = addParameters(
override val parameters: ZplParameters = zplParameters(
"f" to format,
"db" to dataBytes,
"tb" to totalBytes,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/LabelHome.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.sainsburys.k2zpl.builder.ZplBuilder

internal data class LabelHome(val x: Int, val y: Int) : ZplCommand {
override val command: CharSequence = "^LH"
override val parameters: Map<CharSequence, Any?> = addParameters("x" to x, "y" to y)
override val parameters: ZplParameters = zplParameters("x" to x, "y" to y)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal data class LabelLength(val length: Int) : ZplCommand {
}

override val command: CharSequence = "^LL"
override val parameters: Map<CharSequence, Any?> = addParameters("l" to length)
override val parameters: ZplParameters = zplParameters("l" to length)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sainsburys/k2zpl/command/MediaMode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal data class MediaMode(
val prePeelSelect: ZplYesNo
) : ZplCommand {
override val command: CharSequence = "^MM"
override val parameters: Map<CharSequence, Any?> =
addParameters("m" to mediaMode, "p" to prePeelSelect)
override val parameters: ZplParameters =
zplParameters("m" to mediaMode, "p" to prePeelSelect)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sainsburys/k2zpl/command/PrintWidth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal data class PrintWidth(val width: Int) : ZplCommand {
}

override val command: CharSequence = "^PW"
override val parameters: Map<CharSequence, Any?> = addParameters("w" to width)
override val parameters: ZplParameters = zplParameters("w" to width)
}


Expand Down
30 changes: 6 additions & 24 deletions src/main/kotlin/com/sainsburys/k2zpl/command/ZplCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,13 @@ package com.sainsburys.k2zpl.command

interface ZplCommand {
val command: CharSequence
val parameters: Map<CharSequence, Any?> get() = addParameters()
val parameters: ZplParameters get() = zplParameters()
fun build(stringBuilder: StringBuilder) = stringBuilder.apply {
append(command)
with(parameters.values.iterator()) {
if (hasNext()) {
nextNotNull { append(it.toString()) }
}
while (hasNext()) {
nextNotNull {
if (length > command.length) {
append(',')
}
append(it.toString())
}
}
}
parameters
.asSequence()
.mapNotNull { it.value?.toString() }
.filter(String::isNotBlank)
.joinTo(this, separator = ",")
}
}

private fun <T> Iterator<T>.nextNotNull(block: (T) -> Unit) {
next()?.let { block(it) }
}

/**
* A shortcut to adding parameters that helps to enforce use of [LinkedHashMap]
* so that entry order is preserved.
*/
internal fun <K, V> ZplCommand.addParameters(vararg pairs: Pair<K, V>) = linkedMapOf(*pairs)
12 changes: 12 additions & 0 deletions src/main/kotlin/com/sainsburys/k2zpl/command/ZplParameters.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sainsburys.k2zpl.command

class ZplParameters(map: Map<CharSequence, Any?>) : Iterable<Map.Entry<CharSequence, Any?>> {
constructor(vararg pairs: Pair<CharSequence, Any?>) : this(mapOf(*pairs))
private val _parameters: LinkedHashMap<CharSequence, Any?> = LinkedHashMap(map)
operator fun get(key: CharSequence) = _parameters[key]
override fun iterator(): Iterator<Map.Entry<CharSequence, Any?>> =
_parameters.iterator()
}

fun zplParameters(vararg pairs: Pair<CharSequence, Any?>) = ZplParameters(*pairs)
fun zplParameters(builderBlock: MutableMap<CharSequence, Any?>.() -> Unit) = ZplParameters(buildMap(builderBlock))
itsmattking marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ class ZplCommandWithoutParameters : ZplCommand {

class ZplCommandWithOneParameter : ZplCommand {
override val command = "^ZCP"
override val parameters: Map<CharSequence, Any?> = addParameters(
override val parameters: ZplParameters = zplParameters(
"param-one" to "value-one"
)
}

class ZplCommandWithMultipleParameters : ZplCommand {
override val command = "^ZCPS"
override val parameters: Map<CharSequence, Any?> = addParameters(
override val parameters: ZplParameters = zplParameters(
"param-one" to "value-one",
"param-two" to "value-two"
)
}

class ZplCommandWitNullFirstParameter : ZplCommand {
override val command = "^ZCPN"
override val parameters: Map<CharSequence, Any?> = addParameters(
override val parameters: ZplParameters = zplParameters(
"param-one" to null,
"param-two" to "value-two"
)
}

class ZplCommandWitNullSecondParameter : ZplCommand {
override val command = "^ZCPNS"
override val parameters: Map<CharSequence, Any?> = addParameters(
override val parameters: ZplParameters = zplParameters(
"param-one" to "value-one",
"param-two" to null
)
Expand Down
73 changes: 73 additions & 0 deletions src/test/kotlin/com/sainsburys/k2zpl/command/ZplParametersTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.sainsburys.k2zpl.command

import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe

class ZplParametersTest : DescribeSpec({
isolationMode = IsolationMode.InstancePerLeaf

describe("ZplParameters pairs constructor") {
val result = ZplParameters("a" to "b", "c" to "d")
it("returns values passed to ZplParameters") {
result.forEachIndexed { index, param ->
when (index) {
0 -> param.value shouldBe "b"
1 -> param.value shouldBe "d"
}
}
}
it("makes values accessible by get operator") {
result["a"] shouldBe "b"
result["c"] shouldBe "d"
}
}
describe("ZplParameters map constructor") {
val result = ZplParameters(mapOf("a" to "b", "c" to "d"))
it("returns values passed to ZplParameters") {
result.forEachIndexed { index, param ->
when (index) {
0 -> param.value shouldBe "b"
1 -> param.value shouldBe "d"
}
}
}
it("makes values accessible by get operator") {
result["a"] shouldBe "b"
result["c"] shouldBe "d"
}
}
describe("zplParameters pairs function") {
val result = zplParameters("e" to "f", "g" to "h")
it("returns values passed to ZplParameters") {
result.forEachIndexed { index, param ->
when (index) {
0 -> param.value shouldBe "f"
1 -> param.value shouldBe "h"
}
} }
it("makes values accessible by get operator") {
result["e"] shouldBe "f"
result["g"] shouldBe "h"
}
}

describe("ZplParameters builder function") {
val result = zplParameters {
put("a", "b")
put("c", "d")
}
it("returns values passed to ZplParameters") {
result.forEachIndexed { index, param ->
when (index) {
0 -> param.value shouldBe "b"
1 -> param.value shouldBe "d"
}
}
}
it("makes values accessible by get operator") {
result["a"] shouldBe "b"
result["c"] shouldBe "d"
}
}
})