0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-19 03:01:27 -05:00

♻️ Migrate to common spec namespace.

This commit is contained in:
Andrey Antukh 2020-01-11 20:25:50 +01:00
parent 32131c5aa7
commit c88602c7ab
33 changed files with 111 additions and 260 deletions

View file

@ -6,15 +6,15 @@
(ns uxbox.config
"A configuration management."
(:require [clojure.java.io :as io]
[clojure.tools.logging :as log]
[clojure.edn :as edn]
[cuerdas.core :as str]
[buddy.core.hash :as hash]
[environ.core :refer [env]]
[mount.core :refer [defstate]]
[uxbox.util.exceptions :as ex]
[uxbox.util.data :refer [deep-merge]]))
(:require
[clojure.java.io :as io]
[clojure.tools.logging :as log]
[clojure.edn :as edn]
[cuerdas.core :as str]
[buddy.core.hash :as hash]
[environ.core :refer [env]]
[mount.core :refer [defstate]]
[uxbox.common.exceptions :as ex]))
;; --- Configuration Reading & Loading

View file

@ -10,10 +10,10 @@
[lambdaisland.uri :refer [uri]]
[mount.core :as mount :refer [defstate]]
[promesa.core :as p]
[uxbox.common.exceptions :as ex]
[uxbox.config :as cfg]
[uxbox.core :refer [system]]
[uxbox.util.data :as data]
[uxbox.util.exceptions :as ex]
[uxbox.util.pgsql :as pg]
[vertx.core :as vx])
(:import io.vertx.core.buffer.Buffer))

View file

@ -10,12 +10,12 @@
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.config :as cfg]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.media :as media]
[uxbox.util.exceptions :as ex]
[uxbox.util.emails :as emails]
[uxbox.util.blob :as blob]
[uxbox.util.spec :as us]))
[uxbox.util.blob :as blob]))
;; --- Defaults
@ -44,8 +44,8 @@
(defn send!
"Schedule the email for sending."
[email context]
(s/assert fn? email)
(s/assert map? context)
(us/assert fn? email)
(us/assert map? context)
(let [defaults {:from (:email-from cfg/config)
:reply-to (:email-reply-to cfg/config)}
data (->> (merge defaults context)

View file

@ -7,8 +7,8 @@
(ns uxbox.http.interceptors
(:require
[vertx.web :as vw]
[uxbox.util.transit :as t]
[uxbox.util.exceptions :as ex])
[uxbox.common.exceptions :as ex]
[uxbox.util.transit :as t])
(:import
io.vertx.ext.web.RoutingContext
io.vertx.ext.web.FileUpload

View file

@ -10,7 +10,6 @@
[sieppari.context :as spx]
[vertx.core :as vc]
[uxbox.db :as db]
[uxbox.util.exceptions :as ex]
[uxbox.util.uuid :as uuid]))
;; --- Main API

View file

@ -12,9 +12,9 @@
[datoteka.core :as fs]
[datoteka.proto :as pt]
[datoteka.storages :as st]
[uxbox.media :as media]
[uxbox.util.data :refer (dissoc-in)]
[uxbox.util.spec :as us])
[uxbox.common.data :as d]
[uxbox.common.spec :as us]
[uxbox.media :as media])
(:import
java.io.ByteArrayInputStream
java.io.InputStream
@ -43,8 +43,8 @@
width 200
height 200}
:as opts}]
(s/assert ::thumbnail-opts opts)
(s/assert fs/path? input)
(us/assert ::thumbnail-opts opts)
(us/assert fs/path? input)
(let [tmp (fs/create-tempfile :suffix (str "." format))
opr (doto (IMOperation.)
(.addImage)
@ -60,7 +60,7 @@
(defn make-thumbnail
[input {:keys [width height format quality] :as opts}]
(s/assert ::thumbnail-opts opts)
(us/assert ::thumbnail-opts opts)
(let [[filename ext] (fs/split-ext (fs/name input))
suffix (->> [width height quality format]
(interpose ".")
@ -102,6 +102,6 @@
entry
(let [url (str (st/public-url storage value))]
(-> entry
(dissoc-in src)
(d/dissoc-in src)
(assoc-in dst url))))))

View file

@ -12,11 +12,11 @@
[promesa.core :as p]
[uxbox.core :refer [system]]
[postal.core :as postal]
[uxbox.common.exceptions :as ex]
[uxbox.config :as cfg]
[uxbox.db :as db]
[uxbox.util.jobs :as uj]
[uxbox.util.blob :as blob]
[uxbox.util.exceptions :as ex]
[mount.core :as mount :refer [defstate]]))
;; TODO: implement low priority sending emails.

View file

@ -18,13 +18,13 @@
[datoteka.storages :as st]
[datoteka.core :as fs]
[uxbox.config]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.http]
[uxbox.migrations]
[uxbox.media :as media]
[uxbox.util.svg :as svg]
[uxbox.util.transit :as t]
[uxbox.util.spec :as us]
[uxbox.util.blob :as blob]
[uxbox.util.uuid :as uuid]
[uxbox.util.data :as data])

