Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit e5353b7

Browse files
author
Jim Clark
committed
Add example for static dispatch
1 parent 5df70f8 commit e5353b7

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,48 @@ Create `vonwig/pod-atomisthq-tools.docker` which is a manifest list with pod bin
4747
bb build-pod-image
4848
```
4949

50+
## Namespace generation
51+
52+
The `pods/load-pod` call is convenient for a repl-session, or a script, but what if you are `aot` compiling, or building a native binary. In the example above, the namespaces emitted by `pods/load-pod` are not available until runtime.
53+
54+
Here is an example of bindings that will resolve at compile-time and go through the same dispatch.
55+
56+
```
57+
; require the babashka.pods in a namespace
58+
(require '[babashka.pods.impl :as impl])
59+
60+
; call at runtime to initialize pod system
61+
(defn load-pod
62+
([pod-spec] (load-pod pod-spec nil))
63+
([pod-spec version opts] (load-pod pod-spec (assoc opts :version version)))
64+
([pod-spec opts]
65+
(let [opts (if (string? opts)
66+
{:version opts}
67+
opts)
68+
pod (impl/load-pod
69+
pod-spec
70+
(merge {:remove-ns remove-ns
71+
:resolve (fn [sym]
72+
(or (resolve sym)
73+
(intern
74+
(create-ns (symbol (namespace sym)))
75+
(symbol (name sym)))))}
76+
opts))]
77+
(future (impl/processor pod))
78+
{:pod/id (:pod-id pod)})))
79+
80+
;; statically define dispatch functions
81+
(defn parse [s]
82+
(impl/invoke-public "pod.atomisthq.docker" "pod.atomisthq.docker/parse-dockerfile" [s] {}))
83+
```
84+
85+
```
86+
(pods/load-pod 'atomisthq/tools.docker "7.3.0")
87+
(pods/load-pod "my-executable")
88+
```
89+
90+
This method of dispatch does not require any dynamic namespace generation.
91+
5092
## Contributing
5193

5294
You can find information about contributing to this project in the CONTRIBUTING.md

0 commit comments

Comments
 (0)