Skip to content

Instantly share code, notes, and snippets.

@90K2
90K2 / Trie.kt
Last active July 8, 2022 09:07
Kotlin realization of Trie data structure
class TrieNode<T>(
val key: String?, var value: T?
) {
var parent: TrieNode<T>? = null
val children: HashMap<String, TrieNode<T>> = hashMapOf()
var end: Boolean = false
fun getWord(): String {
val output = mutableListOf<String?>()
var node: TrieNode<T>? = this
@90K2
90K2 / Extension.kt
Created July 12, 2022 17:58
Kotlin extension for serialization data objects with @JsonProperty annotated values instead of field names. Reflextion style
fun Any.forceSerialize(separator: String, sorted: Boolean = false): String {
var fieldNameToAnnotatedNameMap = this.javaClass.declaredFields.map { it.name }.associateWith { fieldName ->
val jsonFieldName =
this::class.primaryConstructor?.parameters?.first { it.name == fieldName }?.annotations?.firstOrNull { it is JsonProperty }
val serializedName = if (jsonFieldName != null) (jsonFieldName as JsonProperty).value else fieldName
serializedName
}
if (sorted)
fieldNameToAnnotatedNameMap = fieldNameToAnnotatedNameMap.toList().sortedBy { (_, value) -> value}.toMap()
return fieldNameToAnnotatedNameMap.entries.joinToString(separator) { e ->
@90K2
90K2 / TelegramVerifier.kt
Created July 13, 2022 10:49
Kotlin implementation of telegram verification flow
@JsonIgnoreProperties(ignoreUnknown = true)
data class TelegramAuthRequestDTO(
@JsonProperty("auth_date")
val authDate: Long,
@JsonProperty("first_name")
val firstName: String?,
@JsonProperty("last_name")
val lastName: String?,
import kotlinx.coroutines.reactor.mono
import org.ton.api.tonnode.TonNodeBlockIdExt
import org.ton.bigint.BigInt
import org.ton.block.*
import org.ton.crypto.base64
import org.ton.lite.api.liteserver.LiteServerBlockTransactions
import org.ton.lite.api.liteserver.LiteServerTransactionId3
import org.ton.lite.client.LiteClient
import reactor.kotlin.core.publisher.toFlux
import kotlinx.coroutines.reactor.mono
import org.ton.api.tonnode.TonNodeBlockId
import org.ton.api.tonnode.TonNodeBlockIdExt
import org.ton.bigint.BigInt
import org.ton.block.*
import org.ton.crypto.base64
import org.ton.lite.api.liteserver.LiteServerBlockTransactions
import org.ton.lite.api.liteserver.LiteServerTransactionId3
import org.ton.lite.client.LiteClient
import reactor.kotlin.core.publisher.toFlux
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
private inline fun < reified T: Any> downloadData(link: String, key: String, offset: String?): T {
val url = link.toHttpUrl()
offset?.let { url.newBuilder().addQueryParameter("offset", offset).build() }
val response = OkHttpClient().newCall(
Request.Builder()
.url(url)
@90K2
90K2 / Extension.kt
Created August 10, 2022 13:52
Serialize Kotlin data class constructor fields based on @JsonProperty annotations into string json. Nested non primitive fields will be serialized also but without any Annotations driving
fun Any.toJsonV2(): String {
var fieldNameToAnnotatedNameMap = this@toJsonV2.javaClass.declaredFields.map { it.name }.associateWith { fieldName ->
val jsonFieldName =
this@toJsonV2::class.primaryConstructor?.parameters?.first { it.name == fieldName }?.annotations?.firstOrNull { it is JsonProperty }
val serializedName = if (jsonFieldName != null) (jsonFieldName as JsonProperty).value else fieldName
serializedName
}
return buildString {
append("{\n")
@90K2
90K2 / isHostAvailable.kt
Created August 18, 2022 10:10
check whether is host port available or not
private fun isHostAvailable(ip: Int, port: Int): Boolean {
return kotlin.runCatching {
Socket(Utils.ipv4IntToStr(ip), port)
true
}.getOrNull() ?: false
}
@90K2
90K2 / PageableLite.kt
Created October 4, 2022 11:25
Spring pageable extension. Fixed current page number, reduced pageable and sorting extra fields, map function for content transformation saved
import org.springframework.data.domain.Page
class PageLite<T>(page: Page<T>) {
var content: List<T> = page.content
var pageable = PageableLite(
number = page.pageable.pageNumber + 1,
size = page.pageable.pageSize,
total = page.totalElements
)
@90K2
90K2 / liteClientBeanConfig.kt
Created December 15, 2022 13:46
LiteClient bean
@Bean("liteClientExtended")
fun liteClient(tonChainConfigReader: TonChainConfigReader): LiteClient {
val configList = tonChainConfigReader.load().liteservers
val nearestNodesList = mutableListOf<TonChainConfigReader.TonNetConfig.LiteServerParams>()
configList.forEach {
val liteClient = LiteClient(
LiteClientConfigGlobal(
liteservers = listOf(
LiteServerDesc(id = PublicKeyEd25519(base64(it.id.key)), ip = it.ip, port = it.port)