(ns ^{:doc "In-repl user documentation"}
  midje.doc
  (:require [clojure.java.browse :as browse]
            [commons.clojure.core :refer :all :exclude [any?]]
            [midje.config :as config]
            [midje.emission.colorize :as color]
            [midje.util.ecosystem :as ecosystem]))

(def appropriate? config/running-in-repl?)

(defn repl-notice []
  (println (color/note "Run `(doc midje-repl)` for descriptions of Midje repl functions.")))

(defn midje-notice []
  (println (color/note "Run `(doc midje)` for Midje usage.")))

;;; KLUDGE: The list of values to be imported must be known to the importing namespaces.
;;; Don't forget to update them if adding new doc strings.

(def ^{:doc "
   Detailed help:
   (doc midje-facts)             -- The basic form.
   (doc midje-checkers)          -- Predefined predicates to use with arrows.
   (doc midje-arrows)            -- Alternate ways of describing the relationship
                                 -- between actual and expected values.

   (doc midje-prerequisites)     -- For top-down test-driven design.
   (doc midje-defining-checkers) -- Defining checkers
   (doc midje-setup)             -- Setup and teardown for facts and checkers.
   (doc midje-configuration)     -- Changing Midje's defaults.
   (doc midje-print-levels)      -- Changing Midje's verbosity.

   See also the `(guide)` macro, which takes you to pages in the
   user's guide.
   "} midje)

;; midje-repl
(def ^{:doc
  "
  Here are Midje repl functions. Use `doc` for more info on each one.
  To control verbosity of output, use the print levels described by
  `(doc midje-print-levels)`.
  Some functions take filter arguments that narrow down which facts
  within a namespace are acted upon. See the individual doc strings
  for details.

  ----- Autotest

  (autotest)       ; Reload changed files and all their dependents.

  ----- Loading facts
  You load facts by namespace.
  (load-facts <ns> <ns>...)
  (load-facts 'midje.util.*)      ; Load all namespaces below midje.util.
  (load-facts <ns> :integration)  ; Filter to just integration tests.
  (load-facts)                    ; Repeat most recent load-facts.

  ----- Checking facts, once loaded
  (check-facts <ns> <ns>...)        ; in given namespaces
  (check-facts :all)                ; all defined facts
  (check-facts :all :integration)   ; all integration tests

  Note: `check-facts` with no argument will check the same facts
  as the most recent `check-facts` or `load-facts`.

  ----- Rerunning facts
  (recheck-fact)                ; Check just-checked fact again.
  (recheck-fact :print-nothing) ; Check silently (but produce true/false).
  (rcf)                         ; Synonym for above.

  Note: facts with `:check-only-at-load-time`metadata do not get
  stored for rechecking.

  ----- Forgetting facts
  Same notation as the `check-facts` family, but with
  \"forget\" instead of \"check\".

  ----- Fetching facts
  Same notation as the `check-facts` family, but with
  \"fetch\" instead of \"check\".
  To check the returned facts, use `check-one-fact`:
     (map check-one-fact (fetch-facts :all))

  In addition, you can fetch the last fact checked with
  `(last-fact-checked)`. `(source-of-last-fact-checked)`
  gives you its source.

  To query fact metadata more easily, use these:
  -- (fact-name <ff>)         ; result might be nil
  -- (fact-source <ff>)
  -- (fact-file <ff>)
  -- (fact-line <ff>)
  -- (fact-namespace <ff>)    ; a symbol
  -- (fact-description <ff>)  ; the doc string; might be nil
  "} midje-repl)

;; print-levels
(def ^{:doc "
  The `load-facts`, `check-facts`, and `recheck-fact`
  functions normally print any fact failures and a final
  summary. The detail printed can be adjusted by passing
  either certain keywords or corresponding numbers to the
  functions. (The numbers are easier to remember.)
  For example, here's how you check all facts in the most
  verbose way:
    (check-facts :all 2)
    (check-facts :all :print-facts)

  Here are all the variants:

  :print-normally     (0)  -- failures and a summary.
  :print-no-summary  (-1)  -- failures only.
  :print-nothing     (-2)  -- nothing is printed.
                           -- (but return value can be checked)
  :print-namespaces   (1)  -- print the namespace for each group of facts.
  :print-facts        (2)  -- print fact descriptions in addition to namespaces.
"} midje-print-levels)

(defn alternate-doc-source [copy original]
  (intern *ns* (vary-meta copy assoc :doc (:doc (meta (resolve original))))))

(alternate-doc-source 'midje-print-level 'midje-print-levels)

(def ^{:doc "
  * A common form:
    (fact \"fact name / doc string\"
      (let [result (prime-ish 5)]
        result => odd?
        result => (roughly 13)))

  * Nested facts
    (facts \"about life\"
      (facts \"about birth\"...)
      (facts \"about childhood\"...)
      ...)

  * Tabular facts
    (tabular
      (fact (+ ?x ?y) => 3)
      ?x   ?y
       1    2
       0    3)

  * Metadata
    (fact :integration ...)
    (fact {:priority 5} ...)
"} midje-facts)

(alternate-doc-source 'midje-fact 'midje-facts)

(def ^{:doc "
  (facts \"about checkers\"
    (f) => truthy
    (f) => falsey
    (f) => irrelevant ; or `anything`
    (f) => (exactly odd?) ; when you expect a particular function
    (f) => (roughly 10 0.1)
    (f) => (throws SomeException #\"with message\")
    (f) => (contains [1 2 3]) ; works with strings, maps, etc.
    (f) => (contains [1 2 3] :in-any-order :gaps-ok)
    (f) => (just [1 2 3])
    (f) => (has every? odd?)
    (f) => (nine-of odd?) ; must be exactly 9 odd values.
    (f) => (every-checker odd? (roughly 9)) ; both must be true
    (f) => (some-checker odd? (roughly 9))) ; one must be true
  "} midje-checkers)

(def ^{:doc "
  Any function can be used on the right-hand side of a check:
      (fact 500 => (fn [actual] (> actual 100)))

  In the left-hand side of a prerequisite, use `checker` or
  `as-checker` to define the checker:
      (provided
         (f (checker [arg] (> arg 100))) => 3
         (h (as-checker even?)) => 4)

  Checkers can be named with `defchecker`:
        (defchecker twosie [actual]
           (and (pos? actual) (even? actual)))
        (fact 2 => twosie)

        (defchecker roughly [expected delta]
          (checker [actual]
             (and (number? actual)
                  ...)))
        (fact 1.1 => (roughly 1 0.2))

  Chatty checkers print the values of subexpressions in the
  case of a failure:

       (fact 4 => (chatty-checker [actual] (< (h actual) (g actual))))

  Use `every-checker` or `some-checker` to define checkers for
  boolean expressions:

       (def pleasingly-positive (every-checker number? even? pos?))
       (fact 4 => pleasingly-positive)

       (def unnatural-number (some-checker (complement number?) odd? neg?))
       (fact 'fred => unnatural-number)
  "} midje-defining-checkers)

(def ^{:doc "
  * Prerequisites and top-down TDD:
    (unfinished char-value chars)
    (fact \"a row value is composed of character values\"
       (row-value ..row..) => \"53\"
       (provided
         (chars ..row..) => [..five.. ..three..]
         (char-value ..five..) => \"5\"
         (char-value ..three..) => \"3\"))

  * Prerequisites can be defaulted for claims within a fact:
    (fact \"No one is ready until everyone is ready.\"
      (prerequisites (pilot-ready?) => true,
                     (copilot-ready?) => true,
                     (flight-engineer-ready?) => true)
      (ready?) => truthy
      (ready?) => falsey (provided (pilot-ready?) => false)
      (ready?) => falsey (provided (copilot-ready?) => false)
      (ready?) => falsey (provided (flight-engineer-ready?) => false))

  * Prerequisites apply to nested facts
    (facts \"about airplanes\"
       (prerequisites (wings) => 2
                      (engines => 2)
          (fact
             (prerequisite (crew) => 8)
             ...)))
"} midje-prerequisites)

(def ^{:doc
       "
  Setup and Teardown
  * Applies to all enclosed facts
    (with-state-changes [(before :facts (do-this))
                         (after :facts (do-that))]
       (fact ...)
       (fact ...))

  * For the rest of this namespace:

       (namespace-state-changes (before :facts (do-this))
                                (after :facts (do-that)))
"} midje-setup)

(alternate-doc-source 'midje-teardown 'midje-setup)

(def ^{:doc "
  * For checks
    5     =not=>    even?      ; Invert the check. Synonym: =deny=>
    (f)   =future=> halts      ; don't check, but issue reminder.
    (m x) =expands-to=> form   ; expand macro and check result

  * In prerequisites
    ..meta.. =contains=> {:a 1} ; partial specification of map-like object
    (f)      =streams=>  (range 5 1000)
    (f)      =throws=>   (IllegalArgumentException. \"boom\")
"} midje-arrows)

(def ^{:doc "
  On startup, Midje loads ${HOME}/.midje.clj and ./.midje.clj
  (in that order). To affect the default configuration, use
  code like this in those files:
      (change-defaults :visible-deprecation false
                       :visible-future false
                       :print-level :print-namespaces)

  If you want different configurations for repl and command-line
  Midje, use the `running-in-repl?` predicate.

  You can temporarily override the configuration like this:
      (midje.config/with-augmented-config {:print-level :print-no-summary}
         <forms>...)

  ------ Configuration keywords
  :print-level                  ; Verbosity of printing.
                                ; See `(doc midje-print-levels)`.

  :visible-deprecation          ; Whether information about deprecated
                                ; features or functions is printed.
                                ; Default: true.

  :visible-future               ; Whether future facts produce output.
                                ; Default: true.
                                ; More: `(guide future-facts)`

  :visible-failure-namespace    ; Should failure messages include the
                                ; namespace name as well as the file name?
                                ; Default: false.

  :fact-filter                  ; A function applied to the metadata of
                                ; a fact to see if it should be checked.
                                ; Default: (constantly true)

  :check-after-creation         ; Should facts be checked as they're loaded?
                                ; Default: true.

  :emitter                      ; Namespace or pathname that contains
                                  an \"emitter\" of custom output.

  :partial-prerequisites        ; Whether the real function can be used.
                                ; Default false.
                                ; More: `(guide partial-prerequisites)`
"} midje-configuration)

(def ^{:doc "
   Background-changers are discouraged in favor of `with-state-changes`
   and `prerequisites`, but they still may sometimes be needed.

   There are two types of background-changers. The first provides a default
   prerequisite and has the same format as `provided`. Here's an example of
   two inside in a `background` form:

      (background (f 33) => 12, (f 34) => 21)

   The other form runs arbitrary code before, after, or around a fact or
   individual check. Here's how you execute some setup code before each of a
   series of facts:

      (against-background [(before :facts (reset! some-atom 0))]
         (fact ...)
         (fact ...))

   Here's how you execute some code after each check in a fact:

      (against-background [(after :checks (reset! some-atom 0))]
        (fact
          (f 1) => 2   ; Reset happens after.
          (f 2) => 3)) ; Reset happens after.

   You can execute code both before and after a fact like this:

      (against-background [(before :facts (reset! in-atom 0)
                                   :after (reset! out-atom 0))]
         (fact ...)
         (fact ...))

   You can also wrap code around a fact or check:

      (against-background [(around :facts (sql/with-connection db ?form))]
         ...)

   The two types of background-changers can be intermingled.
"} midje-background-changers)

(def guide-raw
 ['user-guide "https://github.com/marick/Midje/wiki"
  'concept-index "https://github.com/marick/Midje/wiki/Concept-index"
  'syntax-reference "https://github.com/marick/Midje/wiki/Syntax-and-a-little-semantics"
  '--- ""
  'file-an-issue ecosystem/issues-url
  'unfixed-syntax-errors ecosystem/syntax-errors-that-will-not-be-fixed
  '--- ""
  'future-facts  "https://github.com/marick/Midje/wiki/Future-facts"
  'tabular-facts "https://github.com/marick/Midje/wiki/Tabular-facts"
  'fact-metadata "https://github.com/marick/Midje/wiki/Using-metadata-to-filter-facts"
  "" ""
  'setup-and-teardown "https://github.com/marick/Midje/wiki/Setup-and-teardown"
  'prerequisites "https://github.com/marick/Midje/wiki/Describing-one-checkable%27s-prerequisites"
  'fact-wide-prerequisites "https://github.com/marick/Midje/wiki/Establishing-fact-wide-prerequisites"
  'partial-prerequisites "https://github.com/marick/Midje/wiki/Partial-prerequisites"
  "" ""
  'checking-sequences "https://github.com/marick/Midje/wiki/Checking-sequential-collections"
  'checking-maps-and-records "https://github.com/marick/Midje/wiki/Checking-maps-and-records"
  'checking-sets "https://github.com/marick/Midje/wiki/Checking-sets"
  'combining-checkers "https://github.com/marick/Midje/wiki/Combining-checkers"
  'chatty-checkers "https://github.com/marick/Midje/wiki/Chatty-checkers"

  ])

(def guide-topics (map first (partition 2 guide-raw)))
(def guide-urls (map second (partition 2 guide-raw)))

(def guide-map (zipmap guide-topics guide-urls))

(defmacro guide
  "Open a web page that addresses a particular topic"
  ([topic]
     `(if-let [url# (guide-map '~topic)]
        (browse/browse-url url#)
        (do
          (println "There is no such topic. Did you mean one of these?")
          (doseq [topic# guide-topics]
            (println "   " topic#)))))
  ([]
     `(guide :no-such-topic)))

(ns ^{:doc "In-repl user documentation"}
  midje.doc
  (:require [clojure.java.browse :as browse]
            [commons.clojure.core :refer :all :exclude [any?]]
            [midje.config :as config]
            [midje.emission.colorize :as color]
            [midje.util.ecosystem :as ecosystem]))

(def appropriate? config/running-in-repl?)

(defn repl-notice []
  (println (color/note "Run `(doc midje-repl)` for descriptions of Midje repl functions.")))

(defn midje-notice []
  (println (color/note "Run `(doc midje)` for Midje usage.")))

;;; KLUDGE: The list of values to be imported must be known to the importing namespaces.
;;; Don't forget to update them if adding new doc strings.

(def ^{:doc "
   Detailed help:
   (doc midje-facts)             -- The basic form.
   (doc midje-checkers)          -- Predefined predicates to use with arrows.
   (doc midje-arrows)            -- Alternate ways of describing the relationship
                                 -- between actual and expected values.

   (doc midje-prerequisites)     -- For top-down test-driven design.
   (doc midje-defining-checkers) -- Defining checkers
   (doc midje-setup)             -- Setup and teardown for facts and checkers.
   (doc midje-configuration)     -- Changing Midje's defaults.
   (doc midje-print-levels)      -- Changing Midje's verbosity.

   See also the `(guide)` macro, which takes you to pages in the
   user's guide.
   "} midje)

;; midje-repl
(def ^{:doc
  "
  Here are Midje repl functions. Use `doc` for more info on each one.
  To control verbosity of output, use the print levels described by
  `(doc midje-print-levels)`.
  Some functions take filter arguments that narrow down which facts
  within a namespace are acted upon. See the individual doc strings
  for details.

  ----- Autotest

  (autotest)       ; Reload changed files and all their dependents.

  ----- Loading facts
  You load facts by namespace.
  (load-facts <ns> <ns>...)
  (load-facts 'midje.util.*)      ; Load all namespaces below midje.util.
  (load-facts <ns> :integration)  ; Filter to just integration tests.
  (load-facts)                    ; Repeat most recent load-facts.

  ----- Checking facts, once loaded
  (check-facts <ns> <ns>...)        ; in given namespaces
  (check-facts :all)                ; all defined facts
  (check-facts :all :integration)   ; all integration tests

  Note: `check-facts` with no argument will check the same facts
  as the most recent `check-facts` or `load-facts`.

  ----- Rerunning facts
  (recheck-fact)                ; Check just-checked fact again.
  (recheck-fact :print-nothing) ; Check silently (but produce true/false).
  (rcf)                         ; Synonym for above.

  Note: facts with `:check-only-at-load-time` metadata do not get
  stored for rechecking.

  ----- Forgetting facts
  Same notation as the `check-facts` family, but with
  \"forget\" instead of \"check\".

  ----- Fetching facts
  Same notation as the `check-facts` family, but with
  \"fetch\" instead of \"check\".
  To check the returned facts, use `check-one-fact`:
     (map check-one-fact (fetch-facts :all))

  In addition, you can fetch the last fact checked with
  `(last-fact-checked)`. `(source-of-last-fact-checked)`
  gives you its source.

  To query fact metadata more easily, use these:
  -- (fact-name <ff>)         ; result might be nil
  -- (fact-source <ff>)
  -- (fact-file <ff>)
  -- (fact-line <ff>)
  -- (fact-namespace <ff>)    ; a symbol
  -- (fact-description <ff>)  ; the doc string; might be nil
  "} midje-repl)

;; print-levels
(def ^{:doc "
  The `load-facts`, `check-facts`, and `recheck-fact`
  functions normally print any fact failures and a final
  summary. The detail printed can be adjusted by passing
  either certain keywords or corresponding numbers to the
  functions. (The numbers are easier to remember.)
  For example, here's how you check all facts in the most
  verbose way:
    (check-facts :all 2)
    (check-facts :all :print-facts)

  Here are all the variants:

  :print-normally     (0)  -- failures and a summary.
  :print-no-summary  (-1)  -- failures only.
  :print-nothing     (-2)  -- nothing is printed.
                           -- (but return value can be checked)
  :print-namespaces   (1)  -- print the namespace for each group of facts.
  :print-facts        (2)  -- print fact descriptions in addition to namespaces.
"} midje-print-levels)

(defn alternate-doc-source [copy original]
  (intern *ns* (vary-meta copy assoc :doc (:doc (meta (resolve original))))))

(alternate-doc-source 'midje-print-level 'midje-print-levels)

(def ^{:doc "
  * A common form:
    (fact \"fact name / doc string\"
      (let [result (prime-ish 5)]
        result => odd?
        result => (roughly 13)))

  * Nested facts
    (facts \"about life\"
      (facts \"about birth\"...)
      (facts \"about childhood\"...)
      ...)

  * Tabular facts
    (tabular
      (fact (+ ?x ?y) => 3)
      ?x   ?y
       1    2
       0    3)

  * Metadata
    (fact :integration ...)
    (fact {:priority 5} ...)
"} midje-facts)

(alternate-doc-source 'midje-fact 'midje-facts)

(def ^{:doc "
  (facts \"about checkers\"
    (f) => truthy
    (f) => falsey
    (f) => irrelevant ; or `anything`
    (f) => (exactly odd?) ; when you expect a particular function
    (f) => (roughly 10 0.1)
    (f) => (throws SomeException #\"with message\")
    (f) => (contains [1 2 3]) ; works with strings, maps, etc.
    (f) => (contains [1 2 3] :in-any-order :gaps-ok)
    (f) => (just [1 2 3])
    (f) => (has every? odd?)
    (f) => (nine-of odd?) ; must be exactly 9 odd values.
    (f) => (every-checker odd? (roughly 9)) ; both must be true
    (f) => (some-checker odd? (roughly 9))) ; one must be true
  "} midje-checkers)

(def ^{:doc "
  Any function can be used on the right-hand side of a check:
      (fact 500 => (fn [actual] (> actual 100)))

  In the left-hand side of a prerequisite, use `checker` or
  `as-checker` to define the checker:
      (provided
         (f (checker [arg] (> arg 100))) => 3
         (h (as-checker even?)) => 4)

  Checkers can be named with `defchecker`:
        (defchecker twosie [actual]
           (and (pos? actual) (even? actual)))
        (fact 2 => twosie)

        (defchecker roughly [expected delta]
          (checker [actual]
             (and (number? actual)
                  ...)))
        (fact 1.1 => (roughly 1 0.2))

  Chatty checkers print the values of subexpressions in the
  case of a failure:

       (fact 4 => (chatty-checker [actual] (< (h actual) (g actual))))

  Use `every-checker` or `some-checker` to define checkers for
  boolean expressions:

       (def pleasingly-positive (every-checker number? even? pos?))
       (fact 4 => pleasingly-positive)

       (def unnatural-number (some-checker (complement number?) odd? neg?))
       (fact 'fred => unnatural-number)
  "} midje-defining-checkers)

(def ^{:doc "
  * Prerequisites and top-down TDD:
    (unfinished char-value chars)
    (fact \"a row value is composed of character values\"
       (row-value ..row..) => \"53\"
       (provided
         (chars ..row..) => [..five.. ..three..]
         (char-value ..five..) => \"5\"
         (char-value ..three..) => \"3\"))

  * Prerequisites can be defaulted for claims within a fact:
    (fact \"No one is ready until everyone is ready.\"
      (prerequisites (pilot-ready?) => true,
                     (copilot-ready?) => true,
                     (flight-engineer-ready?) => true)
      (ready?) => truthy
      (ready?) => falsey (provided (pilot-ready?) => false)
      (ready?) => falsey (provided (copilot-ready?) => false)
      (ready?) => falsey (provided (flight-engineer-ready?) => false))

  * Prerequisites apply to nested facts
    (facts \"about airplanes\"
       (prerequisites (wings) => 2
                      (engines => 2)
          (fact
             (prerequisite (crew) => 8)
             ...)))
"} midje-prerequisites)

(def ^{:doc
       "
  Setup and Teardown
  * Applies to all enclosed facts
    (with-state-changes [(before :facts (do-this))
                         (after :facts (do-that))]
       (fact ...)
       (fact ...))

  * For the rest of this namespace:

       (namespace-state-changes (before :facts (do-this))
                                (after :facts (do-that)))
"} midje-setup)

(alternate-doc-source 'midje-teardown 'midje-setup)

(def ^{:doc "
  * For checks
    5     =not=>    even?      ; Invert the check. Synonym: =deny=>
    (f)   =future=> halts      ; don't check, but issue reminder.
    (m x) =expands-to=> form   ; expand macro and check result

  * In prerequisites
    ..meta.. =contains=> {:a 1} ; partial specification of map-like object
    (f)      =streams=>  (range 5 1000)
    (f)      =throws=>   (IllegalArgumentException. \"boom\")
"} midje-arrows)

(def ^{:doc "
  On startup, Midje loads ${HOME}/.midje.clj and ./.midje.clj
  (in that order). To affect the default configuration, use
  code like this in those files:
      (change-defaults :visible-deprecation false
                       :visible-future false
                       :print-level :print-namespaces)

  If you want different configurations for repl and command-line
  Midje, use the `running-in-repl?` predicate.

  You can temporarily override the configuration like this:
      (midje.config/with-augmented-config {:print-level :print-no-summary}
         <forms>...)

  ------ Configuration keywords
  :print-level                  ; Verbosity of printing.
                                ; See `(doc midje-print-levels)`.

  :visible-deprecation          ; Whether information about deprecated
                                ; features or functions is printed.
                                ; Default: true.

  :visible-future               ; Whether future facts produce output.
                                ; Default: true.
                                ; More: `(guide future-facts)`

  :visible-failure-namespace    ; Should failure messages include the
                                ; namespace name as well as the file name?
                                ; Default: false.

  :fact-filter                  ; A function applied to the metadata of
                                ; a fact to see if it should be checked.
                                ; Default: (constantly true)

  :check-after-creation         ; Should facts be checked as they're loaded?
                                ; Default: true.

  :emitter                      ; Namespace or pathname that contains
                                  an \"emitter\" of custom output.

  :partial-prerequisites        ; Whether the real function can be used.
                                ; Default false.
                                ; More: `(guide partial-prerequisites)`
"} midje-configuration)

(def ^{:doc "
   Background-changers are discouraged in favor of `with-state-changes`
   and `prerequisites`, but they still may sometimes be needed.

   There are two types of background-changers. The first provides a default
   prerequisite and has the same format as `provided`. Here's an example of
   two inside in a `background` form:

      (background (f 33) => 12, (f 34) => 21)

   The other form runs arbitrary code before, after, or around a fact or
   individual check. Here's how you execute some setup code before each of a
   series of facts:

      (against-background [(before :facts (reset! some-atom 0))]
         (fact ...)
         (fact ...))

   Here's how you execute some code after each check in a fact:

      (against-background [(after :checks (reset! some-atom 0))]
        (fact
          (f 1) => 2   ; Reset happens after.
          (f 2) => 3)) ; Reset happens after.

   You can execute code both before and after a fact like this:

      (against-background [(before :facts (reset! in-atom 0)
                                   :after (reset! out-atom 0))]
         (fact ...)
         (fact ...))

   You can also wrap code around a fact or check:

      (against-background [(around :facts (sql/with-connection db ?form))]
         ...)

   The two types of background-changers can be intermingled.
"} midje-background-changers)

(def guide-raw
 ['user-guide "https://github.com/marick/Midje/wiki"
  'concept-index "https://github.com/marick/Midje/wiki/Concept-index"
  'syntax-reference "https://github.com/marick/Midje/wiki/Syntax-and-a-little-semantics"
  '--- ""
  'file-an-issue ecosystem/issues-url
  'unfixed-syntax-errors ecosystem/syntax-errors-that-will-not-be-fixed
  '--- ""
  'future-facts  "https://github.com/marick/Midje/wiki/Future-facts"
  'tabular-facts "https://github.com/marick/Midje/wiki/Tabular-facts"
  'fact-metadata "https://github.com/marick/Midje/wiki/Using-metadata-to-filter-facts"
  "" ""
  'setup-and-teardown "https://github.com/marick/Midje/wiki/Setup-and-teardown"
  'prerequisites "https://github.com/marick/Midje/wiki/Describing-one-checkable%27s-prerequisites"
  'fact-wide-prerequisites "https://github.com/marick/Midje/wiki/Establishing-fact-wide-prerequisites"
  'partial-prerequisites "https://github.com/marick/Midje/wiki/Partial-prerequisites"
  "" ""
  'checking-sequences "https://github.com/marick/Midje/wiki/Checking-sequential-collections"
  'checking-maps-and-records "https://github.com/marick/Midje/wiki/Checking-maps-and-records"
  'checking-sets "https://github.com/marick/Midje/wiki/Checking-sets"
  'combining-checkers "https://github.com/marick/Midje/wiki/Combining-checkers"
  'chatty-checkers "https://github.com/marick/Midje/wiki/Chatty-checkers"

  ])

(def guide-topics (map first (partition 2 guide-raw)))
(def guide-urls (map second (partition 2 guide-raw)))

(def guide-map (zipmap guide-topics guide-urls))

(defmacro guide
  "Open a web page that addresses a particular topic"
  ([topic]
     `(if-let [url# (guide-map '~topic)]
        (browse/browse-url url#)
        (do
          (println "There is no such topic. Did you mean one of these?")
          (doseq [topic# guide-topics]
            (println "   " topic#)))))
  ([]
     `(guide :no-such-topic)))

add space

add space


(ns ^{:no-doc true} onyx.state.serializers.windowing-key-encoder
  (:import [org.agrona.concurrent UnsafeBuffer]
           [java.nio.ByteOrder]))

;; TODO: try to remove the extra allocation in get-bytes
;; It would be preferable to directly write from the buffer into LMDB
(defprotocol PEncoder
  (set-state-idx [this idx])
  (set-group-id [this group-id])
  (set-min-extent [this] "Set the minimum extent representable in lexicographic order to allow for iteration.")
  (set-extent [this extent])
  (get-bytes [this])
  (wrap-impl [this bs])
  (length [this]))

(defn ^bytes encode-key [enc idx ^bytes group extent]
  (set-state-idx enc idx)
  (set-group-id enc group)
  (set-extent enc extent)
  (get-bytes enc))

(deftype GroupedTriggerEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this group-id]
    (.putBytes buffer (unchecked-add-int offset 2) group-id))
  (length [this] 10)
  (set-extent [this _])
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype GroupedNoExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this group-id]
    (.putBytes buffer (unchecked-add-int offset 2) group-id))
  (set-min-extent [this])
  (set-extent [this _])
  (length [this] 10)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype GroupedLongExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this group-id]
    (.putBytes buffer (unchecked-add-int offset 2) group-id))
  (set-min-extent [this]
    (set-extent this 0))
  (set-extent [this extent]
    (.putLong buffer (unchecked-add-int offset 10) extent java.nio.ByteOrder/BIG_ENDIAN))
  (length [this] 18)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype GroupedLongLongExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this group-id]
    (.putBytes buffer (unchecked-add-int offset 2) group-id))
  (set-min-extent [this]
    (set-extent this [0 0]))
  (set-extent [this ee]
    (let [[extent1 extent2] ee
          extent1-offset (unchecked-add-int offset 10)
          extent2-offset (unchecked-add-int offset 18)]
      (.putLong buffer extent1-offset extent1 java.nio.ByteOrder/BIG_ENDIAN)
      (.putLong buffer extent2-offset extent2 java.nio.ByteOrder/BIG_ENDIAN)))
  (length [this] 26)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype UngroupedTriggerEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (length [this]
    2)
  (set-extent [this _])
  (set-group-id [this _])
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype UngroupedNoExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this _])
  (set-extent [this _])
  (set-min-extent [this])
  (length [this] 2)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype UngroupedLongExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this _])
  (set-min-extent [this]
    (set-extent this 0))
  (set-extent [this extent]
    (.putLong buffer 2 extent java.nio.ByteOrder/BIG_ENDIAN))
  (length [this] 10)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array 10)]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype UngroupedLongLongExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this _] nil)
  (set-min-extent [this]
    (set-extent this [0 0]))
  (set-extent [this [extent1 extent2]]
    (.putLong buffer 2 extent1 java.nio.ByteOrder/BIG_ENDIAN)
    (.putLong buffer 10 extent2 java.nio.ByteOrder/BIG_ENDIAN))
  (length [this] 18)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(defn grouped-trigger [buffer offset]
  (->GroupedTriggerEncoder buffer offset))

(defn grouped-long-extent [buffer offset]
  (->GroupedLongExtentEncoder buffer offset))

(defn grouped-long-long-extent [buffer offset]
  (->GroupedLongLongExtentEncoder buffer offset))

(defn grouped-no-extent [buffer offset]
  (->GroupedNoExtentEncoder buffer offset))

(defn ungrouped-trigger [buffer offset]
  (->UngroupedTriggerEncoder buffer offset))

(defn ungrouped-no-extent [buffer offset]
  (->UngroupedNoExtentEncoder buffer offset))

(defn ungrouped-long-extent [buffer offset]
  (->UngroupedLongExtentEncoder buffer offset))

(defn ungrouped-long-long-extent [buffer offset]
  (->UngroupedLongLongExtentEncoder buffer offset))

(ns ^{:no-doc true} onyx.state.serializers.windowing-key-encoder
  (:import [org.agrona.concurrent UnsafeBuffer]
           [java.nio ByteOrder]))

;; TODO: try to remove the extra allocation in get-bytes
;; It would be preferable to directly write from the buffer into LMDB
(defprotocol PEncoder
  (set-state-idx [this idx])
  (set-group-id [this group-id])
  (set-min-extent [this] "Set the minimum extent representable in lexicographic order to allow for iteration.")
  (set-extent [this extent])
  (get-bytes [this])
  (wrap-impl [this bs])
  (length [this]))

(defn ^bytes encode-key [enc idx ^bytes group extent]
  (set-state-idx enc idx)
  (set-group-id enc group)
  (set-extent enc extent)
  (get-bytes enc))

(deftype GroupedTriggerEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this group-id]
    (.putBytes buffer (unchecked-add-int offset 2) group-id))
  (length [this] 10)
  (set-extent [this _])
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype GroupedNoExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this group-id]
    (.putBytes buffer (unchecked-add-int offset 2) group-id))
  (set-min-extent [this])
  (set-extent [this _])
  (length [this] 10)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype GroupedLongExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this group-id]
    (.putBytes buffer (unchecked-add-int offset 2) group-id))
  (set-min-extent [this]
    (set-extent this 0))
  (set-extent [this extent]
    (.putLong buffer (unchecked-add-int offset 10) extent java.nio.ByteOrder/BIG_ENDIAN))
  (length [this] 18)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype GroupedLongLongExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this group-id]
    (.putBytes buffer (unchecked-add-int offset 2) group-id))
  (set-min-extent [this]
    (set-extent this [0 0]))
  (set-extent [this ee]
    (let [[extent1 extent2] ee
          extent1-offset (unchecked-add-int offset 10)
          extent2-offset (unchecked-add-int offset 18)]
      (.putLong buffer extent1-offset extent1 java.nio.ByteOrder/BIG_ENDIAN)
      (.putLong buffer extent2-offset extent2 java.nio.ByteOrder/BIG_ENDIAN)))
  (length [this] 26)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype UngroupedTriggerEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (length [this]
    2)
  (set-extent [this _])
  (set-group-id [this _])
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype UngroupedNoExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this _])
  (set-extent [this _])
  (set-min-extent [this])
  (length [this] 2)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype UngroupedLongExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this _])
  (set-min-extent [this]
    (set-extent this 0))
  (set-extent [this extent]
    (.putLong buffer 2 extent java.nio.ByteOrder/BIG_ENDIAN))
  (length [this] 10)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array 10)]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(deftype UngroupedLongLongExtentEncoder [^UnsafeBuffer buffer offset]
  PEncoder
  (set-state-idx [this idx]
    (.putShort buffer offset idx))
  (set-group-id [this _] nil)
  (set-min-extent [this]
    (set-extent this [0 0]))
  (set-extent [this [extent1 extent2]]
    (.putLong buffer 2 extent1 java.nio.ByteOrder/BIG_ENDIAN)
    (.putLong buffer 10 extent2 java.nio.ByteOrder/BIG_ENDIAN))
  (length [this] 18)
  (wrap-impl [this bs]
    (.wrap buffer ^bytes bs))
  (get-bytes [this]
    (let [ret-bs (byte-array (length this))]
      (.getBytes buffer 0 ^bytes ret-bs)
      ret-bs)))

(defn grouped-trigger [buffer offset]
  (->GroupedTriggerEncoder buffer offset))

(defn grouped-long-extent [buffer offset]
  (->GroupedLongExtentEncoder buffer offset))

(defn grouped-long-long-extent [buffer offset]
  (->GroupedLongLongExtentEncoder buffer offset))

(defn grouped-no-extent [buffer offset]
  (->GroupedNoExtentEncoder buffer offset))

(defn ungrouped-trigger [buffer offset]
  (->UngroupedTriggerEncoder buffer offset))

(defn ungrouped-no-extent [buffer offset]
  (->UngroupedNoExtentEncoder buffer offset))

(defn ungrouped-long-extent [buffer offset]
  (->UngroupedLongExtentEncoder buffer offset))

(defn ungrouped-long-long-extent [buffer offset]
  (->UngroupedLongLongExtentEncoder buffer offset))

Fix invalid :import caught with Clojure 1.10 specs (#881)

But note that this invalid statement previously had no effect, so you can see the fully-qualified name being used in the source. It might be better to either remove the import or remove the qualification in the source.


(ns planck.core
  (:require-macros [cljs.env.macros :refer [with-compiler-env]])
  (:require [cljs.js :as cljs]
            [cljs.tagged-literals :as tags]
            [cljs.tools.reader :as r]
            [cljs.analyzer :as ana]
            [cljs.repl :as repl]
            [cljs.stacktrace :as st]
            [cljs.source-map :as sm]
            [tailrecursion.cljson :refer [cljson->clj]]
            [planck.io]))

(defonce st (cljs/empty-state))

(defonce current-ns (atom 'cljs.user))

(defonce app-env (atom nil))

(defn map-keys [f m]
  (reduce-kv (fn [r k v] (assoc r (f k) v)) {} m))

(defn ^:export init-app-env [app-env]
  (reset! planck.core/app-env (map-keys keyword (cljs.core/js->clj app-env))))

(defn repl-read-string [line]
  (r/read-string {:read-cond :allow :features #{:cljs}} line))

(defn ^:export is-readable? [line]
  (binding [r/*data-readers* tags/*cljs-data-readers*]
    (try
      (repl-read-string line)
      true
      (catch :default _
        false))))

(defn ns-form? [form]
  (and (seq? form) (= 'ns (first form))))

(def repl-specials '#{in-ns require require-macros doc})

(defn repl-special? [form]
  (and (seq? form) (repl-specials (first form))))

(def repl-special-doc-map
  '{in-ns          {:arglists ([name])
                    :doc      "Sets *cljs-ns* to the namespace named by the symbol, creating it if needed."}
    require        {:arglists ([& args])
                    :doc      "Loads libs, skipping any that are already loaded."}
    require-macros {:arglists ([& args])
                    :doc      "Similar to the require REPL special function but\n  only for macros."}
    doc            {:arglists ([name])
                    :doc      "Prints documentation for a var or special form given its name"}})

(defn- repl-special-doc [name-symbol]
  (assoc (repl-special-doc-map name-symbol)
    :name name-symbol
    :repl-special-function true))

(defn resolve
  "Given an analysis environment resolve a var. Analogous to
   clojure.core/resolve"
  [env sym]
  {:pre [(map? env) (symbol? sym)]}
  (try
    (ana/resolve-var env sym
      (ana/confirm-var-exists-throw))
    (catch :default _
      (ana/resolve-macro-var env sym))))

(defn ^:export get-current-ns []
  (str @current-ns))

(defn completion-candidates-for-ns [ns-sym allow-private?]
  (map (comp str key)
    (filter (if allow-private?
              identity
              #(not (:private (:meta (val %)))))
      (apply merge
        ((juxt :defs :macros)
          (get (:cljs.analyzer/namespaces @planck.core/st) ns-sym))))))

(defn is-completion? [buffer-match-suffix candidate]
  (re-find (js/RegExp. (str "^" buffer-match-suffix)) candidate))

(defn ^:export get-completions [buffer]
  (let [namespace-candidates (map str
                               (keys (:cljs.analyzer/namespaces @planck.core/st)))
        top-form? (re-find #"^\(\s*[^()]*$" buffer)
        all-candidates (set (concat namespace-candidates
                                    (completion-candidates-for-ns 'cljs.core false)
                                    (completion-candidates-for-ns @current-ns true)
                                    (when top-form? (map str repl-specials))))]
    (let [buffer-match-suffix (re-find #"[a-zA-Z]*$" buffer)
          buffer-prefix (subs buffer 0 (- (count buffer) (count buffer-match-suffix)))]
      (clj->js (if (= "" buffer-match-suffix)
                 []
                 (map #(str buffer-prefix %)
                   (sort
                     (filter (partial is-completion? buffer-match-suffix)
                       all-candidates))))))))

(defn extension->lang [extension]
  (if (= ".js" extension)
    :js
    :clj))

(defn load-and-callback! [path extension cb]
  (when-let [source (js/PLANCK_LOAD (str path extension))]
    (cb {:lang   (extension->lang extension)
         :source source})
    :loaded))

(defn load [{:keys [name macros path] :as full} cb]
  #_(prn full)
  (loop [extensions (if macros
                      [".clj" ".cljc"]
                      [".cljs" ".cljc" ".js"])]
    (if extensions
      (when-not (load-and-callback! path (first extensions) cb)
        (recur (next extensions)))
      (cb nil))))

(defn require [macros-ns? sym reload]
  (cljs.js/require
    {:*compiler*     st
     :*data-readers* tags/*cljs-data-readers*
     :*load-fn*      load
     :*eval-fn*      cljs/js-eval}
    sym
    reload
    {:macros-ns  macros-ns?
     :verbose    (:verbose @app-env)
     :source-map true}
    (fn [res]
      #_(println "require result:" res))))

(defn require-destructure [macros-ns? args]
  (let [[[_ sym] reload] args]
    (require macros-ns? sym reload)))

(defn ^:export run-main [main-ns args]
  (let [main-args (js->clj args)]
    (require false (symbol main-ns) nil)
    (cljs/eval-str st
      (str "(var -main)")
      nil
      {:ns         (symbol main-ns)
       :load       load
       :eval       cljs/js-eval
       :source-map true
       :context    :expr}
      (fn [{:keys [ns value error] :as ret}]
        (apply value args)))
    nil))

(defn load-core-source-maps! []
  (when-not (get (:source-maps @planck.core/st) 'planck.core)
    (swap! st update-in [:source-maps] merge {'planck.core
                                              (sm/decode
                                                (cljson->clj
                                                  (js/PLANCK_LOAD "planck/core.js.map")))
                                              'cljs.core
                                              (sm/decode
                                                (cljson->clj
                                                  (js/PLANCK_LOAD "cljs/core.js.map")))})))

(defn print-error [error]
  (let [cause (or (.-cause error) error)]
    (println (.-message cause))
    (load-core-source-maps!)
    (let [canonical-stacktrace (st/parse-stacktrace
                                 {}
                                 (.-stack cause)
                                 {:ua-product :safari}
                                 {:output-dir "file://(/goog/..)?"})]
      (println
        (st/mapped-stacktrace-str
          canonical-stacktrace
          (or (:source-maps @planck.core/st) {})
          nil)))))

(defn ^:export read-eval-print
  [source expression? print-nil-expression?]
  (binding [ana/*cljs-ns* @current-ns
            *ns* (create-ns @current-ns)
            r/*data-readers* tags/*cljs-data-readers*]
    (let [expression-form (and expression? (repl-read-string source))]
      (if (repl-special? expression-form)
        (let [env (assoc (ana/empty-env) :context :expr
                                         :ns {:name @current-ns})]
          (case (first expression-form)
            in-ns (reset! current-ns (second (second expression-form)))
            require (require-destructure false (rest expression-form))
            require-macros (require-destructure true (rest expression-form))
            doc (if (repl-specials (second expression-form))
                  (repl/print-doc (repl-special-doc (second expression-form)))
                  (repl/print-doc
                    (let [sym (second expression-form)
                          var (with-compiler-env st
                                (resolve env sym))]
                      (:meta var)))))
          (prn nil))
        (try
          (cljs/eval-str
            st
            source
            (if expression? source "File")
            (merge
              {:ns         @current-ns
               :load       load
               :eval       cljs/js-eval
               :source-map false
               :verbose    (:verbose @app-env)}
              (when expression?
                {:context       :expr
                 :def-emits-var true}))
            (fn [{:keys [ns value error] :as ret}]
              (if expression?
                (if-not error
                  (do
                    (when (or print-nil-expression?
                            (not (nil? value)))
                      (prn value))
                    (when-not
                      (or ('#{*1 *2 *3 *e} expression-form)
                        (ns-form? expression-form))
                      (set! *3 *2)
                      (set! *2 *1)
                      (set! *1 value))
                    (reset! current-ns ns)
                    nil)
                  (do
                    (set! *e error))))
              (when error
                (print-error error))))
          (catch :default e
            (print-error e)))))))

(ns planck.core
  (:require-macros [cljs.env.macros :refer [with-compiler-env]])
  (:require [cljs.js :as cljs]
            [cljs.tagged-literals :as tags]
            [cljs.tools.reader :as r]
            [cljs.analyzer :as ana]
            [cljs.repl :as repl]
            [cljs.stacktrace :as st]
            [cljs.source-map :as sm]
            [tailrecursion.cljson :refer [cljson->clj]]
            [planck.io]))

(defonce st (cljs/empty-state))

(defonce current-ns (atom 'cljs.user))

(defonce app-env (atom nil))

(defn map-keys [f m]
  (reduce-kv (fn [r k v] (assoc r (f k) v)) {} m))

(defn ^:export init-app-env [app-env]
  (reset! planck.core/app-env (map-keys keyword (cljs.core/js->clj app-env))))

(defn repl-read-string [line]
  (r/read-string {:read-cond :allow :features #{:cljs}} line))

(defn ^:export is-readable? [line]
  (binding [r/*data-readers* tags/*cljs-data-readers*]
    (try
      (repl-read-string line)
      true
      (catch :default _
        false))))

(defn ns-form? [form]
  (and (seq? form) (= 'ns (first form))))

(def repl-specials '#{in-ns require require-macros doc})

(defn repl-special? [form]
  (and (seq? form) (repl-specials (first form))))

(def repl-special-doc-map
  '{in-ns          {:arglists ([name])
                    :doc      "Sets *cljs-ns* to the namespace named by the symbol, creating it if needed."}
    require        {:arglists ([& args])
                    :doc      "Loads libs, skipping any that are already loaded."}
    require-macros {:arglists ([& args])
                    :doc      "Similar to the require REPL special function but\n  only for macros."}
    doc            {:arglists ([name])
                    :doc      "Prints documentation for a var or special form given its name"}})

(defn- repl-special-doc [name-symbol]
  (assoc (repl-special-doc-map name-symbol)
    :name name-symbol
    :repl-special-function true))

(defn resolve
  "Given an analysis environment resolve a var. Analogous to
   clojure.core/resolve"
  [env sym]
  {:pre [(map? env) (symbol? sym)]}
  (try
    (ana/resolve-var env sym
      (ana/confirm-var-exists-throw))
    (catch :default _
      (ana/resolve-macro-var env sym))))

(defn ^:export get-current-ns []
  (str @current-ns))

(defn completion-candidates-for-ns [ns-sym allow-private?]
  (map (comp str key)
    (filter (if allow-private?
              identity
              #(not (:private (:meta (val %)))))
      (apply merge
        ((juxt :defs :macros)
          (get (:cljs.analyzer/namespaces @planck.core/st) ns-sym))))))

(defn is-completion? [buffer-match-suffix candidate]
  (re-find (js/RegExp. (str "^" buffer-match-suffix)) candidate))

(defn ^:export get-completions [buffer]
  (let [namespace-candidates (map str
                               (keys (:cljs.analyzer/namespaces @planck.core/st)))
        top-form? (re-find #"^\(\s*[^()]*$" buffer)
        all-candidates (set (concat namespace-candidates
                                    (completion-candidates-for-ns 'cljs.core false)
                                    (completion-candidates-for-ns @current-ns true)
                                    (when top-form? (map str repl-specials))))]
    (let [buffer-match-suffix (re-find #"[a-zA-Z-]*$" buffer)
          buffer-prefix (subs buffer 0 (- (count buffer) (count buffer-match-suffix)))]
      (clj->js (if (= "" buffer-match-suffix)
                 []
                 (map #(str buffer-prefix %)
                   (sort
                     (filter (partial is-completion? buffer-match-suffix)
                       all-candidates))))))))

(defn extension->lang [extension]
  (if (= ".js" extension)
    :js
    :clj))

(defn load-and-callback! [path extension cb]
  (when-let [source (js/PLANCK_LOAD (str path extension))]
    (cb {:lang   (extension->lang extension)
         :source source})
    :loaded))

(defn load [{:keys [name macros path] :as full} cb]
  #_(prn full)
  (loop [extensions (if macros
                      [".clj" ".cljc"]
                      [".cljs" ".cljc" ".js"])]
    (if extensions
      (when-not (load-and-callback! path (first extensions) cb)
        (recur (next extensions)))
      (cb nil))))

(defn require [macros-ns? sym reload]
  (cljs.js/require
    {:*compiler*     st
     :*data-readers* tags/*cljs-data-readers*
     :*load-fn*      load
     :*eval-fn*      cljs/js-eval}
    sym
    reload
    {:macros-ns  macros-ns?
     :verbose    (:verbose @app-env)
     :source-map true}
    (fn [res]
      #_(println "require result:" res))))

(defn require-destructure [macros-ns? args]
  (let [[[_ sym] reload] args]
    (require macros-ns? sym reload)))

(defn ^:export run-main [main-ns args]
  (let [main-args (js->clj args)]
    (require false (symbol main-ns) nil)
    (cljs/eval-str st
      (str "(var -main)")
      nil
      {:ns         (symbol main-ns)
       :load       load
       :eval       cljs/js-eval
       :source-map true
       :context    :expr}
      (fn [{:keys [ns value error] :as ret}]
        (apply value args)))
    nil))

(defn load-core-source-maps! []
  (when-not (get (:source-maps @planck.core/st) 'planck.core)
    (swap! st update-in [:source-maps] merge {'planck.core
                                              (sm/decode
                                                (cljson->clj
                                                  (js/PLANCK_LOAD "planck/core.js.map")))
                                              'cljs.core
                                              (sm/decode
                                                (cljson->clj
                                                  (js/PLANCK_LOAD "cljs/core.js.map")))})))

(defn print-error [error]
  (let [cause (or (.-cause error) error)]
    (println (.-message cause))
    (load-core-source-maps!)
    (let [canonical-stacktrace (st/parse-stacktrace
                                 {}
                                 (.-stack cause)
                                 {:ua-product :safari}
                                 {:output-dir "file://(/goog/..)?"})]
      (println
        (st/mapped-stacktrace-str
          canonical-stacktrace
          (or (:source-maps @planck.core/st) {})
          nil)))))

(defn ^:export read-eval-print
  [source expression? print-nil-expression?]
  (binding [ana/*cljs-ns* @current-ns
            *ns* (create-ns @current-ns)
            r/*data-readers* tags/*cljs-data-readers*]
    (let [expression-form (and expression? (repl-read-string source))]
      (if (repl-special? expression-form)
        (let [env (assoc (ana/empty-env) :context :expr
                                         :ns {:name @current-ns})]
          (case (first expression-form)
            in-ns (reset! current-ns (second (second expression-form)))
            require (require-destructure false (rest expression-form))
            require-macros (require-destructure true (rest expression-form))
            doc (if (repl-specials (second expression-form))
                  (repl/print-doc (repl-special-doc (second expression-form)))
                  (repl/print-doc
                    (let [sym (second expression-form)
                          var (with-compiler-env st
                                (resolve env sym))]
                      (:meta var)))))
          (prn nil))
        (try
          (cljs/eval-str
            st
            source
            (if expression? source "File")
            (merge
              {:ns         @current-ns
               :load       load
               :eval       cljs/js-eval
               :source-map false
               :verbose    (:verbose @app-env)}
              (when expression?
                {:context       :expr
                 :def-emits-var true}))
            (fn [{:keys [ns value error] :as ret}]
              (if expression?
                (if-not error
                  (do
                    (when (or print-nil-expression?
                            (not (nil? value)))
                      (prn value))
                    (when-not
                      (or ('#{*1 *2 *3 *e} expression-form)
                        (ns-form? expression-form))
                      (set! *3 *2)
                      (set! *2 *1)
                      (set! *1 value))
                    (reset! current-ns ns)
                    nil)
                  (do
                    (set! *e error))))
              (when error
                (print-error error))))
          (catch :default e
            (print-error e)))))))

Include hyphen in suffix

Include hyphen in suffix

Former-commit-id: 1ba6d2c4c7641dbdb638a7c8b26db03f874f219d


(ns trak.utils
  (:require [datascript.core :as d]))

(defn js-apply [f target args]
  (.apply f target (to-array args)))

;; Console-related stuff

(defn log [& args]
  (js-apply (.-log js/console) js/console args))

(defn info [& args]
  (js-apply (.-info js/console) js/console args))

(defn debug [& args]
  (js-apply (.-debug js/console) js/console args))

(defn error [& args]
  (js-apply (.-error js/console) js/console args))

;; DB Utils

(defn current-path [db]
  (when-let [match (:application/state (first (d/q '[:find [(pull ?e [:application/state])]
                                                     :in $ ?type
                                                     :where [?e :application/state-type ?type]]
                                                   db :path)))]
    {:handler (:handler match) :params (:params match)}))

Add current-path helper

Add current-path helper


(ns status-im.ui.screens.accounts.recover.views
  (:require-macros [status-im.utils.views :refer [defview letsubs]])
  (:require [re-frame.core :as re-frame]
            [reagent.core :as reagent]
            [status-im.ui.components.text-input.view :as text-input]
            [status-im.ui.components.react :as react]
            [status-im.ui.components.status-bar.view :as status-bar]
            [status-im.ui.components.toolbar.view :as toolbar]
            [status-im.i18n :as i18n]
            [status-im.ui.screens.accounts.recover.styles :as styles]
            [status-im.ui.components.styles :as components.styles]
            [status-im.utils.config :as config]
            [status-im.utils.core :as utils.core]
            [status-im.react-native.js-dependencies :as js-dependencies]
            [status-im.ui.components.common.common :as components.common]
            [status-im.utils.security :as security]))

(defview passphrase-input [passphrase error warning]
  (letsubs [input-ref (reagent/atom nil)]
    {:component-did-mount (fn [_] (when config/testfairy-enabled?
                                    (.hideView js-dependencies/testfairy @input-ref)))
     ;;TODO(rasom): remove this line when default-value will
     ;;be fixed in react-native-desktop
     :should-component-update (fn [] false)}
    [text-input/text-input-with-label
     {:style               styles/recovery-phrase-input
      :height              92
      :ref                 (partial reset! input-ref)
      :label               (i18n/label :t/recovery-phrase)
      :placeholder         (i18n/label :t/enter-12-words)
      :multiline           true
      :default-value       passphrase
      :auto-correct        false
      :on-change-text      #(re-frame/dispatch [:accounts.recover.ui/passphrase-input-changed (security/mask-data %)])
      :on-blur             #(re-frame/dispatch [:accounts.recover.ui/passphrase-input-blured])
      :error               (cond error (i18n/label error)
                                 warning (i18n/label warning))}]))

(defview password-input [password error on-submit-editing]
  [react/view {:style                       styles/password-input
               :important-for-accessibility :no-hide-descendants}
   [text-input/text-input-with-label
    {:label             (i18n/label :t/password)
     :placeholder       (i18n/label :t/enter-password)
     :default-value     password
     :auto-focus        false
     :on-change-text    #(re-frame/dispatch [:accounts.recover.ui/password-input-changed (security/mask-data %)])
     :on-blur           #(re-frame/dispatch [:accounts.recover.ui/password-input-blured])
     :secure-text-entry true
     :error             (when error (i18n/label error))
     :on-submit-editing on-submit-editing}]])

(defview recover []
  (letsubs [recovered-account [:get-recover-account]]
    (let [{:keys [passphrase password processing passphrase-valid? password-valid?
                  password-error passphrase-error passphrase-warning processing?]} recovered-account
          valid-form? (and password-valid? passphrase-valid?)
          disabled?   (or (not recovered-account) processing? (not valid-form?))
          sign-in     #(re-frame/dispatch [:accounts.recover.ui/sign-in-button-pressed])]
      [react/keyboard-avoiding-view {:style styles/screen-container}
       [status-bar/status-bar]
       [toolbar/toolbar nil toolbar/default-nav-back
        [toolbar/content-title (i18n/label :t/sign-in-to-another)]]
       [components.common/separator]
       [react/view styles/inputs-container
        [passphrase-input (or passphrase "") passphrase-error passphrase-warning]
        [password-input (or password "") password-error (when-not disabled? sign-in)]]
       [react/view components.styles/flex]
       (if processing
         [react/view styles/processing-view
          [react/activity-indicator {:animating true}]
          [react/i18n-text {:style styles/sign-you-in
                            :key   :sign-you-in}]]
         [react/view {:style styles/bottom-button-container}
          [react/view {:style components.styles/flex}]
          [components.common/bottom-button
           {:forward?  true
            :label     (i18n/label :t/sign-in)
            :disabled? disabled?
            :on-press  sign-in}]])])))

(ns status-im.ui.screens.accounts.recover.views
  (:require-macros [status-im.utils.views :refer [defview letsubs]])
  (:require [re-frame.core :as re-frame]
            [reagent.core :as reagent]
            [status-im.ui.components.text-input.view :as text-input]
            [status-im.ui.components.react :as react]
            [status-im.ui.components.status-bar.view :as status-bar]
            [status-im.ui.components.toolbar.view :as toolbar]
            [status-im.i18n :as i18n]
            [status-im.ui.screens.accounts.recover.styles :as styles]
            [status-im.ui.components.styles :as components.styles]
            [status-im.utils.config :as config]
            [status-im.utils.core :as utils.core]
            [status-im.react-native.js-dependencies :as js-dependencies]
            [status-im.ui.components.common.common :as components.common]
            [status-im.utils.security :as security]))

(defview passphrase-input [passphrase error warning]
  (letsubs [input-ref (reagent/atom nil)]
    [text-input/text-input-with-label
     {:style               styles/recovery-phrase-input
      :height              92
      :ref                 (partial reset! input-ref)
      :label               (i18n/label :t/recovery-phrase)
      :placeholder         (i18n/label :t/enter-12-words)
      :multiline           true
      :default-value       passphrase
      :auto-correct        false
      :on-change-text      #(re-frame/dispatch [:accounts.recover.ui/passphrase-input-changed (security/mask-data %)])
      :on-blur             #(re-frame/dispatch [:accounts.recover.ui/passphrase-input-blured])
      :error               (cond error (i18n/label error)
                                 warning (i18n/label warning))}]))

(defview password-input [password error on-submit-editing]
  [react/view {:style                       styles/password-input
               :important-for-accessibility :no-hide-descendants}
   [text-input/text-input-with-label
    {:label             (i18n/label :t/password)
     :placeholder       (i18n/label :t/enter-password)
     :default-value     password
     :auto-focus        false
     :on-change-text    #(re-frame/dispatch [:accounts.recover.ui/password-input-changed (security/mask-data %)])
     :on-blur           #(re-frame/dispatch [:accounts.recover.ui/password-input-blured])
     :secure-text-entry true
     :error             (when error (i18n/label error))
     :on-submit-editing on-submit-editing}]])

(defview recover []
  (letsubs [recovered-account [:get-recover-account]]
    (let [{:keys [passphrase password processing passphrase-valid? password-valid?
                  password-error passphrase-error passphrase-warning processing?]} recovered-account
          valid-form? (and password-valid? passphrase-valid?)
          disabled?   (or (not recovered-account) processing? (not valid-form?))
          sign-in     #(re-frame/dispatch [:accounts.recover.ui/sign-in-button-pressed])]
      [react/keyboard-avoiding-view {:style styles/screen-container}
       [status-bar/status-bar]
       [toolbar/toolbar nil toolbar/default-nav-back
        [toolbar/content-title (i18n/label :t/sign-in-to-another)]]
       [components.common/separator]
       [react/view styles/inputs-container
        [passphrase-input (or passphrase "") passphrase-error passphrase-warning]
        [password-input (or password "") password-error (when-not disabled? sign-in)]]
       [react/view components.styles/flex]
       (if processing
         [react/view styles/processing-view
          [react/activity-indicator {:animating true}]
          [react/i18n-text {:style styles/sign-you-in
                            :key   :sign-you-in}]]
         [react/view {:style styles/bottom-button-container}
          [react/view {:style components.styles/flex}]
          [components.common/bottom-button
           {:forward?  true
            :label     (i18n/label :t/sign-in)
            :disabled? disabled?
            :on-press  sign-in}]])])))

fix passphrase-validation

[#6162] fix passphrase-validation


;; https://github.com/technomancy/leiningen/blob/stable/doc/PROFILES.md
{:user
 {:plugins
  [[cider/cider-nrepl "0.27.2"]
   [lein-ancient "1.0.0-RC3"]
   [lein-check-namespace-decls "1.0.4"]
   [lein-cljfmt "0.8.0"]
   [lein-nsorg "0.3.0"]
   [nrepl "0.9.0-beta3"]
   [refactor-nrepl "3.0.0-alpha13"]]}
  :dependencies
   [#_[alembic "0.3.2"]
    [clj-kondo "RELEASE"]
    [antq "RELEASE"]
    [vvvvalvalval/scope-capture "0.3.2"]]
  :injections [(require 'sc.api)]
  :aliases {"clj-kondo" ["run" "-m" "clj-kondo.main"]
            "outdated"  ["run" "-m" "antq.core"]}}

;; https://github.com/technomancy/leiningen/blob/stable/doc/PROFILES.md
{:user
 {:plugins
  [[cider/cider-nrepl "0.27.4"]
   [lein-ancient "1.0.0-RC3"]
   [lein-check-namespace-decls "1.0.4"]
   [lein-cljfmt "0.8.0"]
   [lein-nsorg "0.3.0"]
   [nrepl "0.9.0"]
   [refactor-nrepl "3.1.0"]]}
  :dependencies
   [#_[alembic "0.3.2"]
    [clj-kondo "RELEASE"]
    [antq "RELEASE"]
    [vvvvalvalval/scope-capture "0.3.2"]]
  :injections [(require 'sc.api)]
  :aliases {"clj-kondo" ["run" "-m" "clj-kondo.main"]
            "outdated"  ["run" "-m" "antq.core"]}}

Update config for Clojure projects

Update config for Clojure projects


(ns vip.data-processor.validation.data-spec.v5-1
  (:require [vip.data-processor.validation.data-spec.value-format :as format]
            [vip.data-processor.validation.data-spec.coerce :as coerce]))

(def street-segments
  {:columns [{:name "id"}
             {:name "includes_all_addresses"
              :coerce coerce/postgres-boolean}
             {:name "address_direction"}
             {:name "city"}
             {:name "odd_even_both"}
             {:name "precinct_id"}
             {:name "start_house_number"
              :format format/all-digits
              :coerce coerce/coerce-integer}
             {:name "end_house_number"
              :format format/all-digits
              :coerce coerce/coerce-integer}
             {:name "state"}
             {:name "street_direction"}
             {:name "street_name"}
             {:name "street_suffix"}
             {:name "zip"}]})

(def data-specs
  [{:filename "office.txt"
    :table :offices
    :columns [{:name "id"}
              {:name "contact_information_id"}
              {:name "electoral_district_id"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "filing_deadline"}
              {:name "is_partisan"}
              {:name "name"}
              {:name "office_holder_person_id"}
              {:name "term_type"}
              {:name "term_start_date"}
              {:name "term_end_date"}]}
   {:filename "voter_service.txt"
    :table :voter-services
    :columns [{:name "id"}
              {:name "department_id"}
              {:name "contact_information_id"}
              {:name "description"}
              {:name "election_official_person_id"}
              {:name "voter_service_type"}
              {:name "other_type"}]}
   {:filename "ballot_measure_contest.txt"
    :table :ballot-measure-contests
    :columns [{:name "abbreviation"}
              {:name "ballot_selection_ids"}
              {:name "ballot_sub_title"}
              {:name "ballot_title"}
              {:name "electoral_district_id"}
              {:name "electorate_specification"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "has_rotation"}
              {:name "name"}
              {:name "sequence_order"}
              {:name "vote_variation"}
              {:name "other_vote_variation"}
              {:name "id"}
              {:name "con_statement"}
              {:name "effect_of_abstain"}
              {:name "full_text"}
              {:name "info_uri"}
              {:name "passage_threshold"}
              {:name "pro_statement"}
              {:name "summary_text"}
              {:name "type"}
              {:name "other_type"}]}
   {:filename "ballot_selection.txt"
    :table :ballot-selections
    :columns [{:name "id"}
              {:name "ballot_measure_contest_ids"}
              {:name "ballot_measure_contest_selection_ids"}
              {:name "text"}
              {:name "candidate_id"}
              {:name "endorsement_party_id"}
              {:name "is_write_in"}
              {:name "sequence_order"}]}
   {:filename "ballot_style.txt"
    :table :ballot-styles
    :columns [{:name "id"}
              {:name "image_uri"}
              {:name "ordered_contest_ids"}
              {:name "party_id"}]}
   {:filename "candidate.txt"
    :table :candidates
    :columns [{:name "id"}
              {:name "ballot_name"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "file_date"}
              {:name "is_incumbent"}
              {:name "is_top_ticket"}
              {:name "party_id"}
              {:name "person_id"}
              {:name "post_election_status"}
              {:name "pre_election_status"}
              {:name "sequence_order"}
              {:name "contest_id"}]}
   {:filename "candidate_contest.txt"
    :table :candidate-contests
    :columns [{:name "id"}
              {:name "abbreviation"}
              {:name "ballot_sub_title"}
              {:name "ballot_title"}
              ;; {:name "ballot_selection_ids" ;; this is in a join-table named 'candidate-contest-ballot-selections'
              {:name "electoral_district_id"}
              {:name "electorate_specification"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "has_rotation"}
              {:name "name"}
              {:name "sequence_order"}
              {:name "vote_variation"}
              {:name "other_vote_variation"}
              {:name "number_elected"}
              {:name "primary_party_ids"}
              {:name "votes_allowed"}
              {:name "office_ids"}]}
   {:filename "department.txt"
    :table :departments
    :columns [{:name "id"}
              {:name "department_name"}
              {:name "election_administration_id"}
              {:name "contact_information_id"}
              {:name "election_official_person_id"}]}
   {:filename "election.txt"
    :table :elections
    :columns [{:name "id"}
              {:name "date"}
              {:name "name"}
              {:name "election_type"}
              {:name "state_id"}
              {:name "is_statewide"}
              {:name "registration_info"}
              {:name "absentee_ballot_info"}
              {:name "results_uri"}
              {:name "polling_hours"}
              {:name "has_election_day_registration"}
              {:name "registration_deadline"}
              {:name "absentee_request_deadline"}
              {:name "hours_open_id"}]}
   {:filename "election_administration.txt"
    :table :election-administrations
    :columns [{:name "id"}
              {:name "absentee_uri"}
              {:name "am_i_registered_uri"}
              {:name "elections_uri"}
              {:name "registration_uri"}
              {:name "rules_uri"}
              {:name "what_is_on_my_ballot_uri"}
              {:name "where_do_i_vote_uri"}]}
   {:filename "electoral_district.txt"
    :table :electoral-districts
    :columns [{:name "id"}
              {:name "name"}
              {:name "type"}
              {:name "number"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "other_type"}]}
   {:filename "hours_open.txt"
    :table :hours-open
    :columns [{:name "id"}
              {:name "schedule_id"}]}
   {:filename "schedule.txt"
    :table :schedules
    :columns [{:name "id"}
              {:name "start_time"}
              {:name "end_time"}
              {:name "start_time2"}
              {:name "end_time2"}
              {:name "is_only_by_appointment"}
              {:name "is_or_by_appointment"}
              {:name "is_subject_to_change"}
              {:name "start_date"}
              {:name "end_date"}]}
   {:filename "locality.txt"
    :table :localities
    :columns [{:name "id"}
              {:name "name"}
              {:name "state_id"}
              {:name "type"}
              {:name "other_type"}
              {:name "election_administration_id"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}]}
   {:filename "locality_polling_location.txt"
    :table :locality-polling-locations
    :columns [{:name "locality_id"}
              {:name "polling_location_id"}]}
   {:filename "party.txt"
    :table :parties
    :columns [{:name "id"}
              {:name "abbreviation"}
              {:name "color"}
              {:name "external_identifiers"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "logo_uri"}
              {:name "name"}]}
   {:filename "person.txt"
    :table :people
    :columns [{:name "id"}
              {:name "contact_information_id"}
              {:name "date_of_birth"}
              {:name "first_name"}
              {:name "last_name"}
              {:name "middle_name"}
              {:name "nickname"}
              {:name "prefix"}
              {:name "suffix"}
              {:name "title"}
              {:name "profession"}
              {:name "party_id"}]}
   {:filename "polling_location.txt"
    :table :polling-locations
    :columns [{:name "id"}
              {:name "address_line"}
              {:name "directions"}
              {:name "hours"}
              {:name "hours_open_id"}
              {:name "photo_uri"}
              {:name "is_drop_box"}
              {:name "is_early_voting"}
              {:name "latitude"}
              {:name "longitude"}
              {:name "latlng_source"}]}
   {:filename "precinct.txt"
    :table :precincts
    :columns [{:name "id"}
              {:name "name"}
              {:name "number"}
              {:name "locality_id"}
              {:name "ward"}
              {:name "is_mail_only"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "precinct_split_name"}
              {:name "ballot_style_id"}]}
   {:filename "precinct_electoral_district.txt"
    :table :precinct-electoral-districts
    :columns [{:name "precinct_id"}
              {:name "electoral_district_id"}]}
   {:filename "precinct_polling_location.txt"
    :table :precinct-polling-locations
    :columns [{:name "precinct_id"}
              {:name "polling_location_id"}]}
   {:filename "source.txt"
    :table :sources
    :columns [{:name "id"}
              {:name "name"}
              {:name "vip_id"}
              {:name "date_time"}
              {:name "description"}
              {:name "organization_uri"}
              {:name "feed_contact_information_id"}
              {:name "terms_of_use_uri"}
              {:name "version"}]}
   {:filename "state.txt"
    :table :states
    :columns [{:name "id"}
              {:name "name"}
              {:name "election_administration_id"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}]}
   {:filename "state_polling_location.txt"
    :table :state-polling-locations
    :columns [{:name "polling_location_id"}
              {:name "state_id"}]}
   {:filename "street_segment.txt"
    :table :street-segments
    :columns [{:name "id"}
              {:name "includes_all_addresses"
               :coerce coerce/postgres-boolean}
              {:name "address_direction"}
              {:name "city"}
              {:name "odd_even_both"}
              {:name "precinct_id"}
              {:name "start_house_number"
               :format format/all-digits
               :coerce coerce/coerce-integer}
              {:name "end_house_number"
               :format format/all-digits
               :coerce coerce/coerce-integer}
              {:name "unit_number"}
              {:name "state"}
              {:name "street_direction"}
              {:name "street_name"}
              {:name "street_suffix"}
              {:name "zip"}]}])

(ns vip.data-processor.validation.data-spec.v5-1
  (:require [vip.data-processor.validation.data-spec.value-format :as format]
            [vip.data-processor.validation.data-spec.coerce :as coerce]))

(def street-segments
  {:columns [{:name "id"}
             {:name "includes_all_addresses"
              :coerce coerce/postgres-boolean}
             {:name "address_direction"}
             {:name "city"}
             {:name "odd_even_both"}
             {:name "precinct_id"}
             {:name "start_house_number"
              :format format/all-digits
              :coerce coerce/coerce-integer}
             {:name "end_house_number"
              :format format/all-digits
              :coerce coerce/coerce-integer}
             {:name "state"}
             {:name "street_direction"}
             {:name "street_name"}
             {:name "street_suffix"}
             {:name "zip"}]})

(def data-specs
  [{:filename "office.txt"
    :table :offices
    :columns [{:name "id"}
              {:name "contact_information_id"}
              {:name "electoral_district_id"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "filing_deadline"}
              {:name "is_partisan"}
              {:name "name"}
              {:name "office_holder_person_ids"}
              {:name "term_type"}
              {:name "term_start_date"}
              {:name "term_end_date"}]}
   {:filename "voter_service.txt"
    :table :voter-services
    :columns [{:name "id"}
              {:name "department_id"}
              {:name "contact_information_id"}
              {:name "description"}
              {:name "election_official_person_id"}
              {:name "voter_service_type"}
              {:name "other_type"}]}
   {:filename "ballot_measure_contest.txt"
    :table :ballot-measure-contests
    :columns [{:name "abbreviation"}
              {:name "ballot_selection_ids"}
              {:name "ballot_sub_title"}
              {:name "ballot_title"}
              {:name "electoral_district_id"}
              {:name "electorate_specification"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "has_rotation"}
              {:name "name"}
              {:name "sequence_order"}
              {:name "vote_variation"}
              {:name "other_vote_variation"}
              {:name "id"}
              {:name "con_statement"}
              {:name "effect_of_abstain"}
              {:name "full_text"}
              {:name "info_uri"}
              {:name "passage_threshold"}
              {:name "pro_statement"}
              {:name "summary_text"}
              {:name "type"}
              {:name "other_type"}]}
   {:filename "ballot_selection.txt"
    :table :ballot-selections
    :columns [{:name "id"}
              {:name "ballot_measure_contest_ids"}
              {:name "ballot_measure_contest_selection_ids"}
              {:name "text"}
              {:name "candidate_id"}
              {:name "endorsement_party_id"}
              {:name "is_write_in"}
              {:name "sequence_order"}]}
   {:filename "ballot_style.txt"
    :table :ballot-styles
    :columns [{:name "id"}
              {:name "image_uri"}
              {:name "ordered_contest_ids"}
              {:name "party_id"}]}
   {:filename "candidate.txt"
    :table :candidates
    :columns [{:name "id"}
              {:name "ballot_name"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "file_date"}
              {:name "is_incumbent"}
              {:name "is_top_ticket"}
              {:name "party_id"}
              {:name "person_id"}
              {:name "post_election_status"}
              {:name "pre_election_status"}
              {:name "contest_id"}]}
   {:filename "candidate_contest.txt"
    :table :candidate-contests
    :columns [{:name "id"}
              {:name "abbreviation"}
              {:name "ballot_sub_title"}
              {:name "ballot_title"}
              {:name "ballot_selection_ids"}
              {:name "electoral_district_id"}
              {:name "electorate_specification"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "has_rotation"}
              {:name "name"}
              {:name "sequence_order"}
              {:name "vote_variation"}
              {:name "other_vote_variation"}
              {:name "number_elected"}
              {:name "primary_party_ids"}
              {:name "votes_allowed"}
              {:name "office_ids"}]}
   {:filename "department.txt"
    :table :departments
    :columns [{:name "id"}
              {:name "department_name"}
              {:name "election_administration_id"}
              {:name "contact_information_id"}
              {:name "election_official_person_id"}]}
   {:filename "election.txt"
    :table :elections
    :columns [{:name "id"}
              {:name "date"}
              {:name "name"}
              {:name "election_type"}
              {:name "state_id"}
              {:name "is_statewide"}
              {:name "registration_info"}
              {:name "absentee_ballot_info"}
              {:name "results_uri"}
              {:name "polling_hours"}
              {:name "has_election_day_registration"}
              {:name "registration_deadline"}
              {:name "absentee_request_deadline"}
              {:name "hours_open_id"}]}
   {:filename "election_administration.txt"
    :table :election-administrations
    :columns [{:name "id"}
              {:name "absentee_uri"}
              {:name "am_i_registered_uri"}
              {:name "elections_uri"}
              {:name "registration_uri"}
              {:name "rules_uri"}
              {:name "what_is_on_my_ballot_uri"}
              {:name "where_do_i_vote_uri"}]}
   {:filename "electoral_district.txt"
    :table :electoral-districts
    :columns [{:name "id"}
              {:name "name"}
              {:name "type"}
              {:name "number"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "other_type"}]}
   {:filename "hours_open.txt"
    :table :hours-open
    :columns [{:name "id"}
              {:name "schedule_id"}]}
   {:filename "schedule.txt"
    :table :schedules
    :columns [{:name "id"}
              {:name "start_time"}
              {:name "end_time"}
              {:name "start_time2"}
              {:name "end_time2"}
              {:name "is_only_by_appointment"}
              {:name "is_or_by_appointment"}
              {:name "is_subject_to_change"}
              {:name "start_date"}
              {:name "end_date"}]}
   {:filename "locality.txt"
    :table :localities
    :columns [{:name "id"}
              {:name "name"}
              {:name "state_id"}
              {:name "type"}
              {:name "other_type"}
              {:name "election_administration_id"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "polling_location_ids"}]}
   {:filename "party.txt"
    :table :parties
    :columns [{:name "id"}
              {:name "abbreviation"}
              {:name "color"}
              {:name "external_identifiers"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "logo_uri"}
              {:name "name"}]}
   {:filename "person.txt"
    :table :people
    :columns [{:name "id"}
              {:name "contact_information_id"}
              {:name "date_of_birth"}
              {:name "first_name"}
              {:name "last_name"}
              {:name "middle_name"}
              {:name "nickname"}
              {:name "prefix"}
              {:name "suffix"}
              {:name "title"}
              {:name "profession"}
              {:name "party_id"}
              {:name "gender"}]}
   {:filename "polling_location.txt"
    :table :polling-locations
    :columns [{:name "id"}
              {:name "address_line"}
              {:name "directions"}
              {:name "hours"}
              {:name "hours_open_id"}
              {:name "photo_uri"}
              {:name "is_drop_box"}
              {:name "is_early_voting"}
              {:name "latitude"}
              {:name "longitude"}
              {:name "latlng_source"}]}
   {:filename "precinct.txt"
    :table :precincts
    :columns [{:name "id"}
              {:name "name"}
              {:name "number"}
              {:name "locality_id"}
              {:name "ward"}
              {:name "is_mail_only"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "precinct_split_name"}
              {:name "ballot_style_id"}
              {:name "electoral_district_ids"}
              {:name "polling_location_ids"}]}
   {:filename "source.txt"
    :table :sources
    :columns [{:name "id"}
              {:name "name"}
              {:name "vip_id"}
              {:name "date_time"}
              {:name "description"}
              {:name "organization_uri"}
              {:name "feed_contact_information_id"}
              {:name "terms_of_use_uri"}
              {:name "version"}]}
   {:filename "state.txt"
    :table :states
    :columns [{:name "id"}
              {:name "name"}
              {:name "election_administration_id"}
              {:name "external_identifier_type"}
              {:name "external_identifier_othertype"}
              {:name "external_identifier_value"}
              {:name "polling_location_ids"}]}
   {:filename "street_segment.txt"
    :table :street-segments
    :columns [{:name "id"}
              {:name "includes_all_addresses"
               :coerce coerce/postgres-boolean}
              {:name "address_direction"}
              {:name "city"}
              {:name "odd_even_both"}
              {:name "precinct_id"}
              {:name "start_house_number"
               :format format/all-digits
               :coerce coerce/coerce-integer}
              {:name "end_house_number"
               :format format/all-digits
               :coerce coerce/coerce-integer}
              {:name "unit_number"}
              {:name "state"}
              {:name "street_direction"}
              {:name "street_name"}
              {:name "street_suffix"}
              {:name "zip"}]}])

Update data-spec for 5.1 CSVs

Update data-spec for 5.1 CSVs


(ns chameleon.crawler
  (:require [chameleon.client :as client]
            [chameleon.db :as db]
            [chameleon.utils :as utils]
            [monger.collection :as mc]))

(defn create-user-map [user-id]
  ; We're actually making two calls to the Stack Overflow API
  ; here -- should find a way to save the data from one call
  ; and re-use it.
  {:user-id user-id,
   :display-name (client/display-name user-id),
   :rep (client/rep user-id)})

(defn crawl-users [user-ids]
  (map create-user-map user-ids))

(defn create-user-document [user-map]
  {:_id (:user-id user-map), :display_name (:display-name user-map)})

(defn create-sample-document [user-map]
  {:user (:user-id user-map),
   :timestamp (utils/utcnow),
   :reputation (:rep user-map)})

(defn update-user! [user-map]
  (mc/update db/db
             "users"
             {:_id (:user-id user-map)}
             {:display_name (:display-name user-map)}))

(defn create-sample! [user-map]
  (mc/insert db/db "samples" (create-sample-document user-map)))

(defn update-users! [user-maps]
  (dorun (map update-user! user-maps)))

(defn insert-samples! [user-maps]
  (dorun (map create-sample! user-maps)))

(ns chameleon.crawler
  (:require [chameleon.client :as client]
            [chameleon.db :as db]
            [chameleon.utils :as utils]
            [monger.collection :as mc]))

(defn create-user-map [user-id]
  ; We're actually making two calls to the Stack Overflow API
  ; here -- should find a way to save the data from one call
  ; and re-use it.
  {:user-id user-id,
   :display-name (client/display-name user-id),
   :rep (client/rep user-id)})

(defn crawl-users [user-ids]
  (map create-user-map user-ids))

(defn create-user-document [user-map]
  {:_id (:user-id user-map), :display_name (:display-name user-map)})

(defn create-sample-document [user-map]
  {:user (:user-id user-map),
   :timestamp (utils/utcnow),
   :reputation (:rep user-map)})

(defn update-user! [user-map]
  (mc/update db/db
             "users"
             {:_id (:user-id user-map)}
             {:display_name (:display-name user-map)}))

(defn create-sample! [user-map]
  (mc/insert db/db "samples" (create-sample-document user-map)))

(defn update-users! [user-maps]
  (doseq [u user-maps] (update-user! u)))

(defn insert-samples! [user-maps]
  (doseq [u user-maps] (create-sample! u)))

Use doseq instead of map

Use doseq instead of map

doseq is specifically intended to be used with functions that cause side
effects.


;; 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) 2015-2016 Andrey Antukh <niwi@niwi.nz>
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>

(ns uxbox.ui
  (:require [sablono.core :as html :refer-macros [html]]
            [promesa.core :as p]
            [goog.dom :as gdom]
            [rum.core :as rum]
            [lentes.core :as l]
            [uxbox.state :as s]
            [uxbox.router :as r]
            [uxbox.rstore :as rs]
            [uxbox.data.projects :as dp]
            [uxbox.data.users :as udu]
            [uxbox.ui.lightbox :as ui-lightbox]
            [uxbox.ui.auth :as ui-auth]
            [uxbox.ui.dashboard :as ui-dashboard]
            [uxbox.ui.settings :as ui-settings]
            [uxbox.ui.workspace :refer (workspace)]
            [uxbox.ui.mixins :as mx]
            [uxbox.ui.shapes]))

(def ^:const auth-data
  (as-> (l/key :auth) $
    (l/focus-atom $ s/state)))

(def ^:const +unrestricted+
  #{:auth/login})

(def ^:const restricted?
  (complement +unrestricted+))

(defn app-render
  [own]
  (let [route (rum/react r/route-l)
        auth (rum/react auth-data)
        location (:id route)
        params (:params route)]
    (if (and (restricted? location) (not auth))
      (do (p/schedule 0 #(r/go :auth/login)) nil)
      (case location
        :auth/login (ui-auth/login)
        :dashboard/projects (ui-dashboard/projects-page)
        :dashboard/elements (ui-dashboard/elements-page)
        :dashboard/icons (ui-dashboard/icons-page)
        :dashboard/colors (ui-dashboard/colors-page)
        :settings/profile (ui-settings/profile-page)
        :settings/password (ui-settings/password-page)
        :settings/notifications (ui-settings/notifications-page)
        :workspace/page (let [projectid (:project-uuid params)
                              pageid (:page-uuid params)]
                          (workspace projectid pageid))
        nil
        ))))

(defn app-will-mount
  [own]
  (when @auth-data
    (rs/emit! (udu/fetch-profile)
              (dp/fetch-projects)))
  own)

(def app
  (mx/component
   {:render app-render
    :will-mount app-will-mount
    :mixins [rum/reactive]
    :name "app"}))

(defn init
  []
  (println "ui/init")
  (let [app-dom (gdom/getElement "app")
        lb-dom (gdom/getElement "lightbox")]
    (rum/mount (app) app-dom)
    (rum/mount (ui-lightbox/lightbox) lb-dom)))

;; 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) 2015-2016 Andrey Antukh <niwi@niwi.nz>
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>

(ns uxbox.ui
  (:require [sablono.core :as html :refer-macros [html]]
            [promesa.core :as p]
            [goog.dom :as gdom]
            [rum.core :as rum]
            [lentes.core :as l]
            [uxbox.state :as s]
            [uxbox.router :as r]
            [uxbox.rstore :as rs]
            [uxbox.data.projects :as dp]
            [uxbox.data.users :as udu]
            [uxbox.ui.icons :as i]
            [uxbox.ui.lightbox :as ui-lightbox]
            [uxbox.ui.auth :as ui-auth]
            [uxbox.ui.dashboard :as ui-dashboard]
            [uxbox.ui.settings :as ui-settings]
            [uxbox.ui.workspace :refer (workspace)]
            [uxbox.ui.mixins :as mx]
            [uxbox.ui.shapes]))

;; --- Lentes

(def ^:const auth-data-l
  (-> (l/key :auth)
      (l/focus-atom s/state)))

(def ^:const loader-l
  (-> (l/key :loader)
      (l/focus-atom s/state)))

;; --- Constants

(def ^:const +unrestricted+ #{:auth/login})
(def ^:const restricted? (complement +unrestricted+))

;; --- Main App (Component)

(defn app-render
  [own]
  (let [route (rum/react r/route-l)
        auth (rum/react auth-data-l)
        location (:id route)
        params (:params route)]
    (if (and (restricted? location) (not auth))
      (do (p/schedule 0 #(r/go :auth/login)) nil)
      (case location
        :auth/login (ui-auth/login)
        :dashboard/projects (ui-dashboard/projects-page)
        :dashboard/elements (ui-dashboard/elements-page)
        :dashboard/icons (ui-dashboard/icons-page)
        :dashboard/colors (ui-dashboard/colors-page)
        :settings/profile (ui-settings/profile-page)
        :settings/password (ui-settings/password-page)
        :settings/notifications (ui-settings/notifications-page)
        :workspace/page (let [projectid (:project-uuid params)
                              pageid (:page-uuid params)]
                          (workspace projectid pageid))
        nil
        ))))

(defn app-will-mount
  [own]
  (when @auth-data-l
    (rs/emit! (udu/fetch-profile)))
  own)

(def app
  (mx/component
   {:render app-render
    :will-mount app-will-mount
    :mixins [rum/reactive]
    :name "app"}))

;; --- Loader

(defn loader-render
  [own]
  (when (rum/react loader-l)
    (html
     [:div.loader-content i/loader])))

(def loader
  (mx/component
   {:render loader-render
    :name "loader"
    :mixins [rum/reactive mx/static]}))

;; --- Main Entry Point

(defn init
  []
  (let [app-dom (gdom/getElement "app")
        lightbox-dom (gdom/getElement "lightbox")
        loader-dom (gdom/getElement "loader")]
    (rum/mount (app) app-dom)
    (rum/mount (ui-lightbox/lightbox) lightbox-dom)
    (rum/mount (loader) loader-dom)))

Add main loader in ui ns.

Add main loader in ui ns.