Skip to content

Commit

Permalink
core: clojobuf.core/generate does not generate :? #6
Browse files Browse the repository at this point in the history
  • Loading branch information
st-cheewah committed Jan 16, 2025
1 parent ef0e94d commit 97a3ae8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/clojobuf/core.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns clojobuf.core
(:require [clojobuf.encode :refer [encode-msg]]
[clojobuf.decode :refer [decode-msg]]
[clojobuf.schema :refer [xform-ast vschemas-pb-types vschemas-update-msg-field-presence vschemas-make-defaults]]
[clojobuf.schema :refer [xform-ast vschemas-pb-types vschemas-update-msg-field-presence vschemas-update-generator-fmap vschemas-make-defaults]]
[clojobuf.util :as util]
[malli.core :as m]
[malli.error :as me]
Expand Down Expand Up @@ -49,18 +49,20 @@
(merge (default-msg registry msg-id)
(mg/generate [:ref (util/dot-qualify msg-id)] (second registry))))

(defn ->complete-malli-schema
(defn- ->complete-malli-schema
"Add value schema to a composite registry. Return the new registry."
[schema] (into vschemas-pb-types schema))


(defn ->malli-registry
"Use `schema`, which is expected to be (second (protoc ... :malli-composite-registry false)), to build
a malli registry and returns it."
[schema]
{:registry (mr/composite-registry
m/default-registry
(->complete-malli-schema schema))
:defaults (vschemas-make-defaults schema)})
(let [schema (vschemas-update-generator-fmap schema)]
{:registry (mr/composite-registry
m/default-registry
(->complete-malli-schema schema))
:defaults (vschemas-make-defaults schema)}))

; protoc needs file access which is not available to cljs browser runtime
; * for cljs browser runtime, use clojobuf.macro/protoc-macro
Expand Down
16 changes: 16 additions & 0 deletions src/clojobuf/schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@
update-implicit-property
vschemas)))

(defn vschemas-update-generator-fmap
"See vschemas-update-msg-field-presence for example input.
Changes {... {:close true} ...} to {... {:closed true, :gen/fmap #(dissoc % :?)} ...}"
[vschemas]
(let [xform (fn [form] (assoc form :gen/fmap #(dissoc % :?)))]
(sp/transform [sp/ALL-WITH-META
(sp/nthpath 1)
; visit all elements of message (:map) type
(sp/cond-path #(= :map (first %)) (sp/nthpath 1)
#(and (= :and (-> % first)) ; if message contains oneof, top level is [:and [:map ...] ...]
(= :map (-> % second first))) (sp/nthpath 1 1))
; only visit property map
(sp/if-path map? sp/STAY)]
xform
vschemas)))

(defn vschemas-make-defaults
"Make default values from validation schema."
[vschemas]
Expand Down
3 changes: 1 addition & 2 deletions test/clojobuf/generate_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@
(is (= (codec msg-id msg) msg))))

(deftest test-generate
(let [val (generate registry :my.ns.simple/Simple)
val (dissoc val :?)]
(let [val (generate registry :my.ns.simple/Simple)]
(rt :my.ns.simple/Simple val)))

0 comments on commit 97a3ae8

Please sign in to comment.