View file

@ -6,8 +6,7 @@
(ns uxbox.services.mutations
(:require
[uxbox.util.dispatcher :as uds]
[uxbox.util.exceptions :as ex]))
[uxbox.util.dispatcher :as uds]))
(uds/defservice handle
{:dispatch-by ::type

View file

@ -10,10 +10,10 @@
[buddy.hashers :as hashers]
[promesa.core :as p]
[uxbox.config :as cfg]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.services.mutations :as sm]
[uxbox.util.spec :as us]
[uxbox.util.exceptions :as ex]))
[uxbox.services.mutations :as sm]))
(def ^:private user-by-username-sql
"select id, password

View file

@ -9,11 +9,11 @@
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.common.spec :as us]
[uxbox.services.mutations :as sm]
[uxbox.services.util :as su]
[uxbox.services.queries.icons :refer [decode-icon-row]]
[uxbox.util.blob :as blob]
[uxbox.util.spec :as us]
[uxbox.util.uuid :as uuid]))
;; --- Helpers & Specs

View file

@ -11,6 +11,8 @@
[datoteka.storages :as ds]
[promesa.core :as p]
[promesa.exec :as px]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.media :as media]
[uxbox.images :as images]
@ -18,8 +20,6 @@
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.data :as data]
[uxbox.util.exceptions :as ex]
[uxbox.util.spec :as us]
[uxbox.util.uuid :as uuid]
[vertx.core :as vc]))
@ -117,7 +117,15 @@
(def valid-image-types?
#{"image/jpeg", "image/png", "image/webp"})
(s/def ::file ::us/upload)
(s/def :uxbox$upload/name ::us/string)
(s/def :uxbox$upload/size ::us/integer)
(s/def :uxbox$upload/mtype ::us/string)
(s/def ::upload
(s/keys :req-un [:uxbox$upload/name
:uxbox$upload/size
:uxbox$upload/mtype]))
(s/def ::file ::upload)
(s/def ::width ::us/integer)
(s/def ::height ::us/integer)
(s/def ::mimetype valid-image-types?)

View file

@ -9,11 +9,11 @@
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.util.spec :as us]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.services.mutations :as sm]
[uxbox.services.mutations.projects :as proj]
[uxbox.services.util :as su]
[uxbox.util.exceptions :as ex]
[uxbox.util.blob :as blob]
[uxbox.util.uuid :as uuid]))

View file

@ -9,14 +9,14 @@
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.common.pages :as cp]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.services.mutations :as sm]
[uxbox.services.mutations.project-files :as files]
[uxbox.services.queries.project-pages :refer [decode-row]]
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.exceptions :as ex]
[uxbox.util.spec :as us]
[uxbox.util.sql :as sql]
[uxbox.util.uuid :as uuid]
[vertx.eventbus :as ve]))

View file

@ -9,11 +9,11 @@
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.util.spec :as us]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.services.mutations :as sm]
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.exceptions :as ex]
[uxbox.util.uuid :as uuid]))
;; --- Helpers & Specs

View file

@ -9,11 +9,11 @@
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.db :as db]
[uxbox.common.spec :as us]
[uxbox.services.mutations :as sm]
[uxbox.services.util :as su]
[uxbox.services.queries.user-attrs :refer [decode-row]]
[uxbox.util.blob :as blob]
[uxbox.util.spec :as us]))
[uxbox.util.blob :as blob]))
;; --- Update

