Skip to content

Instantly share code, notes, and snippets.

View bseib's full-sized avatar

Broc Seib bseib

View GitHub Profile
@bseib
bseib / JavalinPebble32.kt
Created November 30, 2024 15:36
Javalin Pebble class for Pebble >= v3.2.x
package io.javalin.rendering.template
import io.javalin.http.Context
import io.javalin.rendering.FileRenderer
import io.javalin.rendering.util.RenderingDependency.PEBBLE
import io.javalin.util.DependencyUtil
import io.javalin.util.OptionalDependency
import io.pebbletemplates.pebble.PebbleEngine
import io.pebbletemplates.pebble.loader.ClasspathLoader
import java.io.StringWriter
@bseib
bseib / gist:25d41027d0d61eef9c1c13c51b864d11
Created May 22, 2024 14:06
Multiple `addn-hosts` are allowed in `dnsmasq.conf`
##
## Re: https://stackoverflow.com/questions/48644841/multiple-addn-hosts-conf-in-dnsmasq
##
## You may specify multiple addn-hosts on dnsmasq.conf
addn-hosts=/srv/dnsmasq/dns-configs/db.10.0.31.hosts
addn-hosts=/srv/dnsmasq/dns-configs/db.10.0.32.hosts
## :
## :
## etc
## :
@bseib
bseib / IncorrectConditionIsAlwaysFalse.kt
Created May 15, 2023 20:25
IntelliJ IDE gives incorrect warning: "Condition 'list.size > 0' is always false"
package bugs
import kotlin.random.Random.Default.nextBoolean
object IncorrectConditionIsAlwaysFalse {
private fun conditionA() = nextBoolean()
private fun conditionB() = nextBoolean()
data class Action(val list: List<Int>)
@bseib
bseib / barcode-webfont-example.html
Created March 23, 2023 15:05
Demonstrates use of a barcode webfont in a single HTML file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Barcode Webfont Example</title>
<link href="https://fonts.googleapis.com/css2?family=Libre+Barcode+39+Extended+Text&display=swap" rel="stylesheet">
<style type="text/css">
.barcode39 {
font-family: 'Libre Barcode 39 Extended Text', cursive;
@bseib
bseib / PrintoBarfo.vb
Created March 5, 2023 14:24
How to traverse list of printers and get the paper sizes available
Public Sub investigatePrinterPaperSizeSituation()
Dim printerList As PrinterSettings.StringCollection = PrinterSettings.InstalledPrinters()
For Each printer As String In printerList
_logger.logInfo("found printer: " & printer)
Dim settings As PrinterSettings = new PrinterSettings()
'' So...., check this out:
settings.PrinterName = printer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
@bseib
bseib / LocalDateExt.kt
Created May 22, 2022 21:32
Kotlin `LocalDate` extension functions to shift to a particular weekday, and US Holidays
import java.time.DayOfWeek
import java.time.LocalDate
fun LocalDate.lastDayOfWeek(dow: DayOfWeek): LocalDate {
val minus = (this.dayOfWeek.value + 7 - dow.value) % 7
return this.minusDays(minus.toLong())
}
fun LocalDate.firstDayOfWeek(dow: DayOfWeek): LocalDate {
val plus = (dow.value + 7 - this.dayOfWeek.value) % 7
@bseib
bseib / AccessRequirement.kt
Last active May 22, 2022 20:20
Succinct and/or composable/nestable role based access control
sealed class AccessRequirement<R> {
fun isSatisfiedByRoles(callerRoles: Set<R>): Boolean {
return when (this) {
is AlwaysAllow -> true
is NeverAllow -> false
is RequireRole -> callerRoles.contains(role)
is AnyOfRoles -> (callerRoles intersect anyOfRoles).isNotEmpty()
is AllOfRoles -> callerRoles.containsAll(allOfRoles)
is AnyOfAccessRequirements -> anyOf.any { it.isSatisfiedByRoles(callerRoles) }
is AllOfAccessRequirements -> allOf.all { it.isSatisfiedByRoles(callerRoles) }
@bseib
bseib / TestJacksonWithOptional.kt
Created November 4, 2020 15:40
Distinguish nulls and non-present values with Jackson, Optional<T>, and Kotlin
package testing
import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.util.*
class TestJacksonWithOptional {
val defaultObjectMapper = ObjectMapper().apply {
@bseib
bseib / Example.kt
Created September 3, 2020 14:53
A clean way to consume an input stream into a buffer using a sequence
fun inputStreamToTrimmedString(inputStream: InputStream): String {
val baos = ByteArrayOutputStream()
val buffer = ByteArray(128)
inputStream.bufferedSequence(buffer) { bytesRead -> baos.write(buffer, 0, bytesRead) }
return baos.toString("UTF-8").trim()
}
@bseib
bseib / BooleanNamingStrategy.kt
Created July 30, 2020 05:14
Don't let Jackson drop the "is" prefix when serializing a Kotlin class to JSON
// adapted from https://stackoverflow.com/a/51419993/516910
package to.pubsub.util
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.databind.cfg.MapperConfig
import com.fasterxml.jackson.databind.introspect.AnnotatedField
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
class BooleanNamingStrategy : PropertyNamingStrategy() {