Skip to content

Commit

Permalink
feat: change library architecture.
Browse files Browse the repository at this point in the history
  • Loading branch information
EdmonDantes committed Dec 17, 2023
1 parent 794ffea commit 1fc3993
Show file tree
Hide file tree
Showing 119 changed files with 7,612 additions and 3,561 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.version.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
simplekmm = "0.6.3"
kotlin_serialization_version = "1.6.0"
simplekmm = "0.7.0"
kotlin_serialization_version = "1.6.2"

[libraries]
kotlin_serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlin_serialization_version" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,55 @@ import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* A delegate class that enhances the functionality of a [BinaryFormat] implementation by allowing modification of the serialization and deserialization process.
* The modifications are done through transformation functions provided by the user.
* This delegate class implements the [BinaryFormat] interface and delegates all method calls to the provided [delegate] instance.
*
* @property delegate The underlying [BinaryFormat] instance to delegate the method calls to.
* @property serializationModify The transformation function to modify the serialization process.
* @property deserializationModify The transformation function to modify the deserialization process.
* @see BinaryFormat
* @see StringFormatDelegate
*/
public class BinaryFormatDelegate(
private val delegate: BinaryFormat,
private val serializationModify: Encoder.() -> Encoder = { this },
private val deserializationModify: Decoder.() -> Decoder = { this },
) : BinaryFormat by delegate {
override fun <T> encodeToByteArray(
serializer: SerializationStrategy<T>,
value: T,
): ByteArray = delegate.encodeToByteArray(CustomSerializationStrategy(serializer) { it.serializationModify() }, value)

override fun <T> encodeToByteArray(serializer: SerializationStrategy<T>, value: T): ByteArray =
delegate.encodeToByteArray(CustomSerializationStrategy(serializer) { it.serializationModify() }, value)

override fun <T> decodeFromByteArray(deserializer: DeserializationStrategy<T>, bytes: ByteArray): T =
delegate.decodeFromByteArray(CustomDeserializationStrategy(deserializer) { it.deserializationModify() }, bytes)
override fun <T> decodeFromByteArray(
deserializer: DeserializationStrategy<T>,
bytes: ByteArray,
): T = delegate.decodeFromByteArray(CustomDeserializationStrategy(deserializer) { it.deserializationModify() }, bytes)
}

/**
* Modifies a [BinaryFormat] instance by applying the provided transformation [block] to the serialization process.
*
* @param block The transformation function to modify the serialization process.
* The function takes an original [Encoder] instance and returns an [Encoder], that will be used in serialization.
*
* @return A modified [BinaryFormat] instance that applies the given transformation function to the serialization process.
* @see BinaryFormat
* @see Encoder
*/
public fun BinaryFormat.modifySerializer(block: Encoder.() -> Encoder): BinaryFormat =
BinaryFormatDelegate(this, serializationModify = block)

/**
* Modifies a [BinaryFormat] instance by applying the provided transformation [block] to the deserialization process.
*
* @param block The transformation function to modify the deserialization process.
* The function takes an original [Decoder] instance and returns an [Decoder], that will be used in serialization.
*
* @return A modified [BinaryFormat] instance that applies the given transformation function to the deserialization process.
* @see BinaryFormat
* @see Decoder
*/
public fun BinaryFormat.modifyDeserializer(block: Decoder.() -> Decoder): BinaryFormat =
BinaryFormatDelegate(this, deserializationModify = block)
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,55 @@ import kotlinx.serialization.StringFormat
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* A delegate class that enhances the functionality of a [StringFormat] implementation by allowing modification of the serialization and deserialization process.
* The modifications are done through transformation functions provided by the user.
* This delegate class implements the [StringFormat] interface and delegates all method calls to the provided [delegate] instance.
*
* @property delegate The underlying [StringFormat] instance to delegate the method calls to.
* @property serializationModify The transformation function to modify the serialization process.
* @property deserializationModify The transformation function to modify the deserialization process.
* @see StringFormat
* @see BinaryFormatDelegate
*/
public class StringFormatDelegate(
private val delegate: StringFormat,
private val serializationModify: Encoder.() -> Encoder = { this },
private val deserializationModify: Decoder.() -> Decoder = { this },
) : StringFormat by delegate {
override fun <T> encodeToString(
serializer: SerializationStrategy<T>,
value: T,
): String = delegate.encodeToString(CustomSerializationStrategy(serializer) { it.serializationModify() }, value)

override fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String =
delegate.encodeToString(CustomSerializationStrategy(serializer) { it.serializationModify() }, value)

override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T =
delegate.decodeFromString(CustomDeserializationStrategy(deserializer) { it.deserializationModify() }, string)
override fun <T> decodeFromString(
deserializer: DeserializationStrategy<T>,
string: String,
): T = delegate.decodeFromString(CustomDeserializationStrategy(deserializer) { it.deserializationModify() }, string)
}

/**
* Modifies a [StringFormat] instance by applying the provided transformation [block] to the serialization process.
*
* @param block The transformation function to modify the serialization process.
* The function takes an original [Encoder] instance and returns an [Encoder], that will be used in serialization.
*
* @return A modified [StringFormat] instance that applies the given transformation function to the serialization process.
* @see StringFormat
* @see Encoder
*/
public fun StringFormat.modifySerializer(block: Encoder.() -> Encoder): StringFormat =
StringFormatDelegate(this, serializationModify = block)