View file

@ -13,6 +13,8 @@
[promesa.core :as p]
[promesa.exec :as px]
[uxbox.config :as cfg]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.emails :as emails]
[uxbox.images :as images]
@ -24,8 +26,6 @@
strip-private-attrs
resolve-thumbnail]]
[uxbox.util.blob :as blob]
[uxbox.util.exceptions :as ex]
[uxbox.util.spec :as us]
[uxbox.util.token :as token]
[uxbox.util.uuid :as uuid]
[vertx.core :as vc]))
@ -125,7 +125,16 @@
;; --- Mutation: Update Photo
(s/def ::file ::us/upload)
(s/def :uxbox$upload/name ::us/string)
(s/def :uxbox$upload/size ::us/integer)
(s/def :uxbox$upload/mtype ::us/string)
(s/def ::upload
(s/keys :req-un [:uxbox$upload/name
:uxbox$upload/size
:uxbox$upload/mtype]))
(s/def ::file ::upload)
(s/def ::update-profile-photo
(s/keys :req-un [::user ::file]))
@ -286,13 +295,13 @@
;; (defmethod core/query :validate-profile-password-recovery-token
;; [{:keys [token]}]
;; (s/assert ::us/token token)
;; (us/assert ::us/token token)
;; (with-open [conn (db/connection)]
;; (recovery-token-exists? conn token)))
;; (defmethod core/novelty :request-profile-password-recovery
;; [{:keys [username]}]
;; (s/assert ::us/username username)
;; (us/assert ::us/username username)
;; (with-open [conn (db/connection)]
;; (db/atomic conn
;; (request-password-recovery conn username))))
@ -302,7 +311,7 @@
;; (defmethod core/novelty :recover-profile-password
;; [params]
;; (s/assert ::recover-password params)
;; (us/assert ::recover-password params)
;; (with-open [conn (db/connection)]
;; (db/apply-atomic conn recover-password params)))

View file

@ -8,11 +8,11 @@
(:require
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.services.queries :as sq]
[uxbox.util.blob :as blob]
[uxbox.util.exceptions :as ex]
[uxbox.util.spec :as us]))
[uxbox.util.blob :as blob]))
;; --- Helpers & Specs

View file

@ -9,6 +9,8 @@
[clojure.spec.alpha :as s]
[promesa.core :as p]
[promesa.exec :as px]
[uxbox.common.exceptions :as ex]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.media :as media]
[uxbox.images :as images]
@ -16,8 +18,6 @@
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.data :as data]
[uxbox.util.exceptions :as ex]
[uxbox.util.spec :as us]
[uxbox.util.uuid :as uuid]
[vertx.core :as vc]))
@ -75,8 +75,9 @@
and deleted_at is null;"]
(db/query-one conn [sql id])))
(s/def ::id ::us/uuid)
(s/def ::image-by-id
(s/keys :req-un [::user ::us/id]))
(s/keys :req-un [::user ::id]))
(sq/defquery ::image-by-id
[params]

View file

@ -9,11 +9,11 @@
[clojure.spec.alpha :as s]
[cuerdas.core :as str]
[promesa.core :as p]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.services.queries :as sq]
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.spec :as us]))
[uxbox.util.blob :as blob]))
(declare decode-row)

View file

@ -8,11 +8,11 @@
(:require
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.services.queries :as sq]
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.spec :as us]
[uxbox.util.sql :as sql]))
;; --- Helpers & Specs

View file

@ -8,11 +8,11 @@
(:require
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.services.queries :as sq]
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.spec :as us]))
[uxbox.util.blob :as blob]))
(declare decode-row)

View file

@ -8,11 +8,11 @@
(:require
[clojure.spec.alpha :as s]
[promesa.core :as p]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.services.queries :as sq]
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.spec :as us]))
[uxbox.util.blob :as blob]))
(defn decode-row
[{:keys [val] :as row}]
@ -20,6 +20,9 @@
(cond-> row
val (assoc :val (blob/decode val)))))
(s/def ::key ::us/string)
(s/def ::user ::us/uuid)
(s/def ::user-attr
(s/keys :req-un [::key ::user]))

View file

@ -9,12 +9,12 @@
[clojure.spec.alpha :as s]
[promesa.core :as p]
[promesa.exec :as px]
[uxbox.common.spec :as us]
[uxbox.db :as db]
[uxbox.images :as images]
[uxbox.services.queries :as sq]
[uxbox.services.util :as su]
[uxbox.util.blob :as blob]
[uxbox.util.spec :as us]))
[uxbox.util.blob :as blob]))
;; --- Helpers & Specs

View file

@ -10,9 +10,9 @@
[cuerdas.core :as str]
[vertx.core :as vc]
[uxbox.core :refer [system]]
[uxbox.common.exceptions :as ex]
[uxbox.util.uuid :as uuid]
[uxbox.util.dispatcher :as uds]
[uxbox.util.exceptions :as ex]))
[uxbox.util.dispatcher :as uds]))
;; (def logging-interceptor
;; {:enter (fn [data]

View file

@ -13,8 +13,7 @@
[expound.alpha :as expound]
[sieppari.core :as sp]
[sieppari.context :as spx]
[uxbox.util.spec :as us]
[uxbox.util.exceptions :as ex])
[uxbox.common.exceptions :as ex])
(:import
clojure.lang.IDeref
clojure.lang.MapEntry

View file

@ -10,8 +10,8 @@
[clojure.spec.alpha :as s]
[cuerdas.core :as str]
[instaparse.core :as insta]
[uxbox.util.exceptions :as ex]
[uxbox.util.spec :as us]
[uxbox.common.spec :as us]
[uxbox.common.exceptions :as ex]
[uxbox.util.template :as tmpl]))
;; --- Impl.
@ -44,9 +44,9 @@
(rest parts)))
state)))
(s/def ::subject ::us/string)
(s/def ::body-text ::us/string)
(s/def ::body-html ::us/string)
(s/def ::subject string?)
(s/def ::body-text string?)
(s/def ::body-html string?)
(s/def ::parsed-email
(s/keys :req-un [::subject ::body-html ::body-html]))
@ -79,7 +79,7 @@
(s/def ::to ::us/email)
(s/def ::from ::us/email)
(s/def ::reply-to ::us/email)
(s/def ::lang ::us/string)
(s/def ::lang string?)
(s/def ::context
(s/keys :req-un [::to]
@ -90,7 +90,7 @@
([id extra-context]
(s/assert keyword? id)
(fn [context]
(s/assert ::context context)
(us/assert ::context context)
(when-let [spec (s/get-spec id)]
(s/assert spec context))

View file

@ -1,34 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
;; WARNING: DEPRECATED: please use uxbox.common.exceptions
(ns uxbox.util.exceptions
"A helpers for work with exceptions."
(:require [clojure.spec.alpha :as s]))
(s/def ::type keyword?)
(s/def ::code keyword?)
(s/def ::mesage string?)
(s/def ::hint string?)
(s/def ::cause #(instance? Throwable %))
(s/def ::error-params
(s/keys :req-un [::type]
:opt-un [::code
::hint
::mesage
::cause]))
(defn error
[& {:keys [type code message hint cause] :as params}]
(s/assert ::error-params params)
(let [message (or message hint "")
payload (dissoc params :cause :message)]
(ex-info message payload cause)))
(defmacro raise
[& args]
`(throw (error ~@args)))

View file

@ -1,145 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016-2019 Andrey Antukh <niwi@niwi.nz>
;; WARNING: DEPRECATED: please use uxbox.common.spec
(ns uxbox.util.spec
(:refer-clojure :exclude [keyword uuid vector boolean map set])
(:require
[clojure.spec.alpha :as s]
[cuerdas.core :as str]
[datoteka.core :as fs]
[expound.alpha :as expound]
[uxbox.util.exceptions :as ex])
(:import java.time.Instant))
(s/check-asserts true)
;; --- Constants
(def email-rx
#"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
(def uuid-rx
#"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$")
;; --- Public Api
(defn conform
[spec data]
(let [result (s/conform spec data)]
(when (= result ::s/invalid)
(throw (ex/error :type :validation
:code :spec-validation
:explain (with-out-str
(expound/printer data))
:data (::s/problems data))))
result))
;; --- Predicates
(defn email?
[v]
(and (string? v)
(re-matches email-rx v)))
(defn instant?
[v]
(instance? Instant v))
(defn path?
[v]
(instance? java.nio.file.Path v))
(defn regex?
[v]
(instance? java.util.regex.Pattern v))
;; --- Conformers
(defn- uuid-conformer
[v]
(cond
(uuid? v) v
(string? v)
(cond
(re-matches uuid-rx v)
(java.util.UUID/fromString v)
(str/empty? v)
nil
:else
::s/invalid)
:else ::s/invalid))
(defn- integer-conformer
[v]
(cond
(integer? v) v
(string? v)
(if (re-matches #"^[-+]?\d+$" v)
(Long/parseLong v)
::s/invalid)
:else ::s/invalid))
(defn boolean-conformer
[v]
(cond
(boolean? v) v
(string? v)
(if (re-matches #"^(?:t|true|false|f|0|1)$" v)
(contains? #{"t" "true" "1"} v)
::s/invalid)
:else ::s/invalid))
(defn boolean-unformer
[v]
(if v "true" "false"))
(defn path-conformer
[v]
(cond
(string? v) (fs/path v)
(fs/path? v) v
:else ::s/invalid))
(defn- number-conformer
[v]
(cond
(number? v) v
(str/numeric? v) (Double/parseDouble v)
:else ::s/invalid))
;; --- Default Specs
(s/def ::string string?)
(s/def ::integer (s/conformer integer-conformer str))
(s/def ::uuid (s/conformer uuid-conformer str))
(s/def ::boolean (s/conformer boolean-conformer boolean-unformer))
(s/def ::number (s/conformer number-conformer str))
(s/def ::path (s/conformer path-conformer str))
(s/def ::positive pos?)
(s/def ::negative neg?)
(s/def ::uploaded-file any?)
(s/def ::bytes bytes?)
(s/def ::email email?)
(s/def ::file any?)
(s/def ::name ::string)
(s/def ::size ::integer)
(s/def ::mtype ::string)
(s/def ::upload
(s/keys :req-un [::name ::path ::size ::mtype]))
;; TODO: deprecated
(s/def ::id ::uuid)
(s/def ::username ::string)
(s/def ::password ::string)
(s/def ::token ::string)

View file

@ -9,8 +9,8 @@
(:require
[clojure.spec.alpha :as s]
[cuerdas.core :as str]
[uxbox.util.spec :as us]
[uxbox.util.exceptions :as ex])
[uxbox.common.spec :as us]
[uxbox.common.exceptions :as ex])
(:import
org.jsoup.Jsoup
org.jsoup.nodes.Attribute
@ -18,10 +18,10 @@
org.jsoup.nodes.Document
java.io.InputStream))
(s/def ::content ::us/string)
(s/def ::content string?)
(s/def ::width ::us/number)
(s/def ::height ::us/number)
(s/def ::name ::us/string)
(s/def ::name string?)
(s/def ::view-box (s/coll-of ::us/number :min-count 4 :max-count 4))
(s/def ::svg-entity

View file

@ -11,7 +11,7 @@
[clojure.tools.logging :as log]
[clojure.walk :as walk]
[clojure.java.io :as io]
[uxbox.util.exceptions :as ex])
[uxbox.common.exceptions :as ex])
(:import
java.io.StringReader
java.util.HashMap

View file

@ -15,6 +15,17 @@
;; Data Structures Manipulation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn dissoc-in
[m [k & ks :as keys]]
(if ks
(if-let [nextmap (get m k)]
(let [newmap (dissoc-in nextmap ks)]
(if (seq newmap)
(assoc m k newmap)
(dissoc m k)))
m)
(dissoc m k)))
(defn concat
[& colls]
(loop [result (first colls)

View file

@ -98,6 +98,7 @@
;; --- Default Specs
(s/def ::inst inst?)
(s/def ::string string?)
(s/def ::email (s/conformer email-conformer str))
(s/def ::color (s/conformer color-conformer str))
(s/def ::uuid (s/conformer uuid-conformer str))