Skip to content

Instantly share code, notes, and snippets.

@timmc
Last active December 16, 2015 03:58
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 timmc/5373281 to your computer and use it in GitHub Desktop.
Save timmc/5373281 to your computer and use it in GitHub Desktop.
Templating of deftypes
(deftype MultiErrorReporter
[reporters]
ErrorReporter
(constraint-error [_ state constraint]
(into {} (for [[k ^ErrorReporter rep] reporters]
[k (. rep (constraint-error state constraint))])))
(extraneous-path-error [_ state extra-path]
(into {} (for [[k ^ErrorReporter rep] reporters]
[k (. rep (extraneous-path-error state extra-path))])))
(missing-path-error [_ state missing-path]
(into {} (for [[k ^ErrorReporter rep] reporters]
[k (. rep (missing-path-error state missing-path))])))
(predicate-fail-error [_ state val-at-path pred]
(into {} (for [[k ^ErrorReporter rep] reporters]
[k (. rep (predicate-fail-error state val-at-path pred))])))
(instance-of-fail-error [_ state val-at-path expected-class]
(into {} (for [[k ^ErrorReporter rep] reporters]
[k (. rep (instance-of-fail-error state val-at-path expected-class))])))
(pre-validation-transform-error [_ state val-at-path pre-validation-transform-fn]
(into {} (for [[k ^ErrorReporter rep] reporters]
[k (. rep (pre-validation-transform-error
state val-at-path pre-validation-transform-fn))]))))
(inject-forms
(deftype MultiErrorReporter
[reporters]
ErrorReporter
^:splice methods)
[methods `(do-template
[method argvec] (method argvec
(into {} (for [[k ^ErrorReporter rep] reporters]
[k (. rep (name ~@argvec))]))) ;; yes, I'm aware ~@ doesn't work here -- it's to show my intent
(constraint-error [_ state constraint])
(extraneous-path-error [_ state extra-path])
(missing-path-error [_ state missing-path])
(predicate-fail-error [_ state val-at-path pred])
(instance-of-fail-error [_ state val-at-path expected-class])
(pre-validation-transform-error
[_ state val-at-path pre-validation-transform-fn]))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment