Skip to content

Commit

Permalink
Small cleanup
Browse files Browse the repository at this point in the history
* Cleanup and bump dependencies
* Cleanup linter errors
* Add deps.edn
  • Loading branch information
DeLaGuardo committed Sep 22, 2023
1 parent 332f214 commit 1a110d3
Show file tree
Hide file tree
Showing 26 changed files with 365 additions and 282 deletions.
2 changes: 2 additions & 0 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:lint-as {flanders.macros/defleaf schema.core/defrecord
flanders.core/def-entity-type clojure.core/def}}
1 change: 1 addition & 0 deletions .clj-kondo/prismatic/schema/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:lint-as {schema.test/deftest clojure.test/deftest}}
4 changes: 4 additions & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((clojure-mode . ((cider-clojure-cli-aliases . ":test"))))
42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Code check

on: push

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Install Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '8'

- name: Install clojure tools
uses: DeLaGuardo/[email protected]
with:
lein: latest
cli: latest
clj-kondo: latest

- name: Check clojure code
run:
clj-kondo --lint ./src --lint ./test

- name: Cache lein project dependencies
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-clojure-${{ hashFiles('**/project.clj', '**/deps.edn') }}
restore-keys: |
${{ runner.os }}-clojure
- name: Run Clojure CLI tests
run:
clojure -X:test

- name: Run leiningen tests
run:
lein test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ pom.xml.asc
/.nrepl-port
.hgignore
.hg/
/.clj-kondo/.cache
/.lsp/.cache
/.cpcache/
11 changes: 11 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{:paths ["src"]
:deps {metosin/ring-swagger {:mvn/version "0.26.2"}
metosin/schema-tools {:mvn/version "0.13.1"}
org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/core.match {:mvn/version "1.0.0"}
org.clojure/test.check {:mvn/version "1.1.1"}
prismatic/schema {:mvn/version "1.4.1"}}
:aliases {:test {:extra-paths ["test"]
:extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}}
:main-opts ["-m" "cognitect.test-runner"]
:exec-fn cognitect.test-runner.api/test}}}
15 changes: 5 additions & 10 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
:url "http://github.com/threatgrid/flanders"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:pedantic? :abort
:dependencies [[org.clojure/clojure "1.10.1"]
:dependencies [[metosin/ring-swagger "0.26.2"]
[metosin/schema-tools "0.13.1"]
[org.clojure/clojure "1.10.1"]
[org.clojure/core.match "1.0.0"]
[cheshire "5.9.0"]

[prismatic/schema "1.1.12"]
[metosin/ring-swagger "0.26.2"]
[metosin/schema-tools "0.12.2"]]
:global-vars {*warn-on-reflection* true}
:profiles {:dev
{:dependencies [[org.clojure/test.check "1.0.0"]]}})
[org.clojure/test.check "1.1.1"]
[prismatic/schema "1.4.1"]])
15 changes: 7 additions & 8 deletions src/flanders/core.cljc
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
(ns flanders.core
(:refer-clojure :exclude [int key keyword map name num str type])
(:require #?(:clj [clojure.core :as core]
:cljs [cljs.core :as core])
#?(:clj [clojure.spec.alpha :as spec]
:cljs [cljs.spec.alpha :as spec])
[clojure.zip :as z]
[flanders.types :as ft]
[schema.core :as s]))
(:require
#?(:clj [clojure.core :as core]
:cljs [cljs.core :as core])
#?(:clj [clojure.spec.alpha :as spec]
:cljs [cljs.spec.alpha :as spec])
[flanders.types :as ft]))

