-
Notifications
You must be signed in to change notification settings - Fork 7
/
classes.lisp
401 lines (307 loc) · 15.1 KB
/
classes.lisp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
;;; -*- Mode: lisp; Syntax: ansi-common-lisp; Base: 10; Package: org.apache.thrift.implementation; -*-
(in-package :org.apache.thrift.implementation)
;;; This file defines the abstract and metaclass definitions for the `org.apache.thrift` library.
;;;
;;; copyright 2010 [james anderson]([email protected])
;;;
;;; Licensed to the Apache Software Foundation (ASF) under one
;;; or more contributor license agreements. See the NOTICE file
;;; distributed with this work for additional information
;;; regarding copyright ownership. The ASF licenses this file
;;; to you under the Apache License, Version 2.0 (the
;;; "License"); you may not use this file except in compliance
;;; with the License. You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing,
;;; software distributed under the License is distributed on an
;;; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
;;; KIND, either express or implied. See the License for the
;;; specific language governing permissions and limitations
;;; under the License.
;;; The thrift-class metaclass manages struct field definitions, which are used to generate
;;; structure encoding/decoding aspects of request and response operators. The classes
;;; are associated with an external identifier in a mechanism which parallels find-class,
;;; and each field slot definition includes its name and number identifiers.
;;;
;;; The abstract metaclass is specialized as thrift-struct-class and thrift-exception-class
;;; to allow for different instantiation protocols for standard objects and conditions.
(defclass thrift-class (standard-class)
((identifier
:reader class-identifier
:type string
:documentation "The external name used to encode/decode an instance as a struct."))
(:documentation "The thrift-class metaclass records its external identifier and
uses extended slot definitions to record thrift field definitions. It is specialized as
thrift-struct-class and thrift-exception-class.
The initialization protocol includes a step to bind/rebind class identifiers in the global
*thrift-classes* for use with the find-thrift-class operator."))
(defclass thrift-struct-class (thrift-class)
()
(:documentation "Each struct declaration creates a thrift-struct-class, which is used directly
to instantiate structs."))
(defclass thrift-exception-class (thrift-class)
((condition-class
:reader class-condition-class
:documentation "The respective standard condition class of which to make condition,"))
(:documentation "Each exception declaration yields a thrift-exception-class definition _and_
a standard exception class. The former sesrves to model the extended slot descrition, and the
latter to make conditions."))
(defclass thrift-object ()
()
(:documentation "The abstract root class of all struct instances."))
(defclass field-definition ()
((identifier
:initarg :identifier :initarg :identifier-name
:reader field-definition-identifier)
(identifier-number
:initarg :identifier-number
:reader field-definition-identifier-number)
(optional
:initarg :optional :initform nil
:reader field-definition-optional
:documentation "To be used to suppress unbound slots when serializing.
NYI, as the IDL translator does not provide the data.")))
(defclass direct-field-definition (field-definition c2mop:standard-direct-slot-definition)
((identifier-number :initform (error "identifier-number is required."))
(identifier :initform (error "identifier is required."))))
(defclass effective-field-definition (field-definition c2mop:standard-effective-slot-definition)
((reader
:reader field-definition-reader)))
;;; the specialized generic function classes
;;; now serve just to document the relation between the external identifier and the function
;;; all information is compiled statically into the request/response function definitions.
(defclass thrift-generic-function (standard-generic-function)
((identifier
:initarg :identifier
:reader generic-function-identifier))
(:metaclass c2mop:funcallable-standard-class)
(:documentation "The abstract mixin for thrift interface operators which binds the external name."))
(defclass thrift-request-function (thrift-generic-function)
()
(:metaclass c2mop:funcallable-standard-class)
(:documentation "The class of thrift request operators. Each acts as a proxy for an external
operator, encodes and manages the request/response exchange and returns the result value or signals
an exception - as per the response message."))
(defclass thrift-response-function (thrift-generic-function)
((implementation-function
:initarg :implementation-function
:reader generic-function-implementation-function))
(:metaclass c2mop:funcallable-standard-class)
(:documentation "The class of thrift response operators. Each wraps the invocation of a base
implemntation operator with a mechanism to decode the arguments for application, to encode
the results as a 'reply' response message, and to catch exceptions and encode them as an
'exception' response message."))
;;;
;;; thrift-class operators
(defmethod c2mop:validate-superclass ((c1 standard-class) (c2 thrift-class))
t)
(defmethod c2mop:validate-superclass ((c2 thrift-class) (c1 standard-class))
t)
(defmethod thrift:type-of ((value thrift-object))
'struct)
(defmethod make-instance ((class thrift-exception-class) &rest initargs)
(declare (dynamic-extent initargs))
(apply #'make-condition (class-condition-class class) initargs))
(defgeneric find-thrift-class (name &optional errorp)
(:documentation "Return a class registered by identifier name. If none is registered, if
signal an error if errorp is true. Otherwise return nil.")
(:method ((identifier string) &optional (errorp t))
(warn "FIX ME: transform the identifier into a global name: ~s." identifier)
(find-thrift-class (str-sym identifier) errorp))
(:method ((name symbol) &optional (errorp t))
"Lookup first in the thrift-specific registry in order to se exception shadows.
Otherwise fall back to standard class bindings."
(cond ((gethash name *thrift-classes*))
((find-class name nil))
(errorp
(error "thrift-class not found: ~s" name)))))
(defgeneric (setf find-thrift-class) (class name)
(:documentation "Register a classe according to identifier string. Given nil, delete the entry.")
(:method ((object t) (identifier string))
(warn "FIX ME: transform the identifier into a global name: ~s." identifier)
(setf (find-thrift-class (str-sym identifier)) object))
(:method ((class thrift-class) (name symbol))
(setf (gethash name *thrift-classes*) class))
(:method ((class null) (name symbol))
(remhash name *thrift-classes*)))
(defmethod initialize-instance :after ((class thrift-class) &key (identifier (class-name class)))
(initialize-class-identifier class identifier))
(defmethod reinitialize-instance :after ((class thrift-class) &key identifier )
(when identifier
(initialize-class-identifier class identifier)))
(defmethod initialize-instance :after ((class thrift-exception-class) &key condition-class)
(initialize-class-condition-class class condition-class))
(defmethod reinitialize-instance :after ((class thrift-exception-class) &key condition-class )
(when condition-class
(initialize-class-condition-class class condition-class)))
(defun initialize-class-identifier (class identifier)
(loop (etypecase identifier
;; initialize instance asserts a value, reinitial does not
(null (return))
((or string symbol)
(setf (slot-value class 'identifier) (string identifier))
(return))
(cons
(setf identifier (first identifier))))))
(defun initialize-class-condition-class (class condition-class)
(loop (etypecase condition-class
;; initialize instance asserts a value, reinitial does not
(null (error "A condition-class is required."))
(symbol
(setf (slot-value class 'condition-class) condition-class)
(return))
(cons
(setf condition-class (first condition-class))))))
(defgeneric class-identifier (class)
(:documentation "Return the external name for the given class. Given a designator (the class name
of an instance), delegate to the class. Given an external name (a string) return it.")
(:method ((class class))
(string (class-name class)))
(:method ((object structure-object))
(class-identifier (class-of object)))
(:method ((object standard-object))
(class-identifier (class-of object)))
(:method ((class-name symbol))
(class-identifier (find-class class-name)))
(:method ((identifier string))
identifier))
;;; 20110402 : lw does not allow for standard argument keys, thus the &allow-other-keys here
(defmethod c2mop:direct-slot-definition-class ((class thrift-class) &key
identifier (identifier-name identifier)
&allow-other-keys)
"If an id is present in the definition, the slot is included to pr included when de/serializing"
(cond (identifier-name
(find-class 'direct-field-definition))
(t
(call-next-method))))
(defmethod c2mop:effective-slot-definition-class ((class thrift-class) &key name
&allow-other-keys)
"If some direct lost definition indicates thrift support, them carry that over to the effective definition"
(if (some #'(lambda (class)
(typep (find name (c2mop:class-direct-slots class) :key #'field-definition-name)
'direct-field-definition))
(c2mop:class-precedence-list class))
(find-class 'effective-field-definition)
(call-next-method)))
(defmethod c2mop:compute-effective-slot-definition ((class thrift-class) name direct-slots)
(let ((sd (call-next-method)))
(typecase sd
(effective-field-definition
(setf (slot-value sd 'identifier) (or (some #'field-definition-identifier direct-slots)
(error "No direct slot specified an identifier: ~s." name)))
(setf (slot-value sd 'identifier-number) (or (some #'field-definition-identifier-number direct-slots)
(error "No direct slot specified an id number: ~s." name)))
(setf (slot-value sd 'reader) (or (some #'field-definition-reader direct-slots)
(error "No direct slot specified a reader: ~s." name)))
(setf (slot-value sd 'optional) (some #'field-definition-optional direct-slots))))
sd))
(defgeneric field-definition-identifier (field-definition)
(:method ((fd cl:list))
;; for use in macros
(first fd))
(:method ((sd c2mop:slot-definition))
"Provide a base method which returns nil to permit filtering all definitions."
nil))
(defgeneric field-definition-identifier-number (field-definition)
(:method ((fd cl:list))
;; for use in macros
(getf (cddr fd) :id))
(:method ((sd c2mop:slot-definition))
"Provide a base method which returns nil to permit filtering all definitions."
nil))
(defgeneric field-definition-optional (field-definition)
(:method ((fd cl:list))
;; for use in macros
(getf (cddr fd) :optional))
(:method ((sd c2mop:slot-definition))
"Provide a base method which returns nil to permit filtering all definitions."
nil))
(defgeneric field-definition-initarg (field-definition)
(:method ((sd c2mop:slot-definition))
(first (c2mop:slot-definition-initargs sd))))
(defgeneric field-definition-name (field-definition)
(:method ((fd cl:list))
;; for use in macros
(let ((place (first fd)))
(etypecase place
(cons place) ; for (setf (slot-value ...
(string (str-sym place))
;; allow for the gensym in the interposed result field
(symbol place))))
(:method ((sd c2mop:slot-definition))
(c2mop:slot-definition-name sd)))
(defgeneric field-definition-reader (field-definition)
(:method ((sd c2mop:direct-slot-definition))
(first (c2mop:slot-definition-readers sd))))
(defgeneric field-definition-type (field-definition)
(:method ((fd cl:list))
;; for use in macros
(getf (cddr fd) :type))
(:method ((sd c2mop:slot-definition))
(let ((literal-type (c2mop:slot-definition-type sd)))
;; clozure rewrites the types specified in a slot definition
(etypecase literal-type
(symbol (case literal-type
(boolean 'bool)
(double-float 'thrift:double)
(single-float 'thrift:float)
(base-string 'string)
(t literal-type)))
(cons (case (first literal-type)
(member (if (or (equal '(member nil t) literal-type) (equal '(member t nil) literal-type))
'bool
literal-type))
(signed-byte (ecase (second literal-type)
(8 'i08)
(16 'i16)
(32 'i32)
(64 'i64)))
((array vector) 'binary)
(t literal-type)))))))
(defgeneric class-field-definitions (class)
(:method ((class symbol))
(class-field-definitions (find-class class)))
(:method ((class thrift-class))
(unless (c2mop:class-finalized-p class)
(c2mop:finalize-inheritance class))
(remove-if-not #'(lambda (sd) (typep sd 'effective-field-definition)) (c2mop:class-slots class)))
(:method ((object standard-object))
(class-field-definitions (class-of object)))
(:method ((object structure-object))
(class-field-definitions (class-of object)))
(:method ((object condition))
(class-field-definitions (class-of object)))
(:method ((identifier string))
(class-field-definitions (str-sym identifier)))
(:method ((class class))
(if (subtypep class 'condition)
(class-field-definitions (cons-symbol (symbol-package (class-name class)) (class-name class) :-thrift-class))
nil)))
;;;
;;; instantiation : provide specialized make- operators which use make-instance or make-condition
;;; as per metaclass type
(defgeneric make-struct (class &rest initargs)
(:method ((class-name symbol) &rest initargs)
(declare (dynamic-extent initargs))
(apply #'make-struct (find-thrift-class class-name) initargs))
(:method ((class thrift-struct-class) &rest initargs)
(declare (dynamic-extent initargs))
(apply #'make-instance class initargs))
(:method ((class thrift-exception-class) &rest initargs)
(declare (dynamic-extent initargs))
(apply #'make-condition class initargs)))
(defgeneric struct-name (class)
(:method ((class class))
(class-name class))
(:method ((class thrift-struct-class))
(class-name class))
(:method ((class thrift-exception-class))
(class-condition-class class)))
;;;
;;; exceptions
(defmethod unknown-field ((class thrift-class) (name t) (id t) (type t) (value t))
"The default method for thrift classes does nothing, which is intended to leave the final
disposition to the protocol."
nil)