Skip to content

Commit f5c2df9

Browse files
committed
removed unused imports, added detailed comments to EncodedFormat and PackedFormat
1 parent 4450a49 commit f5c2df9

6 files changed

Lines changed: 104 additions & 33 deletions

File tree

src/main/kotlin/com/eignex/kencode/EncodedFormat.kt

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,63 @@
11
package com.eignex.kencode
22

3-
import PackedFormat
43
import kotlinx.serialization.*
54
import kotlinx.serialization.modules.SerializersModule
65

6+
/**
7+
* Holds the configuration for an [EncodedFormat] instance.
8+
*
9+
* @property codec The ASCII-safe byte codec used to turn raw bytes into text (e.g., Base62, Base64).
10+
* @property checksum An optional checksum appended to the binary payload and verified upon decoding.
11+
* @property binaryFormat The underlying binary serialization format used before encoding to text.
12+
*/
713
data class EncodedConfiguration(
814
val codec: ByteEncoding = Base62,
915
val checksum: Checksum? = null,
1016
val binaryFormat: BinaryFormat = PackedFormat.Default
1117
)
1218

1319
/**
14-
* Text `StringFormat` that combines:
20+
* Text `StringFormat` that produces short, predictable string tokens by composing:
1521
*
16-
* - A binary format (e.g. [PackedFormat], `ProtoBuf`)
17-
* - An optional checksum
18-
* - An ASCII-safe byte encoding (e.g. [Base62], [Base64], [Base36], [Base85])
22+
* 1. A binary format (e.g. [PackedFormat], `ProtoBuf`).
23+
* 2. An optional checksum.
24+
* 3. An ASCII-safe byte encoding (e.g. [Base62], [Base64], [Base36], [Base85]).
1925
*
2026
* Typical use:
27+
* - `encodeToString`: serialize -> (optionally) append checksum -> encode bytes to text.
28+
* - `decodeFromString`: decode text to bytes -> (optionally) verify checksum -> deserialize.
2129
*
22-
* - `encodeToString`: serialize → (optionally) append checksum → encode bytes
23-
* - `decodeFromString`: decode bytes → (optionally) verify checksum → deserialize
30+
* Use the [EncodedFormat] builder function to create a customized instance.
2431
*
25-
* This is intended for short, predictable tokens for URLs, headers, file names, etc.
26-
*
27-
* @property codec ASCII-safe byte codec used to turn raw bytes into text.
28-
* @property checksum Optional checksum appended to the binary payload and verified on decode.
29-
* @property binaryFormat Binary serialization format used before encoding.
32+
* @property configuration The active configuration dictating the codec, checksum, and binary format.
3033
*/
3134
@OptIn(ExperimentalSerializationApi::class)
3235
open class EncodedFormat(
3336
val configuration: EncodedConfiguration,
3437
) : StringFormat {
3538

39+
/**
40+
* Secondary constructor for direct instantiation without the builder.
41+
*/
3642
constructor(
3743
codec: ByteEncoding = Base62,
3844
checksum: Checksum? = null,
3945
binaryFormat: BinaryFormat = PackedFormat,
4046
) : this(EncodedConfiguration(codec, checksum, binaryFormat))
4147

48+
/**
49+
* Delegates to the underlying [BinaryFormat]'s serializers module.
50+
*/
4251
override val serializersModule: SerializersModule get() = configuration.binaryFormat.serializersModule
4352

4453
/**
45-
* Default format: `PackedFormat` + `Base62` without checksum.
54+
* Default format: `PackedFormat` + `Base62` without a checksum.
4655
*/
4756
companion object Default : EncodedFormat()
4857

4958
/**
50-
* Serializes [value] with [binaryFormat], optionally appends [checksum],
51-
* and encodes the result using [codec].
59+
* Serializes [value] with the configured binary format, optionally appends a checksum,
60+
* and encodes the resulting byte array using the text codec.
5261
*/
5362
override fun <T> encodeToString(
5463
serializer: SerializationStrategy<T>, value: T
@@ -61,10 +70,10 @@ open class EncodedFormat(
6170
}
6271

6372
/**
64-
* Decodes [string] using [codec], optionally verifies [checksum],
65-
* then deserializes the remaining bytes with [binaryFormat].
73+
* Decodes [string] using the text codec, optionally verifies and strips the checksum,
74+
* then deserializes the remaining bytes with the configured binary format.
6675
*
67-
* @throws IllegalArgumentException if a checksum is configured and does not match.
76+
* @throws IllegalArgumentException if a checksum is configured and the verification fails.
6877
*/
6978
override fun <T> decodeFromString(
7079
deserializer: DeserializationStrategy<T>, string: String
@@ -85,12 +94,39 @@ open class EncodedFormat(
8594
}
8695
}
8796

97+
/**
98+
* Builder for configuring [EncodedFormat] instances.
99+
*/
88100
class EncodedFormatBuilder {
101+
/**
102+
* The ASCII-safe byte codec used to turn raw bytes into text. Defaults to [Base62].
103+
*/
89104
var codec: ByteEncoding = Base62
105+
106+
/**
107+
* An optional checksum appended to the binary payload. Defaults to `null`.
108+
*/
90109
var checksum: Checksum? = null
110+
111+
/**
112+
* The underlying binary serialization format. Defaults to [PackedFormat.Default].
113+
*/
91114
var binaryFormat: BinaryFormat = PackedFormat.Default
92115
}
93116

117+
/**
118+
* Creates a customized [EncodedFormat] instance.
119+
*
120+
* ```
121+
* val format = EncodedFormat {
122+
* codec = Base36
123+
* checksum = Crc16
124+
* }
125+
* ```
126+
*
127+
* @param from An existing [EncodedFormat] instance to copy defaults from.
128+
* @param builderAction The configuration block.
129+
*/
94130
fun EncodedFormat(
95131
from: EncodedFormat = EncodedFormat.Default,
96132
builderAction: EncodedFormatBuilder.() -> Unit

src/main/kotlin/com/eignex/kencode/PackedDecoder.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.eignex.kencode
22

3-
import PackedConfiguration
43
import kotlinx.serialization.DeserializationStrategy
54
import kotlinx.serialization.ExperimentalSerializationApi
65
import kotlinx.serialization.descriptors.*

src/main/kotlin/com/eignex/kencode/PackedEncoder.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.eignex.kencode
22

3-
import PackedConfiguration
43
import kotlinx.serialization.ExperimentalSerializationApi
54
import kotlinx.serialization.SerializationStrategy
65
import kotlinx.serialization.descriptors.*

src/main/kotlin/com/eignex/kencode/PackedFormat.kt

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import com.eignex.kencode.PackedDecoder
2-
import com.eignex.kencode.PackedEncoder
1+
package com.eignex.kencode
2+
33
import kotlinx.serialization.*
44
import kotlinx.serialization.modules.EmptySerializersModule
55
import kotlinx.serialization.modules.SerializersModule
66
import java.io.ByteArrayOutputStream
77

8+
/**
9+
* Holds the configuration for a [PackedFormat] instance.
10+
*
11+
* @property defaultVarInt When true, all `Int` and `Long` fields without specific annotations are encoded as variable-length integers.
12+
* @property defaultZigZag When true, all `Int` and `Long` fields without specific annotations are ZigZag encoded to efficiently store small negative numbers.
13+
*/
814
data class PackedConfiguration(
915
val defaultVarInt: Boolean = false,
1016
val defaultZigZag: Boolean = false
@@ -14,26 +20,32 @@ data class PackedConfiguration(
1420
* Compact `BinaryFormat` optimized for small, flat Kotlin data classes.
1521
*
1622
* Features:
17-
* - Booleans and nullability encoded as bitmasks
18-
* - Optional varint / zig-zag via `@VarInt` / `@VarUInt` annotations
19-
* - Fixed, deterministic field order based on declaration
23+
* - Booleans and nullability encoded as compact bitmasks in a class header.
24+
* - Optional varint / zig-zag encoding via `@VarInt` / `@VarUInt` annotations, or globally via [PackedConfiguration].
25+
* - Fixed, deterministic field order based on declaration.
2026
*
2127
* Limitations:
22-
* - No nested objects, collections, or maps
23-
* - No polymorphism
28+
* - Nested objects and collections are supported, but do not share bitmasks across structural boundaries.
29+
* - Polymorphism support is limited/unoptimized compared to full-featured formats.
30+
*
31+
* Use the [PackedFormat] builder function to create a customized instance.
2432
*
25-
* For richer models, use `ProtoBuf` (or another [BinaryFormat]) with [com.eignex.kencode.EncodedFormat].
33+
* @property configuration The active configuration for default varint/zigzag behaviors.
34+
* @property serializersModule The module used to resolve contextual and polymorphic serializers.
2635
*/
2736
@OptIn(ExperimentalSerializationApi::class)
2837
open class PackedFormat(
2938
val configuration: PackedConfiguration = PackedConfiguration(),
3039
override val serializersModule: SerializersModule = EmptySerializersModule()
3140
) : BinaryFormat {
3241

42+
/**
43+
* Default instance with no default varint/zigzag optimizations applied automatically.
44+
*/
3345
companion object Default : PackedFormat()
3446

3547
/**
36-
* Encodes [value] into a compact binary representation using [PackedEncoder].
48+
* Encodes [value] into a compact binary representation.
3749
*/
3850
override fun <T> encodeToByteArray(
3951
serializer: SerializationStrategy<T>, value: T
@@ -45,7 +57,7 @@ open class PackedFormat(
4557
}
4658

4759
/**
48-
* Decodes [bytes] produced by [PackedFormat] using [PackedDecoder].
60+
* Decodes [bytes] produced by [PackedFormat] back into an object of type [T].
4961
*/
5062
override fun <T> decodeFromByteArray(
5163
deserializer: DeserializationStrategy<T>, bytes: ByteArray
@@ -55,13 +67,40 @@ open class PackedFormat(
5567
}
5668
}
5769

58-
70+
/**
71+
* Builder for configuring [PackedFormat] instances.
72+
*/
5973
class PackedFormatBuilder {
74+
/**
75+
* The module containing contextual and polymorphic serializers.
76+
*/
6077
var serializersModule: SerializersModule = EmptySerializersModule()
78+
79+
/**
80+
* If true, applies variable-length integer encoding to all `Int` and `Long` fields by default.
81+
*/
6182
var defaultVarInt: Boolean = false
83+
84+
/**
85+
* If true, applies ZigZag encoding to all `Int` and `Long` fields by default.
86+
* Overrides [defaultVarInt] as ZigZag inherently implies variable-length encoding.
87+
*/
6288
var defaultZigZag: Boolean = false
6389
}
6490

91+
/**
92+
* Creates a customized [PackedFormat] instance.
93+
*
94+
* ```
95+
* val format = PackedFormat {
96+
* defaultZigZag = true
97+
* serializersModule = myCustomModule
98+
* }
99+
* ```
100+
*
101+
* @param from An existing [PackedFormat] instance to copy defaults from.
102+
* @param builderAction The configuration block.
103+
*/
65104
fun PackedFormat(
66105
from: PackedFormat = PackedFormat.Default,
67106
builderAction: PackedFormatBuilder.() -> Unit

src/test/kotlin/com/eignex/kencode/Examples.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.eignex.kencode
22

3-
import PackedFormat
43
import kotlinx.serialization.*
54
import kotlinx.serialization.protobuf.ProtoBuf
65
import org.bouncycastle.jce.provider.BouncyCastleProvider

src/test/kotlin/com/eignex/kencode/PackedFormatTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
package com.eignex.kencode
44

5-
import PackedFormat
65
import kotlinx.serialization.KSerializer
76
import kotlinx.serialization.builtins.nullable
87
import kotlinx.serialization.builtins.serializer

0 commit comments

Comments
 (0)