;; ----------------------------------------------------------------------
;; Defining Branch Nodes
Expand Down Expand Up @@ -111,7 +110,7 @@
A signature can be variadic by specifying the `:rest-parameter`
argument."
[& {:keys [parameters rest-parameter return] :as opts}]
(if (some? parameters)
(when (some? parameters)
(assert (sequential? parameters) ":parameters argument must be `sequential?`"))
(let [return (if (some? return)
return
Expand Down
40 changes: 24 additions & 16 deletions src/flanders/example.cljc
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
(ns flanders.example
(:require [flanders.predicates :as fp]
[flanders.schema :as fs]
#?(:clj [flanders.types :as ft]
:cljs [flanders.types
:as ft
:refer [AnythingType BooleanType EitherType
InstType IntegerType KeywordType
MapEntry MapType NumberType
SequenceOfType SetOfType SignatureType
StringType]]))
#?(:clj (:import [flanders.types
AnythingType BooleanType EitherType InstType
IntegerType KeywordType MapEntry MapType
NumberType SequenceOfType SetOfType SignatureType
StringType]
[java.util Date])))
(:require
#?(:clj [flanders.types :as ft]
:cljs [flanders.types
:as ft
:refer [AnythingType BooleanType EitherType InstType IntegerType
KeywordType MapEntry MapType NumberType SequenceOfType
SetOfType SignatureType StringType]])
[flanders.schema :as fs])
#?(:clj (:import
[flanders.types
AnythingType
BooleanType
EitherType
InstType
IntegerType
KeywordType
MapEntry
MapType
NumberType
SequenceOfType
SetOfType
SignatureType
StringType]
[java.util Date])))

(defprotocol JsonExampleNode
(->example [node f]))
Expand Down
14 changes: 7 additions & 7 deletions src/flanders/macros.cljc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(ns flanders.macros
(:require [flanders.protocols
:refer [branch? node-children make-node TreeNode]]
[schema.core :as s]))
(:require
[flanders.protocols :as p]
[schema.core :as s]))

