Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
awkay committed Oct 28, 2019
0 parents commit cfc60d2
Show file tree
Hide file tree
Showing 14 changed files with 3,602 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
*~
*.iml
*.log
*.sw?
*.swp
*jar
.DS_Store
.cljs_rhino_repl
.idea
.lein*
.lein-deps-sum
.lein-env
.lein-failures
.lein-plugins/
.lein-repl-history
.nrepl*
.nrepl-port
.repl
bin/publish-local
checkouts
classes
compiled
datahub.log*
docs/.asciidoctor/
docs/basic-db.png
docs/mutations.png
examples/calendar/resources/public/js/specs
examples/calendar/src/quiescent_model
examples/todo/src/quiescent_model
figwheel_server.log
lib
node_modules
out
resources/private/js
resources/public/js
resources/public/js/cards
resources/public/js/test
target
resources/public/getting-started.html
resources/public/.asciidoctor/
resources/public/*.png
docs/_site
docs/.sass-cache
docs/Gemfile.lock
docs/js/[a-fh-su-z]*
docs/js/goog
docs/js/garden
old-docs/.asciidoctor
old-docs/plumbing.png
DevelopersGuide.html
docs/.jekyll-metadata
.floo
.flooignore
.asciidoctor
/ReferenceGuide.html
.cpcache
.shadow-cljs
resources/public/workspaces
!.idea/runConfigurations
14 changes: 14 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
= Guardrails

image:https://img.shields.io/clojars/v/com.fulcrologic/guardrails.svg[link=https://clojars.org/com.fulcrologic/guardrails]

This library is a middle ground between the features of raw Clojure spec and George Lipov's Ghostwheel. I
personally wanted the following features, so I implemented them. Much of the source code in this
library is directly from https://github.com/gnl/ghostwheel[Ghostwheel].

== Copyright and License

The new code in this library is copyrighted by Fulcrologic, LLC. The code derived from Ghostwheel is
by George Lipov and follows the ownership/copyright of that library.

This library follows Ghostwheel's original license: Eclipse public license version 2.0.
19 changes: 19 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{:paths ["src/main"],

:deps {com.taoensso/timbre {:mvn/version "4.10.0"}
org.clojure/clojure {:mvn/version "1.10.1"}
expound {:mvn/version "0.7.2"}}

:aliases {:test {:extra-paths ["src/test"]
:extra-deps {fulcrologic/fulcro-spec {:mvn/version "3.1.4"}}}

:clj-tests {:extra-paths ["src/test"]
:main-opts ["-m" "kaocha.runner"]
:extra-deps {lambdaisland/kaocha {:mvn/version "0.0-529"}}}

:dev {:extra-paths ["src/dev"]
:extra-deps {thheller/shadow-cljs {:mvn/version "2.8.54"}
org.clojure/tools.nrepl {:mvn/version "0.2.13"}
binaryage/devtools {:mvn/version "0.9.10"}
org.clojure/tools.namespace {:mvn/version "0.3.1"}}}}}

5 changes: 5 additions & 0 deletions guardrails.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{:defn-macro nil
:throw? false
:emit-spec? true
:expound {:show-valid-values? true
:print-specs? true}}
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "guardrails",
"version": "1.0.0",
"description": "Tests",
"main": "index.js",
"directories": {},
"devDependencies": {
"karma": "3.1.4",
"karma-chrome-launcher": "2.2.0",
"karma-cljs-test": "0.1.0",
"karma-firefox-launcher": "1.1.0",
"shadow-cljs": "2.8.67"
},
"author": "Tony Kay",
"license": "MIT",
"dependencies": {
}
}
18 changes: 18 additions & 0 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{:deps {:aliases [:test :dev :workspaces]}
:nrepl {:port 9000}
:builds {:test {:target :browser-test
:test-dir "resources/public/js/test"
:ns-regexp "-spec$"
:compiler-options {:static-fns false
:external-config {:guardrails {:throw? false}}}
:devtools {:http-port 9002
:http-resource-root "public"
:http-root "resources/public/js/test"}}

:ci-tests {:target :karma
:compiler-options {:static-fns false
:external-config {:guardrails {:throw? true}}}
:js-options {:js-provider :shadow}
:output-to "target/ci.js"
:ns-regexp "-spec$"}}}

98 changes: 98 additions & 0 deletions src/main/com/fulcrologic/guardrails/config.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
;; Copyright (c) George Lipov. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 2.0 (https://choosealicense.com/licenses/epl-2.0/)
;; which can be found in the file LICENSE at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns ^:no-doc com.fulcrologic.guardrails.config
#?(:cljs (:require-macros com.fulcrologic.guardrails.config))
(:require [com.fulcrologic.guardrails.utils :as util]
#?@(:clj [[clojure.edn :as edn]]
:cljs [[cljs.env :as cljs-env]])))

;; This isn't particularly pretty, but it's how we avoid
;; having ClojureScript as a required dependency on Clojure
#?(:clj (try
(do
(ns-unalias (find-ns 'com.fulcrologic.guardrails.utils) 'cljs-env)
(require '[cljs.env :as cljs-env]))
(catch Exception _ (require '[com.fulcrologic.guardrails.stubs.cljs-env :as cljs-env]))))

(def default-config
{;; Generates standard `defn` function definitions
;; by default. If you require composability with other
;; `defn`-like macros, you can have Ghostwheel desugar to
;; them instead by setting the macro name as a string here.
:defn-macro nil

;; Nilable map of Expound configuration options.
;; If not nil, the spec printer will be set to
;; expound's with the given configuration options.
:expound {:show-valid-values? true
:print-specs? true}})

(let [*config-cache
(atom {::timestamp 0
::value nil})

read-config-file
(fn []
#?(:clj (try
(edn/read-string (slurp "guardrails.edn"))
(catch Exception _ nil))
:cljs nil))

reload-config
(fn []
;#?(:clj (.println System/err (get @cljs-env/*compiler* :options))) ; DEBUG
(let [config (let [cljs-compiler-config
(when cljs-env/*compiler*
(get-in @cljs-env/*compiler* [:options :external-config :guardrails]))]
(when (not #?(:clj (= (System/getProperty "guardrails.enabled") "false") :cljs false))
(merge {}
(read-config-file)
cljs-compiler-config)))]
;#?(:clj (.println System/err config)) ; DEBUG
config))]

(defn get-env-config
([]
(get-env-config true))
([cache?]
(if (or (not cache?)
#?(:clj (= (System/getProperty "guardrails.cache") "false")))
(reload-config)
(let [now (identity #?(:clj (System/currentTimeMillis) :cljs (js/Date.now)))]
(if (< (- now (::timestamp @*config-cache))
2000)
(::value @*config-cache)
(::value (reset! *config-cache {::timestamp now
::value (reload-config)}))))))))

(defn get-base-config-fn
"Base config is defaults + env config."
([]
(get-base-config-fn true))
([cache?]
(->> (get-env-config cache?)
(merge default-config))))

(defmacro get-base-config-macro
([]
(get-base-config-fn))
([cache?]
(get-base-config-fn cache?)))

(defn merge-config [env & meta-maps]
(let [config (->> (apply merge-with
(fn [a b]
(if (every? map? [a b])
(merge a b)
b))
(get-base-config-fn)
(util/get-ns-meta env)
meta-maps)
(into {}))]
config))

0 comments on commit cfc60d2

Please sign in to comment.