Compare commits

..

6 commits

Author SHA1 Message Date
Ingy döt Net
47e55fe5e7
Fix typo (#71) 2024-05-08 14:17:43 +02:00
Michiel Borkent
717cef7af5 ignore 2024-02-27 11:52:37 +01:00
Jo Geraerts
cd968459a7
correct small typo in README.md (#69) 2024-01-26 13:26:09 +01:00
Flávio Sousa
8b717eb001
Update README.md with BABASHKA_PODS_DIR (#68) 2023-06-05 11:13:35 +02:00
Michiel Borkent
6ad6045b94 minor 2023-05-12 16:42:25 +02:00
Jude Payne
b00133ca05
Fix #66: opt-in metadata (#67) 2023-05-12 16:09:40 +02:00
6 changed files with 32 additions and 20 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@
.cache
.clj-kondo/babashka
.clj-kondo/rewrite-clj
src/scratch.clj

View file

@ -77,7 +77,7 @@ On the JVM:
When calling `load-pod` with a string or vector of strings (or declaring it in your `bb.edn`),
the pod is looked up on the local file system (either using the PATH, or using an absolute path).
When it is called with a qualified symbol and a version - like `(load-pod 'org.babashka/aws "0.0.5")`
then it will be looked up in and downloaded from the [pod-registry](https://github.com/babashka/pod-registry).
then it will be looked up in and downloaded from the [pod-registry](https://github.com/babashka/pod-registry). You can customize the file system location that `load-pod` will use by setting the `BABASHKA_PODS_DIR` environment variable.
By default babashka will search for a pod binary matching your system's OS and arch. If you want to download
pods for a different OS / arch (e.g. for deployment to servers), you can set one or both of the following
@ -130,7 +130,7 @@ light weight replacement for native interop (JNI, JNA, etc.).
### Examples
Beyond the already available pods mentioned above, eductional examples of pods
Beyond the already available pods mentioned above, educational examples of pods
can be found [here](examples):
- [pod-lispyclouds-sqlite](examples/pod-lispyclouds-sqlite): a pod that
@ -228,7 +228,7 @@ JSON. It also declares that the pod exposes one namespace,
To encode payloads in EDN use `"edn"` and for Transit JSON use `"transit+json"`.
The pod encodes the above map to bencode and writes it to stdoud. The pod client
The pod encodes the above map to bencode and writes it to stdout. The pod client
reads this message from the pod's stdout.
Upon receiving this message, the pod client creates these namespaces and vars.
@ -414,13 +414,13 @@ to the optional map passed to `transit/writer`. e.g.:
Currently sending metadata on arguments passed to a pod function is available only for the
`transit+json` format and can be enabled on a per var basis.
A pod can enable metadata to be read on arguments by sending the "read-meta?" field to "true"
A pod can enable metadata to be read on arguments by sending the "arg-meta" field to "true"
for the var representing that function. For example:
````clojure
{:format :transit+json
:namespaces [{:name "pod.babashka.demo"
:vars [{"name" "round-trip" "read-meta?" "true"}]}]}
:vars [{"name" "round-trip" "arg-meta" "true"}]}]}
````
#### Deferred namespace loading

View file

@ -29,7 +29,7 @@
(String. bytes))
(defn bytes->boolean [^"[B" bytes]
(boolean bytes))
(= "true" (String. bytes)))
(defn get-string [m k]
(-> (get m k)
@ -90,14 +90,12 @@
(let [wh (transit/write-handler tag-fn val-fn)]
(swap! transit-default-write-handlers assoc *pod-id* wh)))
(defonce vars-with-metadata (atom {}))
(defn transit-json-write
[pod-id ^String s metadata?]
(with-open [baos (java.io.ByteArrayOutputStream. 4096)]
(let [w (transit/writer baos :json (merge {:handlers (get @transit-write-handler-maps pod-id)
:default-handler (get @transit-default-write-handlers pod-id)}
(when metadata? {:transform transit/write-meta})))]
(let [w (transit/writer baos :json (cond-> {:handlers (get @transit-write-handler-maps pod-id)
:default-handler (get @transit-default-write-handlers pod-id)}
metadata? (assoc :transform transit/write-meta)))]
(transit/write w s)
(str baos))))
@ -108,10 +106,8 @@
chans (:chans pod)
write-fn (case format
:edn pr-str
:json cheshire/generate-string
:transit+json #(transit-json-write
(:pod-id pod) %
(contains? (get @vars-with-metadata (:pod-id pod)) pod-var)))
:json cheshire/generate-string
:transit+json #(transit-json-write (:pod-id pod) % (:arg-meta opts)))
id (next-id)
chan (if handlers handlers
(promise))
@ -142,13 +138,11 @@
name-sym (if vmeta
(with-meta name-sym vmeta)
name-sym)
metadata? (get-maybe-boolean var "read-meta?")]
(when metadata?
(swap! vars-with-metadata update (:pod-id pod) #(conj (set %) sym)))
metadata? (get-maybe-boolean var "arg-meta")]
[name-sym
(or code
(fn [& args]
(let [res (invoke pod sym args {:async async?})]
(let [res (invoke pod sym args {:async async? :arg-meta metadata?})]
res)))]))
vars))

View file

@ -137,7 +137,9 @@
"code" "(defn read-other-tag [x] [x x])"
"meta" "{:doc \"unread\"}"}
{"name" "round-trip-meta"
"read-meta?" "true"}
"arg-meta" "true"}
{"name" "dont-round-trip-meta"
"arg-meta" "false"}
{"name" "-local-date-time"}
{"name" "transit-stuff"
"code" "
@ -244,6 +246,14 @@
"id" id
"value" "#my/other-tag[1]"})
pod.test-pod/round-trip-meta
(write out
{"status" ["done"]
"id" id
"value"
(case format
:transit+json (transit-json-write-meta (first args))
(write-fn (first args)))})
pod.test-pod/dont-round-trip-meta
(write out
{"status" ["done"]
"id" id

View file

@ -94,6 +94,11 @@
(= {:my-meta 3} (meta (first (pod.test-pod/round-trip-meta [(with-meta [3] {:my-meta 3})]))))
true))
(def dont-round-trip-meta
(if (= "transit+json" fmt)
(= nil (meta (pod.test-pod/dont-round-trip-meta (with-meta [2] {:my-meta 2}))))
true))
(require '[pod.test-pod.only-code :as only-code])
(def should-be-1 (only-code/foo))
@ -128,6 +133,7 @@
assoc-string-array
round-trip-meta
round-trip-meta-nested
dont-round-trip-meta
should-be-1
add-sync-meta
error-meta

View file

@ -36,6 +36,7 @@
true ;; roundtrip string array
true ;; roundtrip metadata
true ;; roundtrip metadata nested
true ;; dont roundtrip metadata (when arg-meta "false"/ absent)
1
"add the arguments"
nil