(defmacro defleaf [name argsv & sexprs]
`(s/defrecord ~name ~argsv
TreeNode
(branch? [_#] false)
(node-children [_#] nil)
(make-node [this# _#] this#)
p/TreeNode
(~'branch? [_#] false)
(~'node-children [_#] nil)
(~'make-node [this# _#] this#)
~@sexprs))
101 changes: 48 additions & 53 deletions src/flanders/markdown.cljc
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
(ns flanders.markdown
(:refer-clojure :exclude [type])
(:require [clojure.string :as str]
[clojure.zip :as z]
#?(:clj [flanders.macros :refer [defleaf]]
:cljs [flanders.macros :refer-macros [defleaf]])
[flanders.predicates :as fp]
[flanders.schema :as fs]
#?(:clj [flanders.types]
:cljs [flanders.types
:refer [AnythingType BooleanType EitherType InstType
IntegerType KeywordType MapEntry MapType
ParameterListType NumberType
SequenceOfType SetOfType SignatureType
StringType]])
[flanders.utils :as fu])
#?(:clj (:import [flanders.types
AnythingType BooleanType EitherType InstType
IntegerType KeywordType MapEntry MapType
NumberType ParameterListType SequenceOfType
SetOfType SignatureType StringType])))
(:require
#?(:clj [flanders.macros :refer [defleaf]]
:cljs [flanders.macros :refer-macros [defleaf]])
#?(:clj [flanders.types]
:cljs [flanders.types
:refer [AnythingType BooleanType EitherType InstType IntegerType
KeywordType MapEntry MapType NumberType ParameterListType
SequenceOfType SetOfType SignatureType StringType]])
[clojure.string :as str]
[clojure.zip :as z]
[flanders.predicates :as fp]
[flanders.schema :as fs]
[flanders.utils :as fu])
#?(:clj (:import
[flanders.types
AnythingType
BooleanType
EitherType
InstType
IntegerType
KeywordType
MapEntry
MapType
NumberType
ParameterListType
SequenceOfType
SetOfType
SignatureType
StringType])))

(defprotocol MarkdownNode
(->markdown-part [node depth])
Expand All @@ -27,10 +37,8 @@
(defleaf ReferenceNode [text anchor jump-anchor])

(defn ready-for-table [str]
(-> str
(str/replace #"\n" " ")
(str/replace #"\|" "\\\\|")
))
(str/replace str #"(\n|\|)" {"\n" " "
"\\|" "\\\\|"}))

(defn ->default [{:keys [default values]}]
(when (and default (> (count values) 1))
Expand All @@ -42,7 +50,7 @@
([{:keys [description] :as node} leaf?]
(when (seq description)
(str (if leaf? " * " "")
(if-some [name (get node :name)]
(when-some [name (get node :name)]
(str "*" name "* "))
description
(if leaf? "\n" "\n\n")))))
Expand Down Expand Up @@ -88,25 +96,12 @@
(str "* " type-str " Key\n")
(str "* " type-str " Value\n"))))

(defn- ->schema-str [this loc]
(let [schema (pr-str (fs/->schema-at-loc this loc))
schema (cond
(str/starts-with? schema "(enum") "(enum ...)"
(= "java.lang.String" schema) "Str"
(= "java.lang.Boolean" schema) "Bool"
:else schema)]
(str " * Plumatic Schema: "
(if (fp/sequential? loc)
(str "[" schema "]")
schema)
"\n")))

(defn- ->values [{v :values}]
(when (and v (> (count v) 1))
(str " * Allowed Values:\n"
(str/join
(->> (sort (seq v))
(map #(str " * " % "\n")))))))
(->> (sort (seq v))
(map #(str " * " % "\n")))))))

(defn- ->comment
([node]
Expand Down Expand Up @@ -160,7 +155,7 @@

(defn- ->map-summary
"Build the TOC for the given map"
[map-node map-loc]
[_ map-loc]
(let [row-vs (loop [loc (-> map-loc z/node fu/->ddl-zip)
row-m nil
entries []]
Expand Down Expand Up @@ -226,14 +221,14 @@
text)

MapEntry
(->markdown-part [{:keys [key required?] :as this} loc]
(->markdown-part [{:keys [_ required?] :as this} loc]
(str "<a id=\"" (->entry-anchor loc) "\"></a>\n"
(->entry-header this loc)
(->description this)
(if required?
"* This entry is required"
"* This entry is optional") "\n"
(when (some-> loc z/down z/rightmost z/node fp/sequence-of?)
(when (some-> loc z/down z/rightmost z/node fp/seq-of?)
"* This entry's type is sequential (allows zero or more values)\n")
(when (some-> loc z/down z/rightmost z/node fp/set-of?)
"* This entry's type is a set (allows zero or more distinct values)\n")
Expand All @@ -243,19 +238,19 @@
(->short-description [_] "Property")

SequenceOfType
(->markdown-part [{:keys [description]} loc]
(->markdown-part [_ _]
nil)
(->short-description [this]
(str (->short-description (:type this)) " List"))

SetOfType
(->markdown-part [{:keys [description]} loc]
(->markdown-part [_ _]
nil)
(->short-description [this]
(str "#{" (->short-description (:type this)) "}"))

ParameterListType
(->markdown-part [this loc]
(->markdown-part [_ _]
nil)
(->short-description [this]
(str/join ", " (map (fn [schema]
Expand All @@ -265,10 +260,10 @@
(get this :parameters))))

SignatureType
(->markdown-part [{:keys [description parameters] :as this} loc]
(str (if-some [fn-name (get this :name)]
(->markdown-part [{:keys [description] :as this} _]
(str (when-some [fn-name (get this :name)]
(str "# `" fn-name "`" "\n\n"))
(if (not (str/blank? description))
(when (not (str/blank? description))
(str "### Description"
"\n\n"
description
Expand Down Expand Up @@ -298,7 +293,7 @@
(str "(" parameter-list-str ", " rest-parameter-str ") => " return-str))))

EitherType
(->markdown-part [this loc]
(->markdown-part [this _]
(str (->description this :leaf)
(->comment this)
(->usage this :leaf)
Expand All @@ -307,15 +302,15 @@
(->short-description [_] "Either")

AnythingType
(->markdown-part [this loc]
(->markdown-part [this _]
(str (->description this :leaf)
(->comment this :leaf)
(->usage this :leaf)
(->reference this :leaf)))
(->short-description [_] "Anything")

BooleanType
(->markdown-part [this loc]
(->markdown-part [this _]
(str (->description this :leaf)
(->comment this :leaf)
(->usage this :leaf)
Expand Down Expand Up @@ -356,7 +351,7 @@
(->short-description [this] (str (:name this) "String"))

InstType
(->markdown-part [this loc]
(->markdown-part [this _]
(str (->description this :leaf)
(->comment this :leaf)
(->usage this :leaf)
Expand Down Expand Up @@ -413,7 +408,7 @@
(recur (z/next current-map-loc)
maps-to-walk
result-maps
map-counter )))))
map-counter)))))

(defn ->markdown [ddl-root]
(let [[first-map & rest-maps] (find-maps ddl-root)]
Expand Down
Loading

0 comments on commit 1a110d3

Please sign in to comment.