11# Stencil Clojure Client
22
3- A Clojure library designed to easily encode and decode protobuf messages by using Clojure maps.
3+ A Clojure library designed to easily encode and decode protobuf messages by using Clojure maps.
44
55## Installation
66
77Add the below dependency to your ` project.clj ` file:
8+
89``` clj
9- [io.odpf/stencil-clj " 0.2.2 " ]
10+ [io.odpf/stencil-clj " 0.3.0 " ]
1011```
1112
1213## Usage
1314
1415Consider following proto message
16+
1517``` proto
1618syntax = "proto3";
1719
@@ -39,6 +41,7 @@ message Person {
3941 int32 age = 5;
4042}
4143```
44+
42451 . Create stencil client. You can refer to [ java client] ( ../java ) documentation for all available options.
4346
4447``` clojure
@@ -52,6 +55,7 @@ message Person {
5255```
5356
54572. To serialize data from clojure map
58+
5559```clojure
5660(:require [stencil.core :refer [serialize]])
5761
@@ -64,6 +68,7 @@ message Person {
6468```
6569
66703. Deserialize data from bytes to clojure map
71+
6772```clojure
6873(:require [stencil.core :refer [deserialize]])
6974
@@ -78,40 +83,44 @@ message Person {
7883
7984## Protocol buffers - Clojure interop
8085
81- | Protobuf | Clojure | Notes |
82- | ----------- | --------------------------- | ---------------- |
83- | field names | keywords in kebab case | `name` -> `:name`, `field_name` -> `:field-name` |
84- | scalar fields | Values follow [protobuf-java scalar value mappings](https://developers.google.com/protocol-buffers/docs/proto3#scalar ) | |
85- | enums | Values converted as keywords of enum's original value | `UNKNOWN` -> `:UNKNOWN` |
86- | messages | clojure map | ``` message Hello {string name = 1 ; }``` -> {:name "odpf"} |
87- | repeated fields | clojure vector | |
88- | one-of fields | treated as regular fields | if two fields are set that are part of one-of, last seen value is considered while serializing data |
89- | map | map values follow it's [wire representation](https://developers.google.com/protocol-buffers/docs/proto3#backwards_compatibility ) | for `map<string, string>` type, example value will be `[{:key " key" :value " value" }]` |
86+ | Protobuf | Clojure | Notes |
87+ | --------------- | -------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- ---------------- |
88+ | field names | keywords in kebab case | `name` -> `:name`, `field_name` -> `:field-name` |
89+ | scalar fields | Values follow [protobuf-java scalar value mappings](https://developers.google.com/protocol-buffers/docs/proto3#scalar ) | |
90+ | enums | Values converted as keywords of enum's original value | `UNKNOWN` -> `:UNKNOWN` |
91+ | messages | clojure map | `message Hello {string name = 1 ; }` -> {:name "odpf"} |
92+ | repeated fields | clojure vector | |
93+ | one-of fields | treated as regular fields | if two fields are set that are part of one-of, last seen value is considered while serializing data |
94+ | map | map values follow it's [wire representation](https://developers.google.com/protocol-buffers/docs/proto3#backwards_compatibility ) | for `map<string, string>` type, example value will be `[{:key " key" :value " value" }]` |
9095
9196**Note on errors:**
9297Serialize will throw error in following cases
98+
93991. unknown field is passed that's not present in schema `{:cause :unknown-field :info {:field-name <field-name>}}`
941002. if non-collection type is passed to repeated field `{:cause :not-a-collection :info {:value <value>}}`
951013. If unknown enum value passed that's not present in schema `{:cause :unknown-enum-value :info {:field-name <field-name>}}`
96102
97103## API
104+
98105- `create-client (client-config )`
99106
100107 Returns a new Stencil Clojure client instance by passing client-config.
101108
102109 ### Client config structure :
103- | Key | Type | Description |
104- | -----------------------|-----------|---------------------------------------------------------------------------------------------|
105- | `url` | _String_ | Stencil url to fetch latest descriptor sets |
106- | `refresh-cache` | _Boolean_ | Whether the cache should be refreshed or not |
107- | `refresh-ttl` | _Integer_ | Cache TTL in minutes |
108- | `request-timeout` | _Integer_ | Request timeout in milliseconds |
109- | `request-backoff-time` | _Integer_ | Request back off time in minutes |
110- | `retry-count` | _Integer_ | Number of retries to be made to fetch descriptor sets |
111- | `headers` | _Map_ | Map with key as header key and value as header value, which will be passed to stencil server|
112- | `refresh-strategy` | _keyword_ | Possible values :version-based-refresh , :long-polling-refresh. Default :long-polling-refresh|
110+
111+ | Key | Type | Description |
112+ | ---------------------- | --------- | -------------------------------------------------------------------------------------------- |
113+ | `url` | _String_ | Stencil url to fetch latest descriptor sets |
114+ | `refresh-cache` | _Boolean_ | Whether the cache should be refreshed or not |
115+ | `refresh-ttl` | _Integer_ | Cache TTL in minutes |
116+ | `request-timeout` | _Integer_ | Request timeout in milliseconds |
117+ | `request-backoff-time` | _Integer_ | Request back off time in minutes |
118+ | `retry-count` | _Integer_ | Number of retries to be made to fetch descriptor sets |
119+ | `headers` | _Map_ | Map with key as header key and value as header value, which will be passed to stencil server |
120+ | `refresh-strategy` | _keyword_ | Possible values :version-based-refresh , :long-polling-refresh. Default :long-polling-refresh |
113121
114122 Example:
123+
115124 ```clojure
116125 (let [sample-client-config {:url " https://example-url"
117126 :refresh-cache true
@@ -130,17 +139,20 @@ Serialize will throw error in following cases
130139 Returns protobuf descriptor object for the given protobuf class name.
131140
132141 ### Argument list :
133- | Key | Type | Description |
134- | ------------------------------------------------|-------------------|-----------------------------------------------------------------------------|
135- | `client` | _Object_ | Instantiated Clojure client object |
136- | `proto-class-name` | _String_ | Name of the proto class whose proto descriptor object is required |
142+
143+ | Key | Type | Description |
144+ | ------------------ | -------- | ----------------------------------------------------------------- |
145+ | `client` | _Object_ | Instantiated Clojure client object |
146+ | `proto-class-name` | _String_ | Name of the proto class whose proto descriptor object is required |
137147
138148 ### Response structure
139- | Value | Type | Description |
140- |-------------------------------------------------|-------------------|-----------------------------------------------------------------------------|
141- | **proto-desc** | _Object_ | Protobuf descriptor for given proto class name |
149+
150+ | Value | Type | Description |
151+ | -------------- | -------- | ---------------------------------------------- |
152+ | **proto-desc** | _Object_ | Protobuf descriptor for given proto class name |
142153
143154 Example:
155+
144156 ```clojure
145157 (let [client (create-client sample-client-config)
146158 proto-package " io.odpf.stencil_clj_test"
@@ -154,18 +166,21 @@ Serialize will throw error in following cases
154166 Returns Clojure map for the given protobuf encoded byte array and protobuf class name.
155167
156168 ### Argument list :
157- | Key | Type | Description |
158- | ------------------------------------------------|---------------------|-----------------------------------------------------------------------------|
159- | `client` | _Object_ | Instantiated Clojure client object |
160- | `proto-class-name` | _String_ | Name of the proto class whose proto descriptor object is required |
161- | `data` | _Byte-Array_ | Data (byte-array ) to be deserialized using proto-descriptor object |
169+
170+ | Key | Type | Description |
171+ | ------------------ | ------------ | ------------------------------------------------------------------ |
172+ | `client` | _Object_ | Instantiated Clojure client object |
173+ | `proto-class-name` | _String_ | Name of the proto class whose proto descriptor object is required |
174+ | `data` | _Byte-Array_ | Data (byte-array ) to be deserialized using proto-descriptor object |
162175
163176 ### Response structure
164- | Value | Type | Description |
165- |-------------------------------------------------|---------------------|-----------------------------------------------------------------------------|
166- | **deserialized-message** | _PersistentArrayMap_| Deserialized message (Clojure Map) |
177+
178+ | Value | Type | Description |
179+ | ------------------------ | -------------------- | ---------------------------------- |
180+ | **deserialized-message** | _PersistentArrayMap_ | Deserialized message (Clojure Map) |
167181
168182 Example:
183+
169184 ```clojure
170185 (let [client (create-client sample-client-config)
171186 proto-package " io.odpf.stencil_clj_test"
@@ -181,18 +196,21 @@ Serialize will throw error in following cases
181196 Returns protobuf encoded byte array for the given Clojure and protobuf class name.
182197
183198 ### Argument list :
184- | Key | Type | Description |
185- | ------------------------------------------------|----------------------|-----------------------------------------------------------------------------|
186- | `client` | _Object_ | Instantiated Clojure client object |
187- | `proto-class-name` | _String_ | Name of the proto class whose proto descriptor object is required |
188- | `map` | _PersistentArrayMap_ | Data (in the form of map) to be serialized using proto descriptor object |
199+
200+ | Key | Type | Description |
201+ | ------------------ | -------------------- | ------------------------------------------------------------------------ |
202+ | `client` | _Object_ | Instantiated Clojure client object |
203+ | `proto-class-name` | _String_ | Name of the proto class whose proto descriptor object is required |
204+ | `map` | _PersistentArrayMap_ | Data (in the form of map) to be serialized using proto descriptor object |
189205
190206 ### Response structure
191- | Value | Type | Description |
192- |-------------------------------------------------|---------------------|------------------------------------------------------------------------------|
193- | **serialized-message** | _Byte-Array_ | Serialized message (byte-array ) |
207+
208+ | Value | Type | Description |
209+ | ---------------------- | ------------ | ------------------------------- |
210+ | **serialized-message** | _Byte-Array_ | Serialized message (byte-array ) |
194211
195212 Example:
213+
196214 ```clojure
197215 (let [client (create-client sample-client-config)
198216 proto-package " io.odpf.stencil_clj_test"
@@ -201,13 +219,14 @@ Serialize will throw error in following cases
201219 proto-desc (get-descriptor client fully-qualified-proto-name)]
202220 (serialize client fully-qualified-proto-name {:field-one 1.25 }))
203221 ```
222+
204223## Development
205- - Ensure [leiningen](https://leiningen.org/ ) is installed.
206224
207- - Run tests: ```lein clean && lein javac && lein test```
225+ - Ensure [leiningen]( https://leiningen.org/ ) is installed.
208226
209- - Run formatting : ``` lein cljfmt fix`` `
227+ - Run tests : `lein clean && lein javac && lein test `
210228
229+ - Run formatting: `lein cljfmt fix`
211230
212231## License
213232
0 commit comments