git.fiddlerwoaroof.com
Browse code

Rewrite yason encoders to use patmatch:let-pat*

fiddlerwoaroof authored on 24/08/2017 07:38:06
Showing 1 changed files
... ...
@@ -1,56 +1,49 @@
1 1
 (in-package :alimenta.feed-archive.encoders)
2 2
 
3
-(defmethod yason:encode ((object pathname) &optional stream)
4
-  (yason:encode (princ-to-string (uiop:native-namestring object))
5
-                       stream)
6
-  object)
7
-
8
-(defmethod yason:encode ((object puri:uri) &optional stream)
9
-  (yason:encode (puri:render-uri object nil)
10
-                stream)
11
-  object)
12
-
13 3
 (defmethod yason:encode-slots progn ((feed alimenta:feed))
14
-  (with-accessors ((description alimenta:description)
15
-                   (feed-link alimenta:feed-link)
16
-                   (items alimenta:items)
17
-                   (link alimenta:link)
18
-                   (source-type alimenta:source-type)
19
-                   (title alimenta:title)) feed
20
-    (yason:encode-object-element "title" title)
21
-    (yason:encode-object-element "fetch-url"
22
-                                 (puri:render-uri feed-link nil))
23
-    (yason:encode-object-element "link" link)
24
-    ;;(yason:encode-object-element "source-type" source-type)
25
-    (yason:encode-object-element "description" description))
26
-  feed)
4
+  (prog1 feed
5
+    (patmatch:let-pat* (((alimenta:feed :title title
6
+                                        :feed-link feed-link
7
+                                        :link link
8
+                                        :description description)
9
+                         feed))
10
+      (yason:encode-object-element "title" title)
11
+      (yason:encode-object-element "fetch-url" (puri:render-uri feed-link nil))
12
+      (yason:encode-object-element "link" link)
13
+      ;;(yason:encode-object-element "source-type" source-type)
14
+      (yason:encode-object-element "description" description))))
27 15
 
28 16
 
29
-(defmethod yason:encode ((feed alimenta:feed) &optional stream)
30
-  (yason:with-output (stream :indent t)
31
-    (yason:encode-object feed)))
32
-
33 17
 (defmethod yason:encode-slots progn ((item alimenta:item))
34
-  (with-accessors ((author alimenta:author)
35
-                   (content alimenta:content)
36
-                   (date alimenta:date)
37
-                   (id alimenta:id)
38
-                   (link alimenta:link)
39
-                   (title alimenta:title)) item
18
+  (patmatch:let-pat* (((alimenta:item :author author
19
+                                      :content content
20
+                                      :date date
21
+                                      :id id
22
+                                      :link link
23
+                                      :title title)
24
+                       item))
40 25
     (let* ((date (local-time:format-timestring nil date)))
41
-      (yason:with-object ()
42
-	(yason:encode-object-element "title" title)
43
-	(yason:encode-object-element "date" date)
44
-	(yason:encode-object-element "author" title)
45
-	(yason:encode-object-element "id" (princ-to-string id))
46
-	(yason:encode-object-element "link" link)
47
-	(yason:encode-object-element "content" content)))))
26
+      (yason:encode-object-element "title" title)
27
+      (yason:encode-object-element "date" date)
28
+      (yason:encode-object-element "author" author)
29
+      (yason:encode-object-element "id" (princ-to-string id))
30
+      (yason:encode-object-element "link" link)
31
+      (yason:encode-object-element "content" content))))
48 32
 
49
-(defmethod yason:encode ((item alimenta:item) &optional stream)
50
-  (yason:with-output (stream :indent t)
51
-    (yason:encode-slots item))
52
-  item)
33
+(defmethod yason:encode ((object pathname) &optional stream)
34
+  (prog1 object
35
+    (yason:encode (princ-to-string (uiop:native-namestring object))
36
+                  stream)))
53 37
 
38
+(defmethod yason:encode ((object puri:uri) &optional stream)
39
+  (prog1 object
40
+    (yason:encode (puri:render-uri object nil)
41
+                  stream)))
42
+
43
+(defmethod yason:encode ((feed-entity alimenta:feed-entity) &optional stream)
44
+  (prog1 feed-entity
45
+    (yason:with-output (stream :indent t)
46
+      (yason:encode-object feed-entity))))
54 47
 
55 48
 (defun encode-collection-object (other-pairs collection-key collection-value element-encoder)
56 49
   (yason:with-object ()