Skip to content

Instantly share code, notes, and snippets.

View jonnyzzz's full-sized avatar

Eugene Petrenko jonnyzzz

View GitHub Profile
class OpenSSLContainer : GenericContainer<OpenSSLContainer>(
ImageFromDockerfile().withDockerfileFromBuilder { it
.from("ubuntu:20.04")
.env("DEBIAN_FRONTEND", "noninteractive")
.env("LC_ALL", "C.UTF-8")
.run("apt-get update && apt-get install -y openssl")
.run("openssl version")
//we need a running container to call `execInContainer` commands
.cmd("/bin/bash -c 'while true; do sleep 10; done'")
})
@jonnyzzz
jonnyzzz / build.gradle.kts
Last active November 11, 2020 12:24
Setup Micronaut with Kotlin
plugins {
java
kotlin("jvm") version "1.4.10"
kotlin("kapt") version "1.4.10"
id("org.jetbrains.kotlin.plugin.allopen") version "1.4.10"
id("io.micronaut.application") version "1.1.0"
}
micronaut {
version("2.1.3")
@jonnyzzz
jonnyzzz / Atomics.kt
Created June 9, 2020 16:25
Delegated Properties with Atomic References in Kotlin
class X
operator fun <Y> AtomicReference<Y>.getValue(x: Any?, p: KProperty<*>) : Y = this.get()
operator fun <Y> AtomicReference<Y>.setValue(x: Any?, p: KProperty<*>, value: Y) { this.set(value) }
val myStats by AtomicReference<X>(null)
import com.intellij.codeInsight.daemon.ProblemHighlightFilter
import com.intellij.concurrency.JobLauncher
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectCoreUtil
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithTests
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.getCurrentOperatingSystem
fun setupNative(name: String,
configure: KotlinNativeTargetWithTests.() -> Unit): KotlinNativeTargetWithTests {
val os = getCurrentOperatingSystem()
return when {
os.isLinux -> kotlin.linuxX64(name, configure)
os.isWindows -> kotlin.mingwX64(name, configure)
@jonnyzzz
jonnyzzz / kafka.kt
Created March 19, 2019 12:00
DSL in Kotlin
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
/** API **/
data class ProcessorId(val name: String)
data class SourceId(val name: String)
interface ProcessorDelegate {
operator fun provideDelegate(thisRef: Any?,
prop: KProperty<*>) : ReadOnlyProperty<Any?, ProcessorId>
class Z {
operator fun plus(z:Z) = z
operator fun plusAssign(z:Z) = Unit
}
fun test() {
val x = Z() + Z()
x += Z()
}
fun callWithLambda(x: () -> Unit) = x()
inline fun callW(crossinline x: () -> Unit) {
callWithLambda {
x()
x()
}
}
@jonnyzzz
jonnyzzz / catchAll.kt
Created December 15, 2016 17:54
Catch all exceptions wrapper in Kotlin
inline fun catchAll(LOG: Logger, message: String, action: () -> Unit) {
try {
action()
} catch (t: Throwable) {
LOG.warn("Failed to $message. ${t.message}", t)
}
}
@jonnyzzz
jonnyzzz / GuardedByLock.kt
Created December 15, 2016 17:53
Make sure state is accessed only by lock in Kotlin
class GuardedByLock<out T>(
val lock : Lock,
val state : T
) {
inline fun <Y> withLock(action : T.() -> Y) = lock.withLock { state.action() }
}