Skip to content

Instantly share code, notes, and snippets.

@bastman
bastman / InstantRangeDeserializer.kt
Last active Aug 3, 2022
jackson-module-kotlin: InstantRangeDeserializer - How to deserialize ClosedRange<Instant> - fix: Cannot construct instance of `kotlin.ranges.ClosedRange`
View InstantRangeDeserializer.kt
/*
InstantRangeDeserializer.kt
jackson-module-kotlin: InstantRangeDeserializer - How to deserialize ClosedRange<Instant>
Type definition error: [simple type, class kotlin.ranges.ClosedRange];
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `kotlin.ranges.ClosedRange` (no Creators, like default constructor, exist):
@bastman
bastman / SlidingWindowExamples.kt
Last active Dec 14, 2021
sliding-window-algorithm-technique: examples (kotlin)
View SlidingWindowExamples.kt
// see: https://itnext.io/sliding-window-algorithm-technique-6001d5fbe8b3
fun main() {
intro()
`Maximum sum subarray of Size k`()
//`Count Occurrences of Anagram`()
}
fun intro() {
@bastman
bastman / JacksonJmesPathExt.kt
Created Aug 4, 2021
jmespath extensions for jackson
View JacksonJmesPathExt.kt
fun ObjectMapper.convertValueAsJsonNode(value:Any?):JsonNode = convertValue(value, JsonNode::class.java)
inline fun <reified T> JsonNode.jq(
query: String,
noinline convert: (Any) -> T
): T {
val isNullable: Boolean = null is T
try {
val expression: Expression<JsonNode> = JmesPathJackson.compile(query)
@bastman
bastman / DoubleExt.kt
Last active Jul 27, 2021
fun Double.round(decimals:Int)
View DoubleExt.kt
// see discussion on alternatives here: https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/15
fun Double.round(decimals:Int, roundingMode:RoundingMode=RoundingMode.HALF_EVEN):Double =
toBigDecimal().setScale(decimals, roundingMode).toDouble()
# ???
fun Double.round2(decimals: Int): Double {
val locale:Locale = Locale.US
val aTxt:String = String.format(locale, "%.${decimals+1}f", this)
val aDouble:Double = aTxt.toDouble()
@bastman
bastman / jmespath-groupBy.kt
Created Jul 14, 2021
groupBy impl for JMESPath (proof-of-concept)
View jmespath-groupBy.kt
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.ObjectNode
import io.burt.jmespath.Adapter
import io.burt.jmespath.JmesPath
import io.burt.jmespath.JmesPathType
import io.burt.jmespath.RuntimeConfiguration
import io.burt.jmespath.function.ArgumentConstraints
import io.burt.jmespath.function.BaseFunction
@bastman
bastman / find_duplicate_row_ids.sql
Last active Jun 25, 2020
postgres: explore primary keys
View find_duplicate_row_ids.sql
SELECT my_id, COUNT(my_id)
FROM my_table
GROUP BY my_id
HAVING COUNT(my_id) > 1;
@bastman
bastman / k8s-create-job-from-cronjob.sh
Last active Jun 16, 2020
k8s create job from cronjob (how to run a cronjob now + once)
View k8s-create-job-from-cronjob.sh
$ kubectl create job --from=cronjob/<CRONJOB-NAME> <NEW-JOB-NAME>
e.g.:
$ kubectl create job --from=cronjob/my-cron my-cron-manual-001
function k8s-cronjob-run() {
source_cronjob_name=$1
[ -z "$source_cronjob_name" ] && echo "Please provide source-cronjob-name !" && return
sink_run_id=$(date -u +"%Y-%m-%dt%H.%M.%Sz")
View jmespath-tricks.txt
Images[].{a:length(BlockDeviceMappings), b:@} | [?a>=`1`]
Images[?length(BlockDeviceMappings)>=`0`]