-
Notifications
You must be signed in to change notification settings - Fork 7
/
wrapper.clj
207 lines (176 loc) · 6.8 KB
/
wrapper.clj
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
(ns pronto.wrapper
(:require [pronto.utils :as u]
[pronto.transformations :as transform]
[pronto.reflection :as reflect])
(:import [com.google.protobuf ByteString
Descriptors$FieldDescriptor
Descriptors$EnumValueDescriptor]
[pronto ProtoMap]))
(defprotocol Wrapper
(wrap [this v])
(unwrap [this v]))
(def ^:private numeric-scalar?
(comp boolean #{Integer Integer/TYPE Long Long/TYPE Double Double/TYPE Float Float/TYPE}))
(defn- if-instrument [ctx test then else]
(if-not (:instrument? ctx)
then
`(if ~test
~then
~else)))
(defn protobuf-scalar? [^Class clazz]
(boolean (or (numeric-scalar? clazz)
(#{Boolean Boolean/TYPE
Integer/TYPE Integer
Long/TYPE Long
String ByteString} clazz))))
(def ^:private wkt->java-type
;; use string keys since these classes may not be loaded by the classloader
{"com.google.protobuf.DoubleValue" Double/TYPE
"com.google.protobuf.FloatValue" Float/TYPE
"com.google.protobuf.Int64Value" Long/TYPE
"com.google.protobuf.UInt64Value" Long/TYPE
"com.google.protobuf.StringValue" String
"com.google.protobuf.Int32Value" Integer/TYPE
"com.google.protobuf.UInt32Value" Integer/TYPE
"com.google.protobuf.BoolValue" Boolean/TYPE
"com.google.protobuf.BytesValue" ByteString})
(defmulti gen-wrapper
(fn [^Class clazz ctx]
(cond
(get (:encoders ctx) clazz) :custom
(.isEnum clazz) :enum
(= ByteString clazz) :bytes
(wkt->java-type (.getName clazz)) :well-known-type
(not (protobuf-scalar? clazz)) :message
:else :scalar)))
(defn- make-error [^Class clazz ctx v]
(u/make-type-error (:class ctx)
(.getName ^Descriptors$FieldDescriptor (:fd ctx))
clazz
v))
(defmethod gen-wrapper
:custom
[^Class clazz ctx]
(let [{:keys [from-proto to-proto]} (get (:encoders ctx) clazz)]
(reify Wrapper
(wrap [_ v]
`(~from-proto ~v))
(unwrap [_ v]
`(~to-proto ~v)))))
(defmethod gen-wrapper
:well-known-type
[^Class clazz ctx]
(let [java-type (wkt->java-type (.getName clazz))
wrapper (gen-wrapper java-type ctx)]
(reify Wrapper
(wrap [_ v]
(wrap wrapper `(.getValue ~(u/with-type-hint v clazz))))
(unwrap [_ v]
`(let [b# (~(u/static-call clazz "newBuilder"))]
(.setValue b# ~(unwrap wrapper v))
(.build b#))))))
(defmethod gen-wrapper
:enum
[^Class clazz ctx]
(let [values (reflect/enum-values clazz)
enum-value-fn (or (:enum-value-fn ctx) identity)
enum->kw (map-indexed #(vector %1 (keyword (enum-value-fn (.getName ^Descriptors$EnumValueDescriptor %2))))
values)
kw->enum (map #(vector (keyword (enum-value-fn (.getName ^Descriptors$EnumValueDescriptor %1)))
(symbol (str (.getName clazz) "/" (.getName ^Descriptors$EnumValueDescriptor %1))))
values)]
(reify Wrapper
(wrap [_ v]
`(case (.ordinal ~(u/with-type-hint v clazz))
~@(interleave
(map first enum->kw)
(map second enum->kw))
(throw (IllegalArgumentException. (str "can't wrap " ~v)))))
(unwrap [_ v]
(let [v2 (gensym 'v)]
`(let [~v2 ~v]
(if (keyword? ~v2)
(case ~v2
~@(interleave
(map first kw->enum)
(map second kw->enum))
(throw ~(u/make-type-error (:class ctx)
(u/field->kebab-case (:fd ctx))
clazz
v2)))
~(if-instrument ctx
`(instance? ~clazz ~v)
v2
`(throw ~(make-error clazz ctx v))))))))))
(defmethod gen-wrapper
:message
[^Class clazz ctx]
(let [wrapper-type (u/class->map-class-name clazz)
transient-wrapper-type (u/class->transient-class-name clazz)]
(reify Wrapper
(wrap [_ v]
`(new ~wrapper-type ~v (meta ~v)))
(unwrap [_ v]
`(cond
(identical? (class ~v) ~wrapper-type)
~(let [u (with-meta (gensym 'u) {:tag 'pronto.ProtoMap})]
`(let [~u ~v]
(pronto.RT/getProto ~u)))
(identical? (class ~v) ~clazz) ~v
;; TODO: consolidate this with first clause
(instance? ProtoMap ~v)
~(let [u (with-meta (gensym 'u) {:tag 'pronto.ProtoMap})]
`(let [~u ~v]
(pronto.RT/getProto ~u)))
(map? ~v)
;; TODO: duplicate code
~(let [u (with-meta (gensym 'u) {:tag 'pronto.ProtoMap})]
`(let [~u (transform/map->proto-map (new ~transient-wrapper-type (~(u/static-call clazz "newBuilder")) true false) ~v)]
(pronto.RT/getProto ~u)))
:else (throw ~(make-error clazz ctx v)))))))
;; TODO: Get rid of this method, collapse into regular scalar
(defmethod gen-wrapper
:bytes
[_ ctx]
;; the class must be `ByteString`
(reify Wrapper
(wrap [_ v]
v)
(unwrap [_ v]
(if-instrument ctx
`(instance? com.google.protobuf.ByteString ~v)
v
`(throw ~(make-error com.google.protobuf.ByteString ctx v))))))
(defmethod gen-wrapper
:scalar
[^Class clazz ctx]
(reify Wrapper
(wrap [_ v] v)
(unwrap [_ v]
;; TODO: clean this up...
(cond
(= String clazz)
(if-instrument ctx
`(= String (class ~v))
v
`(throw ~(make-error clazz ctx v)))
(or (= Boolean/TYPE clazz) (= Boolean clazz))
(if-instrument ctx
`(= Boolean (class ~v))
v
`(throw ~(make-error clazz ctx v)))
(numeric-scalar? clazz)
(let [vn (u/with-type-hint v Number)
boxed? (get #{Long Integer Double Float} clazz)
primitive-class (if boxed?
(condp = clazz
Integer Integer/TYPE
Long Long/TYPE
Double Double/TYPE
Float Float/TYPE)
clazz)]
(if-instrument ctx
`(instance? Number ~vn)
`(~(symbol (str "." (str primitive-class) "Value")) ~vn)
`(throw ~(make-error clazz ctx v))))
:else (throw (IllegalArgumentException. (str "don't know how to wrap " clazz)))))))