forked from mmontone/schemata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.lisp
169 lines (150 loc) · 7.22 KB
/
parsing.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
(in-package :schemata)
(defgeneric parse-with-schema (schema string-or-data &optional format)
(:documentation "Parses the string to an association list using the schema"))
(defmethod parse-with-schema ((schema symbol) string-or-data &optional (format :json))
(parse-with-schema (find-schema schema) string-or-data format))
(defmethod parse-with-schema (schema data &optional (format :json))
(%parse-with-schema (schema-type schema)
schema
data))
(defmethod %parse-with-schema ((schema-type (eql :object))
schema data)
(if (null data)
data
(loop
for schema-attribute in (object-attributes schema)
for data-attribute = (assoc (string (attribute-name schema-attribute))
data
:test #'equalp
:key #'string)
appending
(cond
;; Don't do validation here, only parsing
#+nil((and (not data-attribute)
(not (attribute-optional-p schema-attribute)))
(validation-error "Attribute ~a not found in ~a"
(attribute-name schema-attribute)
data))
((not data-attribute)
;; don't add the attribute to the data in this case
;; idea to think of: we could use the attribute default value if specified, instead
nil)
((or (equalp (attribute-type schema-attribute) :boolean)
(not (null (cdr data-attribute))))
(list (cons (intern (string (attribute-name schema-attribute)) :keyword)
(parse-schema-attribute schema-attribute (cdr data-attribute)))))))))
(defun parse-schema-attribute (schema-attribute value)
(let ((parsed-value (parse-schema-attribute-value (attribute-type schema-attribute) value)))
(if (attribute-parser schema-attribute)
(funcall (attribute-parser schema-attribute)
parsed-value)
parsed-value)))
(defmethod %parse-with-schema ((schema-type (eql :list))
schema data)
(let ((elem-schema (second schema)))
(flet ((parse-elem (elem)
(if (symbolp elem-schema)
(parse-schema-attribute-value elem-schema elem)
(parse-with-schema elem-schema elem))))
(loop for elem in data
collect (parse-elem elem)))))
(defmethod parse-schema-attribute-value ((type (eql :string)) data)
(string data))
(defmethod parse-schema-attribute-value ((type (eql :boolean)) data)
(cond
((or (eql data t)
(eql data nil))
data)
((stringp data)
(cond
((member data (list "true" "t" "yes" "on") :test #'equalp)
t)
((member data (list "false" "f" "no" "off") :test #'equalp)
nil)
(t (validation-error "~A is not a boolean" data))))
(t (validation-error "~A is not a boolean" data))))
(defmethod parse-schema-attribute-value ((type (eql :integer)) data)
(cond
((integerp data)
data)
((stringp data)
(parse-integer data))
(t (validation-error "~A is not an integer" data))))
(defmethod parse-schema-attribute-value ((type (eql :float)) data)
(cond
((floatp data)
data)
((stringp data)
(read-from-string data))
(t (validation-error "~A is not a float" data))))
(defmethod parse-schema-attribute-value ((type (eql :timestamp)) data)
(or (ignore-errors (local-time:parse-timestring data :allow-missing-timezone-part t))
(chronicity:parse data)))
(defmethod parse-schema-attribute-value ((type (eql :datetime)) data)
(or (ignore-errors (local-time:parse-timestring data :allow-missing-timezone-part t))
(chronicity:parse data)))
(defmethod parse-schema-attribute-value ((type (eql :time)) data)
(or
(ignore-errors (local-time:parse-timestring
data
:allow-missing-date-part t
:allow-missing-timezone-part t))
(chronicity:parse data)))
(defmethod parse-schema-attribute-value ((type (eql :date)) data)
(or
(ignore-errors (local-time:parse-timestring
data
:allow-missing-time-part t
:allow-missing-timezone-part t))
(chronicity:parse data)))
(defmethod parse-schema-attribute-value ((type (eql :keyword)) data)
(intern (string-upcase data) :keyword))
(defmethod parse-schema-attribute-value ((type symbol) data)
(let ((schema (find-schema type)))
(parse-with-schema schema data)))
(defmethod parse-schema-attribute-value ((type cons) data)
(parse-with-schema type data))
(defun parse-xml-with-schema (schema-or-name input)
(let ((schema (if (symbolp schema-or-name)
(find-schema schema-or-name)
schema-or-name)))
(ecase (schema-type schema)
(:list
(let ((items (third input)))
(loop for item in items
collect
(parse-xml-with-schema
(second schema) ;; the list type
item))))
(:object
(assert (equalp (alexandria:make-keyword (object-name schema))
(alexandria:make-keyword (first input))) nil
"~A is not a ~A" input (object-name schema))
(loop for attribute in (object-attributes schema)
appending (let ((input-attribute
(find (symbol-name (attribute-name attribute))
(cddr input)
:key #'first
:test #'equalp)))
(if input-attribute
;; The attrbute is present
(list (cons (alexandria:make-keyword (first input-attribute))
(cond
((listp (attribute-type attribute))
;; It is a compound type (:list, :object, etc)
(parse-xml-with-schema
(second (attribute-type attribute)) ;; The compound object type
(third input-attribute) ;; The attribute value
))
((keywordp (attribute-type attribute))
;; the attribute type is simple, parse the attribute value
(unserialize-schema-attribute-value
(attribute-type attribute)
(third input-attribute)))
((symbolp (attribute-type attribute))
;; assume a schema reference
(let ((attribute-schema (find-schema (attribute-type attribute))))
(parse-xml-with-schema
attribute-schema
(third input-attribute) ;; The attribute value
)))))))))))))