Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No-Op Implementation #4

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
}

repositories {
Expand Down
48 changes: 16 additions & 32 deletions src/main/kotlin/ktee/KTee.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ktee

import ktee.KTee.Companion.debug
import org.slf4j.Logger

class KTee { companion object { var debug = true } }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we actively avoid using var in our codebase. This becomes a global variable which is not good in server/multithreaded/concurrent envs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made some changes. The variable can now be set only once.
If you are concerned about thread safety, @volatile can also be used instead.
Other than that, I fail to see how it's possible to have a toggle without being a var.


/**
* Prints the value to the stdout and returns the same value. Useful when chaining
Expand All @@ -12,71 +14,53 @@ import org.slf4j.Logger
* myList.map(fn).tee(">>> ").reduce(fn)
*
*/
fun <T> T.tee(marker: String = ""): T {
println(marker + this)
return this
}
fun <T> T.tee(marker: String = "") = apply { if (debug) println(marker + this) }

/**
*
* executes the lambda with the value of the chain and writes the
*
*/
inline fun <T> T.tee(fn: (T) -> String): T {
println(fn(this))
return this
}
inline fun <T> T.tee(fn: (T) -> String) = apply { if (debug) println(fn(this)) }

/**
* logs the value to the given logger at info level. Message can be customized using message parameter
*/
fun <T> T.teeToInfo(logger: Logger, message: String = "{}"): T {
logger.info(message, this)
return this
}
fun <T> T.teeToInfo(logger: Logger, message: String = "{}")
= apply { if (debug) logger.info(message, this) }

/**
* Evaluates the lambda and logs the result (of evaluation) to the given logger at info level
*
*/
inline fun <T> T.teeToInfo(logger: Logger, fn: (T) -> String): T {
logger.info(fn(this), this)
return this
}
inline fun <T> T.teeToInfo(logger: Logger, fn: (T) -> String)
= apply { if (debug) logger.info(fn(this), this) }

/**
* logs the value to the given logger at info level. Message can be customized using message parameter
*/
fun <T> T.teeToDebug(logger: Logger, message: String = "{}"): T {
logger.debug(message, this)
return this
}
fun <T> T.teeToDebug(logger: Logger, message: String = "{}")
= apply { if (debug) logger.debug(message, this) }

/**
* Evaluates the lambda and logs the result (of evaluation) to the given logger at debug level
*
*/
inline fun <T> T.teeToDebug(logger: Logger, fn: (T) -> String): T {
logger.debug(fn(this), this)
return this
}
inline fun <T> T.teeToDebug(logger: Logger, fn: (T) -> String)
= apply { if (debug) logger.debug(fn(this), this) }

/**
* logs the value to the given logger at trace level. Message can be customized using message parameter
*/
fun <T> T.teeToTrace(logger: Logger, message: String = "{}"): T {
logger.trace(message, this)
return this
}
fun <T> T.teeToTrace(logger: Logger, message: String = "{}")
= apply { if (debug) logger.trace(message, this) }

/**
* Evaluates the lambda and logs the result (of evaluation) to the given logger at trace level
*
*/
inline fun <T> T.teeToTrace(logger: Logger, fn: (T) -> String): T {
logger.trace(fn(this), this)
return this
}
inline fun <T> T.teeToTrace(logger: Logger, fn: (T) -> String)
= apply { if (debug) logger.trace(fn(this), this) }



Expand Down