-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex3.cljc
45 lines (37 loc) · 1.68 KB
/
ex3.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns clojobuf.example.ex3
(:require [clojobuf.core :refer [encode decode find-fault ->malli-registry]]
[clojobuf.macro :refer [protoc-macro]]))
; use protoc-macro so that schemas is generated at compile time, which is especially useful for cljs which cannot
; use protoc at runtime as js doesn't have file system access.
(def registry (let [[codec malli] (protoc-macro ["resources/protobuf/"] ["example.proto"])]
[codec (->malli-registry malli)]))
;-------------------------------------------------------------------
; Code below are identical to ex1.cljc
;-------------------------------------------------------------------
; message to be encoded
(def msg {:int32_val -1,
:string_val "abc",
:bool_val false,
:enum_val :ZERO,
:either :sint32_val,
:sint32_val -1
:int64_string {1 "abc", 2 "def"}
:double_vals [0.0, 1.0, 2.0]})
; message to be encoded
(def msg2 {:msg1 msg, :msg1s [msg, msg]})
;-------------------------------------------------------------------
; Success case
;-------------------------------------------------------------------
(def binary (encode registry :my.pb.ns/Msg msg))
(decode registry :my.pb.ns/Msg binary)
; => msg
(def binary2 (encode registry :my.pb.ns/Msg2 msg2))
(decode registry :my.pb.ns/Msg2 binary2)
; => msg2
;-------------------------------------------------------------------
; Error case
;-------------------------------------------------------------------
(let [bin (encode registry :my.pb.ns/Msg {:this-field-doesnt-exists 0})]
(when (nil? bin)
(find-fault registry :my.pb.ns/Msg {:this-field-doesnt-exists 0})))
; => {:this-field-doesnt-exists ["disallowed key"]}