/**
* Modifies a [StringFormat] instance by applying the provided transformation [block] to the deserialization process.
*
* @param block The transformation function to modify the deserialization process.
* The function takes an original [Decoder] instance and returns an [Decoder], that will be used in serialization.
*
* @return A modified [StringFormat] instance that applies the given transformation function to the deserialization process.
* @see StringFormat
* @see Decoder
*/
public fun StringFormat.modifyDeserializer(block: Decoder.() -> Decoder): StringFormat =
StringFormatDelegate(this, deserializationModify = block)
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
*/
package io.github.edmondantes.serialization.annotation

import io.github.edmondantes.serialization.decoding.UniqueCompositeDecoder
import io.github.edmondantes.serialization.decoding.UniqueDecoder
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo
import kotlinx.serialization.encoding.Decoder

/**
* This annotation allows decoding for [UniqueDecoder] or [UniqueCompositeDecoder] with an id that is contained in [allow].
* Other [Decoder]s will be ignored
*/
@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
@OptIn(ExperimentalSerializationApi::class)
public annotation class EncodeBy(val id: String) {
public companion object {
public const val SELF: String = ""
}
}
public annotation class AllowDecoder(val allow: Array<String> = [])
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@
*/
package io.github.edmondantes.serialization.annotation

import io.github.edmondantes.serialization.encoding.FilterCompositeEncoder
import io.github.edmondantes.serialization.encoding.FilterEncoder
import io.github.edmondantes.serialization.encoding.UniqueCompositeEncoder
import io.github.edmondantes.serialization.encoding.UniqueEncoder
import io.github.edmondantes.serialization.encoding.filter.FilterCompositeEncoder
import io.github.edmondantes.serialization.encoding.filter.FilterEncoder
import io.github.edmondantes.serialization.encoding.filter.filterBy
import io.github.edmondantes.serialization.encoding.filter.filterByIdentifier
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo
import kotlinx.serialization.encoding.Encoder

/**
* For enable with feature please use [filterBy] or [filterByIdentifier]
* This annotation allows encoding for [UniqueEncoder] or [UniqueCompositeEncoder] with an id that is contained in [allow].
* Other encoder will be ignored
* Other [Encoder]s will be ignored
*
* @see FilterEncoder
* @see FilterCompositeEncoder
* @see filterBy
* @see filterIsInstance
*/
@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2023. Ilia Loginov
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.edmondantes.serialization.annotation

import io.github.edmondantes.serialization.decoding.UniqueCompositeDecoder
import io.github.edmondantes.serialization.decoding.UniqueDecoder
import io.github.edmondantes.serialization.encoding.filter.FilterCompositeEncoder
import io.github.edmondantes.serialization.encoding.filter.FilterEncoder
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo
import kotlinx.serialization.encoding.Decoder

/**
* This annotation ignores encoding for [UniqueDecoder] or [UniqueCompositeDecoder] with an id that is contained in [ignore].
* Other [Decoder]s will be allowed
* @see FilterEncoder
* @see FilterCompositeEncoder
*/
@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
@OptIn(ExperimentalSerializationApi::class)
public annotation class IgnoreDecoder(val ignore: Array<String> = [])
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
*/
package io.github.edmondantes.serialization.annotation

import io.github.edmondantes.serialization.encoding.FilterCompositeEncoder
import io.github.edmondantes.serialization.encoding.FilterEncoder
import io.github.edmondantes.serialization.encoding.UniqueCompositeEncoder
import io.github.edmondantes.serialization.encoding.UniqueEncoder
import io.github.edmondantes.serialization.encoding.filter.FilterCompositeEncoder
import io.github.edmondantes.serialization.encoding.filter.FilterEncoder
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo
import kotlinx.serialization.encoding.Encoder

/**
* This annotation ignores encoding for [UniqueEncoder] or [UniqueCompositeEncoder] with an id that is contained in [ignore].
* Other encoder will be allowed
* Other [Encoder]s will be allowed
* @see FilterEncoder
* @see FilterCompositeEncoder
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package io.github.edmondantes.serialization.annotation
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo

/**
* Marks property or class is inline to parent
*/
@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
@OptIn(ExperimentalSerializationApi::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2023. Ilia Loginov
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.edmondantes.serialization.annotation

import io.github.edmondantes.serialization.annotation.SerializationFormat.Companion.SELF
import io.github.edmondantes.serialization.encoding.format.FormatEncoder
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo

/**
* Marks which format [FormatEncoder] should use to serialization
* @param id format's id for serialization. If it is [SELF], encode will use original format
*/
@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
@OptIn(ExperimentalSerializationApi::class)
public annotation class SerializationFormat(val id: String) {
public companion object {
/**
* Marks original format in annotation [SerializationFormat]
*/
public const val SELF: String = ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
package io.github.edmondantes.serialization.decoding

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

/**
* This class helps for transform [Decoder] in deserialization process
* @param delegate Parent [DeserializationStrategy]
* @param decoderTransformer Function which transform [Decoder]
* @see [SerializationStrategy]
* @see [Encoder]
*/
public class CustomDeserializationStrategy<T>(
private val delegate: DeserializationStrategy<T>,
private val decoderTransformer: (Decoder) -> Decoder,
) : DeserializationStrategy<T> by delegate {

override fun deserialize(decoder: Decoder): T =
delegate.deserialize(decoderTransformer(decoder))
override fun deserialize(decoder: Decoder): T = delegate.deserialize(decoderTransformer(decoder))
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.edmondantes.serialization.encoding
package io.github.edmondantes.serialization.decoding

import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.CompositeDecoder

public interface ConstructEncoder<T> : Encoder {

public fun finishConstruct(): T
/**
* This interface describes an [CompositeDecoder] which has unique id
* @see CompositeDecoder
*/
public interface UniqueCompositeDecoder : CompositeDecoder {
public val id: String
}

0 comments on commit 1fc3993

Please sign in to comment.