Skip to content

Instantly share code, notes, and snippets.

@mbezjak
mbezjak / error-templates.edn
Last active January 12, 2023 16:25
Wrapping malli to produce own errors
{:malli/required "%s is required"
:malli/extra-key "System doesn't recognize property %s"
:malli/null "%s must be empty"
:malli/some "%s must not be null"
:malli/invalid-type "%s has invalid type"
:malli/invalid "%s value is invalid"
:malli/not "%s value is invalid"
:malli/boolean "%s must be a boolean"
:malli/true "%s must have a value of `true`"
:malli/false "%s must have a value of `false`"
@mbezjak
mbezjak / either.clj
Last active January 15, 2023 22:45
Biased either monad
(ns my.company.either
"An implementation of a right-biased Either monad.
Representation:
- `success`: any success value (even `nil`)
- `failure`: represented as `errors`
In classical Either monad terms:
- `right` is `success`
- `left` is `failure`
@mbezjak
mbezjak / errors.clj
Last active December 29, 2022 19:23
Additional functions for the error model
(ns my.company.errors
"Errors is a collection of `error`."
(:require
[my.company.coll :as coll]
[my.company.error :as error]))
;; Functions in addition to:
;; https://gist.github.com/mbezjak/c1baeece563b8ed734692938e6d1a36f
(defn validate
@mbezjak
mbezjak / error.clj
Created December 2, 2022 15:06
Error model
(ns my.company.error
"Error carries error information as a hash-map.
A hash-map can carry any information. Some information is system wide (thus
used by the system) and will be promoted as qualified keywords not to conflict
with user defined ones. Those are:
- `:code` [qualified keyword] - error code
- `:args` [collection] - arguments for `format` to build the error message
- `:message` [string] - a human error message
- `:breadcrumbs` [collection] - a path to the source that caused the error
@mbezjak
mbezjak / coll.clj
Last active November 30, 2022 12:37
clojure.core extensions
(ns my.company.coll
"Missing functions in `clojure.core` that accept a collection.
Argument names and expected positions use the same convention as `clojure.core.`"
(:refer-clojure :exclude [any?]))
(defn any?
"Opposite of `clojure.core/not-any?`.
Returns `false` for empty `coll`."
@mbezjak
mbezjak / defn.clj
Created November 14, 2022 11:44
Clojure Debugging Helpers
(ns defn)
(defn symbols [args]
(mapcat
(fn [a]
(cond
(map? a) (:keys a)
(vector? a) (symbols a)
:else [a]))
args))
@mbezjak
mbezjak / model.edn
Last active December 6, 2022 11:51
Data described applications at helix.hr
(defmodel :employee
[[:integer id :not-form-updatable]
[:string first-name {:sql-column "name" :case :insensitive}]
[:string last-name {:max-size 100 :case :upper}]
[:string address]
[:country country :nullable]])
(defmodel :country
[[:string alpha2]
[:string alpha3]
@mbezjak
mbezjak / generate-projects-and-run-pride.sh
Created July 17, 2015 11:31
Trying out pride 0.10 & 0.11-rc-1
#!/bin/bash
#
set -o errexit
declare -r projectsdir=/tmp/pride-example-projects
declare -r libdir=$projectsdir/lib
declare -r appdir=$projectsdir/app
declare -r pridedir=$projectsdir/pride
declare -r maindir=src/main/groovy/com/example
@mbezjak
mbezjak / CometService.groovy
Created April 6, 2012 10:59
Reset session timout on resource destroy in grails
import org.atmosphere.cpr.AtmosphereResource
import org.atmosphere.cpr.AtmosphereResourceEvent
class CometService {
static transactional = false
static final atmosphere = [ mapping : '/atmosphere/messages' ]
final onRequest = { AtmosphereResource resource ->
// ...