1
1
# Stencil Clojure Client
2
2
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.
4
4
5
5
## Installation
6
6
7
7
Add the below dependency to your ` project.clj ` file:
8
+
8
9
``` clj
9
- [io.odpf/stencil-clj " 0.2.2 " ]
10
+ [io.odpf/stencil-clj " 0.3.0 " ]
10
11
```
11
12
12
13
## Usage
13
14
14
15
Consider following proto message
16
+
15
17
``` proto
16
18
syntax = "proto3";
17
19
@@ -39,6 +41,7 @@ message Person {
39
41
int32 age = 5;
40
42
}
41
43
```
44
+
42
45
1 . Create stencil client. You can refer to [ java client] ( ../java ) documentation for all available options.
43
46
44
47
``` clojure
@@ -52,6 +55,7 @@ message Person {
52
55
```
53
56
54
57
2. To serialize data from clojure map
58
+
55
59
```clojure
56
60
(:require [stencil.core :refer [serialize]])
57
61
@@ -64,6 +68,7 @@ message Person {
64
68
```
65
69
66
70
3. Deserialize data from bytes to clojure map
71
+
67
72
```clojure
68
73
(:require [stencil.core :refer [deserialize]])
69
74
@@ -78,40 +83,44 @@ message Person {
78
83
79
84
## Protocol buffers - Clojure interop
80
85
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" }]` |
90
95
91
96
**Note on errors:**
92
97
Serialize will throw error in following cases
98
+
93
99
1. unknown field is passed that's not present in schema `{:cause :unknown-field :info {:field-name <field-name>}}`
94
100
2. if non-collection type is passed to repeated field `{:cause :not-a-collection :info {:value <value>}}`
95
101
3. If unknown enum value passed that's not present in schema `{:cause :unknown-enum-value :info {:field-name <field-name>}}`
96
102
97
103
## API
104
+
98
105
- `create-client (client-config )`
99
106
100
107
Returns a new Stencil Clojure client instance by passing client-config.
101
108
102
109
### 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 |
113
121
114
122
Example:
123
+
115
124
```clojure
116
125
(let [sample-client-config {:url " https://example-url"
117
126
:refresh-cache true
@@ -130,17 +139,20 @@ Serialize will throw error in following cases
130
139
Returns protobuf descriptor object for the given protobuf class name.
131
140
132
141
### 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 |
137
147
138
148
### 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 |
142
153
143
154
Example:
155
+
144
156
```clojure
145
157
(let [client (create-client sample-client-config)
146
158
proto-package " io.odpf.stencil_clj_test"
@@ -154,18 +166,21 @@ Serialize will throw error in following cases
154
166
Returns Clojure map for the given protobuf encoded byte array and protobuf class name.
155
167
156
168
### 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 |
162
175
163
176
### 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) |
167
181
168
182
Example:
183
+
169
184
```clojure
170
185
(let [client (create-client sample-client-config)
171
186
proto-package " io.odpf.stencil_clj_test"
@@ -181,18 +196,21 @@ Serialize will throw error in following cases
181
196
Returns protobuf encoded byte array for the given Clojure and protobuf class name.
182
197
183
198
### 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 |
189
205
190
206
### 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 ) |
194
211
195
212
Example:
213
+
196
214
```clojure
197
215
(let [client (create-client sample-client-config)
198
216
proto-package " io.odpf.stencil_clj_test"
@@ -201,13 +219,14 @@ Serialize will throw error in following cases
201
219
proto-desc (get-descriptor client fully-qualified-proto-name)]
202
220
(serialize client fully-qualified-proto-name {:field-one 1.25 }))
203
221
```
222
+
204
223
## Development
205
- - Ensure [leiningen](https://leiningen.org/ ) is installed.
206
224
207
- - Run tests: ```lein clean && lein javac && lein test```
225
+ - Ensure [leiningen]( https://leiningen.org/ ) is installed.
208
226
209
- - Run formatting : ``` lein cljfmt fix`` `
227
+ - Run tests : `lein clean && lein javac && lein test `
210
228
229
+ - Run formatting: `lein cljfmt fix`
211
230
212
231
## License
213
232
0 commit comments