Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s/constructor/konstructor/ to avoid cljs edge case #438

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/clj/schema/experimental/generators.clj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
(composite-generator [s params]
(generators/such-that
(complement (.-pre ^schema.spec.collection.CollectionSpec s))
(generators/fmap (:constructor s) (elements-generator (:elements s) params))))
(generators/fmap (:konstructor s) (elements-generator (:elements s) params))))

schema.spec.leaf.LeafSpec
(composite-generator [s params]
Expand Down
4 changes: 2 additions & 2 deletions src/cljc/schema/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@
(if-let [evf (:extra-validator-fn this)]
(some-fn p (spec/precondition this evf #(list 'passes-extra-validation? %)))
p))
(:constructor (meta this))
(:konstructor (meta this))
(map-elements schema)
(map-error)))
(explain [this]
Expand All @@ -1027,7 +1027,7 @@
(clojure.core/defn record* [klass schema map-constructor]
#?(:clj (macros/assert! (class? klass) "Expected record class, got %s" (utils/type-of klass)))
(macros/assert! (map? schema) "Expected map, got %s" (utils/type-of schema))
(with-meta (Record. klass schema) {:constructor map-constructor}))
(with-meta (Record. klass schema) {:konstructor map-constructor}))

#?(:clj
(defmacro record
Expand Down
10 changes: 5 additions & 5 deletions src/cljc/schema/spec/collection.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@
(assert (#{::remaining ::optional} (first elt)))
(mapcat subschemas (next elt)))))

(defrecord CollectionSpec [pre constructor elements on-error]
(defrecord CollectionSpec [pre konstructor elements on-error]
spec/CoreSpec
(subschemas [this] (mapcat subschemas elements))
(checker [this params]
(let [constructor (if (:return-walked? params) constructor (fn [_] nil))
(let [konstructor (if (:return-walked? params) konstructor (fn [_] nil))
t (sequence-transformer elements params (fn [_ x] x))]
(fn [x]
(or (pre x)
Expand All @@ -80,7 +80,7 @@
res #?(:clj res :cljs @res)]
(if (or (seq remaining) (has-error? res))
(utils/error (on-error x res remaining))
(constructor res))))))))
(konstructor res))))))))


(defn collection-spec
Expand All @@ -100,7 +100,7 @@
the user must ensure that the parser enforces the desired semantics, which
should match the structure of the spec for proper generation."
[pre ;- spec/Precondition
constructor ;- (s/=> s/Any [(s/named s/Any 'checked-value)])
konstructor ;- (s/=> s/Any [(s/named s/Any 'checked-value)])
elements ;- [(s/cond-pre
;; {:schema (s/protocol Schema)
;; :parser (s/=> s/Any (s/=> s/Any s/Any) s/Any) ; takes [item-fn coll], calls item-fn on matching items, returns remaining.
Expand All @@ -109,7 +109,7 @@
;; where the last element can optionally be a [::remaining schema]
on-error ;- (=> s/Any (s/named s/Any 'value) [(s/named s/Any 'checked-element)] [(s/named s/Any 'unmatched-element)])
]
(->CollectionSpec pre constructor elements on-error))
(->CollectionSpec pre konstructor elements on-error))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
14 changes: 7 additions & 7 deletions test/cljc/schema/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@
#?(:clj (s/defrecord Nested [^Bar4 b ^LongOrString c p :- (s/protocol PProtocol)]))
(s/defrecord NestedExplicit [b :- Bar4 c :- LongOrString p :- (s/protocol PProtocol)])

(defn test-fancier-defrecord-schema [klass constructor]
(defn test-fancier-defrecord-schema [klass konstructor]
(let [bar1 (Bar. 1 "a")
bar2 (Bar2. 1 "a")]
(is (= (utils/class-schema klass)
Expand All @@ -796,12 +796,12 @@
{:b Bar4
:c LongOrString
:p (s/protocol PProtocol)}
constructor)))
(valid! klass (constructor {:b (Bar4. [1] {}) :c 1 :p bar2}))
(valid! klass (constructor {:b (Bar4. [1] {}) :c "hi" :p bar2}))
(invalid! klass (constructor {:b (Bar4. [1] {}) :c "hi" :p bar1}))
(invalid! klass (constructor {:b (Bar4. [1] {:foo :bar}) :c 1 :p bar2}))
(invalid! klass (constructor {:b nil :c "hi" :p bar2}))))
konstructor)))
(valid! klass (konstructor {:b (Bar4. [1] {}) :c 1 :p bar2}))
(valid! klass (konstructor {:b (Bar4. [1] {}) :c "hi" :p bar2}))
(invalid! klass (konstructor {:b (Bar4. [1] {}) :c "hi" :p bar1}))
(invalid! klass (konstructor {:b (Bar4. [1] {:foo :bar}) :c 1 :p bar2}))
(invalid! klass (konstructor {:b nil :c "hi" :p bar2}))))

(deftest fancier-defrecord-schema-test
#?(:clj (test-fancier-defrecord-schema Nested map->Nested))
Expand Down