-
Notifications
You must be signed in to change notification settings - Fork 3
/
generators.el
383 lines (346 loc) · 15.4 KB
/
generators.el
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
;; [[file:doc.org::*Функции для кодогенерации сущностей][generators]]
;; Copyright © 2014-2015 Glukhov Mikhail. All rights reserved.
;; Licensed under the GNU AGPLv3
(setq org-confirm-babel-evaluate nil)
(defun outlist (lst ident fn)
(let ((outlist-result '())
(first (car lst))
(middle (butlast (cdr lst)))
(last (car (last lst))))
(cond ((equal 0 (length lst))
(push (concat ident "()") outlist-result))
((equal 1 (length lst))
(push (funcall fn (concat ident "(%s)") first) outlist-result))
((< 1 (length lst))
(push (funcall fn (concat ident "(%s") first) outlist-result)
(mapcar #'(lambda (x)
(push (funcall fn (concat "\n" ident " %s") x) outlist-result))
middle)
(push (funcall fn (concat "\n" ident " %s)") last) outlist-result)))
outlist-result))
(defun split-name (s)
(split-string
(let ((case-fold-search nil))
(downcase
(replace-regexp-in-string "\\([a-z]\\)\\([A-Z]\\)" "\\1 \\2" s)))
"[^A-Za-z0-9]+"))
(defun camelcase (s) (mapconcat 'capitalize (split-name s) ""))
(defun underscore (s) (mapconcat 'downcase (split-name s) "_"))
(defun dasherize (s) (mapconcat 'downcase (split-name s) "-"))
(defun colonize (s) (mapconcat 'capitalize (split-name s) "::"))
(defun camelscore (s)
(cond ((string-match-p "\\(?:[a-z]+_\\)+[a-z]+" s)(dasherize s))
((string-match-p "\\(?:[a-z]+-\\)+[a-z]+" s)(camelcase s))
((string-match-p "\\(?:[A-Z][a-z]+\\)+$" s)(colonize s))
(t(underscore s)) ))
(defun camelscore-word-at-point ()
(interactive)
(let* ((case-fold-search nil)
(beg (and (skip-chars-backward "[:alnum:]:_-") (point)))
(end (and (skip-chars-forward "[:alnum:]:_-") (point)))
(txt (buffer-substring beg end))
(cml (camelscore txt)) )
(if cml (progn (delete-region beg end) (insert cml)))))
(defun gen-fields (rows)
(let ((fields) (primary) (foreign) (unique))
(mapcar #'(lambda (val)
(unless (string= "" (nth 3 val))
(mapcar #'(lambda (addon-key)
(let (meta-key)
(if (listp addon-key)
(setq meta-key (car addon-key))
(setq meta-key addon-key))
(cond
((string= meta-key 'primary)
(push (car val) primary))
((string= meta-key 'one-to-many)
(push (list 'one-to-many (car val) (nth 1 addon-key)) foreign))
((string= meta-key 'many-to-one)
(push (list 'many-to-one (car val) (nth 1 addon-key)) foreign))
((string= meta-key 'unique)
(push (car val) unique)))))
(read (nth 3 val)))))
rows)
(values
(outlist rows " "
(function (lambda (tpl val)
(if (not (string= "" (nth 1 val)))
(if (string= "" (nth 2 val))
(format tpl (subseq val 0 2))
(format tpl (subseq val 0 3)))
(format tpl (list (car val)))))))
primary
(outlist (reverse foreign) " "
(function (lambda (tpl val)
(format tpl val))))
unique)))
(defun gen-states (rows)
(let ((result)
(hash (make-hash-table :test #'equal))
(states))
(dolist (elt rows nil)
(puthash (cadr elt) nil hash)
(puthash (cadr (cdr elt)) nil hash))
(maphash (lambda (k v)
(push k states))
hash)
(push "\n" result)
(push " (" result)
(dolist (elt (butlast states))
(push (format ":%s " elt) result))
(push (format ":%s)" (car (last states))) result)
(mapconcat 'identity (reverse result) "")))
(defun gen-actions (rows)
(let ((result))
(push "\n" result)
(let ((x (car rows)))
(push (format " ((:%s :%s :%s)" (cadr x) (cadr (cdr x)) (car x)) result))
(if (equal 1 (length rows))
(push ")" result)
(progn
(push "\n" result)
(mapcar #'(lambda (x)
(push (format " (:%s :%s :%s)\n" (cadr x) (cadr (cdr x)) (car x)) result))
(cdr (butlast rows)))
(let ((x (car (last rows))))
(push (format " (:%s :%s :%s))" (cadr x) (cadr (cdr x)) (car x)) result))))
(mapconcat 'identity (reverse result) "")))
(defmacro define-entity (name desc slot-descriptions primary foreign unique &rest class-options)
(let ((fields))
;; full initial hash with pair 'field-name' => 'slot-description'
(mapcar #'(lambda(slot)
(let ((base-data)
(name (car (subseq slot 0 1)))
(common-type)
(default)
(nullable)
(type)
(type-attrs)
(attrs))
(if (< 1 (length slot))
(setq common-type (car (subseq slot 1 2))))
(if (< 2 (length slot))
(setq default (car (subseq slot 2 3))))
;; преобразование типа из списка
(when (and (listp common-type) (string= "or" (car common-type)))
(setq type (car (last common-type)))
(setq type-attrs (cdr (last common-type)))
(setq nullable t))
(when (and (listp common-type) (not (string= "or" (car common-type))))
(setq type (car common-type))
(setq type-attrs (cdr common-type)))
(if (listp common-type)
(setq type (car common-type))
(setq type common-type))
(cond
((string= "numeric" type)
(setq attrs (plist-put attrs :precision (nth 0 type-attrs)))
(setq attrs (plist-put attrs :scale (nth 1 type-attrs))))
((string= "string" type)
(setq attrs (plist-put attrs :max (nth 0 type-attrs)))))
(setq fields
(plist-put fields (car slot)
(list :name name
:type type
:attrs attrs
:default default
:nullable nullable)))))
slot-descriptions)
;; let's parse type in more usable format
;;primary
(mapcar #'(lambda (primary-field)
(let ((field-value (plist-get fields primary-field)))
(when field-value
(plist-put fields primary-field (plist-put field-value :primary t)))))
primary)
;;foreign
(mapcar #'(lambda (foreign)
(let ((foreign-field (nth 1 foreign))
(field-value (plist-get fields (nth 1 foreign))))
(when field-value
(plist-put fields foreign-field (plist-put field-value
(intern (concat ":" (symbol-name (nth 0 foreign))))
(nth 2 foreign)))))
)
foreign)
;;unique
(mapcar #'(lambda (unique-field)
(let ((field-value (plist-get fields unique-field)))
(when field-value
(plist-put fields unique-field (plist-put field-value :unique t)))))
unique)
;; plist to normal list
(setq fields (cl-loop for (key value) on fields by 'cddr
collect value
))
`',fields))
;; ;; пример
;; (define-entity message "Сущность сообщений"
;; ((id serial)
;; (type integer)
;; (author integer)
;; (target_user integer)
;; (target_holder integer)
;; (datetime datetime)
;; (subject (string 127))
;; (text text)
;; (is_read boolean))
;; (id)
;; ((many-to-one type (message-type id))
;; (many-to-one author (user id))
;; (many-to-one target_user (user id))
;; (many-to-one target_holder (holder id)))
;; ())
(defmacro define-automat (name desc slot-descriptions primary foreign unique &rest class-options)
(let
((fields))
;; full initial hash with pair 'field-name' => 'slot-description'
(mapcar #'(lambda(slot)
(let ((base-data)
(name (car (subseq slot 0 1)))
(common-type)
(default)
(nullable)
(type)
(type-attrs)
(attrs))
(if (< 1 (length slot))
(setq common-type (car (subseq slot 1 2))))
(if (< 2 (length slot))
(setq default (car (subseq slot 2 3))))
;; преобразование типа из списка
(when (and (listp common-type) (string= "or" (car common-type)))
(setq type (car (last common-type)))
(setq type-attrs (cdr (last common-type)))
(setq nullable t))
(when (and (listp common-type) (not (string= "or" (car common-type))))
(setq type (car common-type))
(setq type-attrs (cdr common-type)))
(if (listp common-type)
(setq type (car common-type))
(setq type common-type))
(cond
((string= "numeric" type)
(setq attrs (plist-put attrs :precision (nth 0 type-attrs)))
(setq attrs (plist-put attrs :scale (nth 1 type-attrs))))
((string= "string" type)
(setq attrs (plist-put attrs :max (nth 0 type-attrs)))))
(setq fields
(plist-put fields (car slot)
(list :name name
:type type
:attrs attrs
:default default
:nullable nullable)))))
slot-descriptions)
;; let's parse type in more usable format
;;primary
(mapcar #'(lambda (primary-field)
(let ((field-value (plist-get fields primary-field)))
(when field-value
(plist-put fields primary-field (plist-put field-value :primary t)))))
primary)
;;foreign
(mapcar #'(lambda (foreign)
(let ((foreign-field (nth 1 foreign))
(field-value (plist-get fields (nth 1 foreign))))
(when field-value
(plist-put fields foreign-field (plist-put field-value
(intern (concat ":" (symbol-name (nth 0 foreign))))
(nth 2 foreign)))))
)
foreign)
;;unique
(mapcar #'(lambda (unique-field)
(let ((field-value (plist-get fields unique-field)))
(when field-value
(plist-put fields unique-field (plist-put field-value :unique t)))))
unique)
;; plist to normal list
(setq fields (cl-loop for (key value) on fields by 'cddr
collect value
))
`',fields))
(defmacro with-entity (entity &optional fields)
(let ((entity-data)
(result-fields)
(need-fields))
;; достаем сущность из сгенерированного файла
(setq entity-data
(eval (read
(with-temp-buffer
(insert-file-contents
(concat "./src/" (symbol-name entity) "-entity.lisp"))
(buffer-string)))))
;; фильтр по запрошенным полям
(when fields
(mapcar #'(lambda (field)
(if (listp field)
(setq need-fields (plist-put need-fields (car field) (cdr field)))
(setq need-fields (plist-put need-fields field nil))))
fields))
(if need-fields
(setq result-fields
(apply #'append
(mapcar #'(lambda (entity-field)
(setq field-name (plist-get entity-field :name))
(if (not (plist-member need-fields field-name))
nil
(setq result-field
(if (plist-get need-fields field-name)
(append entity-field
(plist-get need-fields field-name))
entity-field))
(setq result-field (plist-put result-field :entity entity))
(list result-field)))
entity-data)))
;;[TODO:bgg] этот блок можно переписать лучше, избежав дублирования кода
(setq result-fields
(apply #'append
(mapcar #'(lambda (entity-field)
(setq result-field (plist-put entity-field :entity entity))
(list result-field))
entity-data))))
`',result-fields))
;; ;; пример для message-entity без списка необходимых полей
;; (message "%s"
;; (with-entity message))
;; ;; пример для message-entity со списком необходимых полей
;; (message "%s"
;; (with-entity message
;; (id target_user target_holder)))
;; (print
;; (pp
;; (macroexpand-all
;; '(with-entity message
;; (id
;; target_user
;; target_holder
;; (text :label "Your message"))))))
(defun gen-post (rows var)
(flet ((rval (val var)
(let ((match-result (string-match ":" val)))
(if (and (not (null match-result))
(= 0 (string-match ":" val)))
(concat "\"" (substring val 1) "\"")
(format ",(drakma:url-encode (%s %s) :utf-8)" val var)))))
(let ((result))
(push (format "`((\"%s\" . %s)"
(caar rows)
(rval (cadar rows) var)
var)
result)
(mapcar #'(lambda (x)
(push (format "\n (\"%s\" . %s)"
(car x)
(rval (cadr x) var)
var)
result))
(butlast (cdr rows)))
(push (format "\n (\"%s\" . %s))"
(caar (last rows))
(rval (cadar (last rows)) var)
var)
result)
(mapconcat 'identity (reverse result) ""))))
;; generators ends here