Skip to content

Commit

Permalink
[pattern] optimise matching
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds committed Sep 30, 2024
1 parent 7f8c53f commit 4fd5987
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ deps.edn used for this example:
- Clojure [tools.deps](https://clojure.org/guides/getting_started)

### Running tests locally
- `clojure -M:test` to run all tests
- `clojure -X:test` to run all tests

## License

Expand Down
9 changes: 1 addition & 8 deletions src/navi/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@
.getSchema
schema->spec))))

(defn matches-regex?
[exp s]
(-> exp
(re-pattern)
(re-matches s)
(some?)))

(defmulti spec
(fn [^Schema schema]
(or (first (.getTypes schema)) "null")))
Expand All @@ -104,7 +97,7 @@
string?)
pattern (.getPattern schema)]
(if pattern
[:and content-fn [:fn #(matches-regex? pattern %)]]
[:and content-fn (re-pattern pattern)]
content-fn)))

(defmethod spec
Expand Down
16 changes: 8 additions & 8 deletions test/navi/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@
(is (#{[:or string? int?] [:or int? string?]}
(core/schema->spec strint)))))
(testing "regex string"
(let [[kw1 fn1 [kw2 fn2]] (core/schema->spec (doto (Schema.)
(.addType "string")
(.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))]
(is (= :and kw1))
(is (= string? fn1))
(is (= :fn kw2))
(is (fn2 "1024Ki"))
(is (not (fn2 "1024Kib"))))))
(let [[kw f regex] (core/schema->spec (doto (Schema.)
(.addType "string")
(.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))]
(is (= :and kw))
(is (= string? f))
(is (instance? java.util.regex.Pattern regex))
(is (some? (re-matches regex "1024Ki")))
(is (nil? (re-matches regex "1024Kib"))))))

(deftest responses-to-malli-spec
(testing "empty response"
Expand Down

0 comments on commit 4fd5987

Please sign in to comment.