Skip to content

Instantly share code, notes, and snippets.

@xdevs23
Last active November 17, 2022 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdevs23/915fe785b765adc9739e39baf589c0d1 to your computer and use it in GitHub Desktop.
Save xdevs23/915fe785b765adc9739e39baf589c0d1 to your computer and use it in GitHub Desktop.
A collection of Kotlin files. Batteries not included.
object Env { operator fun get(name: String): String? = System.getenv(name) }

Kotlin file collection

A collection of Kotlin files – licensed under the MIT License.

suspend inline fun <reified T : Any> ApplicationCall.receive(then: (T) -> Unit) = receive<T>().let(then)
/*
Let's you do this:
call.receive { book: Book ->
}
*/
package lib.extensions
inline val Any.canonicalClassName get() = this::class.java.canonicalName
Copyright (c) 2022 Simão Gomes Viana
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
import mu.KLogger
import mu.KotlinLogging
import java.io.InputStream
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
class DelegatedLogger(name: String) {
private val logger by lazy { KotlinLogging.logger(name) }
operator fun getValue(thisRef: Any?, property: KProperty<*>): KLogger {
return logger
}
}
class DelegatedClassLogger<THIS : Any>(klass: KClass<THIS>) {
private val logger by lazy { KotlinLogging.logger(klass.java.canonicalName) }
operator fun getValue(thisRef: THIS, property: KProperty<*>): KLogger {
return logger
}
}
fun namedLogger(name: String) = DelegatedLogger(name)
inline fun <reified THIS : Any> THIS.classLogger() = DelegatedClassLogger(THIS::class)
inline val <reified THIS : Any> THIS.classLogger get() = classLogger()
infix fun InputStream.pipeInto(logger: KLogger) =
use { bufferedReader().useLines { l -> l.forEach { logger.info("console > $it") } } }
fun Process.consolePipedInto(logger: KLogger) = apply { inputStream pipeInto logger }
// If you need an elegant inline fun with a reified type parameter,
// but you can't do it because you have private members or for another
// reason, then you can use this method (only looks better, performs same as
// using the non-reified version directly)
fun example(klass: KClass<*>) { /* your logic */ }
inline fun <reified T> example() = example(T::class)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment