Re-structure code and add comments
This commit is contained in:
parent
a798184ad7
commit
f7d3750c48
1 changed files with 26 additions and 20 deletions
46
src/as2.lisp
46
src/as2.lisp
|
@ -96,6 +96,8 @@
|
|||
'("block" "delete" "ignore" "read" "remove" "type" "first" "last")
|
||||
"List of ActivityStream 2 names which are in conflict with reserved Common Lisp names.")
|
||||
|
||||
;;; From JSON to Object
|
||||
|
||||
(defun camel-kebab (camel-string)
|
||||
"Convert CAMEL-STRING as CamelCase to kebab-case."
|
||||
(string-downcase
|
||||
|
@ -106,6 +108,29 @@
|
|||
(write-char #\- out))
|
||||
(write-char c out)))))
|
||||
|
||||
(defun add-postfix (postfix string)
|
||||
(concatenate 'string string postfix))
|
||||
|
||||
(defun add-as2-postfix (string)
|
||||
(if (member string conflicting-type-names :test #'string=)
|
||||
(add-postfix "-as2" string)
|
||||
string))
|
||||
|
||||
(defun string-symbol (string)
|
||||
(intern (string-upcase (add-as2-postfix (camel-kebab string))) :ml-as2))
|
||||
|
||||
(defun object-from-hash (hash-table)
|
||||
"Create an AS2 object instance based on the 'type' field in the hash-table."
|
||||
(let* ((type-string (gethash "type" hash-table "Object"))
|
||||
(object-type-symbol (string-symbol type-string)))
|
||||
(if (find-class object-type-symbol nil)
|
||||
(let ((object (make-instance object-type-symbol)))
|
||||
(setf (slot-value object 'properties) hash-table)
|
||||
object)
|
||||
(error "Unknown AS2 object type: ~A" type-string))))
|
||||
|
||||
;;; From Object to JSON
|
||||
|
||||
(defun kebab-camel (kebab-string)
|
||||
"Convert KEBAB-STRING as kebab-case to camelCase."
|
||||
(with-output-to-string (out)
|
||||
|
@ -118,14 +143,6 @@
|
|||
(setf capitalize nil))
|
||||
(t (write-char (char-downcase char) out))))))
|
||||
|
||||
(defun add-postfix (postfix string)
|
||||
(concatenate 'string string postfix))
|
||||
|
||||
(defun add-as2-postfix (string)
|
||||
(if (member string conflicting-type-names :test #'string=)
|
||||
(add-postfix "-as2" string)
|
||||
string))
|
||||
|
||||
(defun remove-postfix (postfix string)
|
||||
"Remove the given postfix from the string if it exists."
|
||||
(let* ((postfix-length (length postfix))
|
||||
|
@ -143,18 +160,7 @@
|
|||
(defun prepare-property-key (name)
|
||||
(kebab-camel (remove-as2-postfix (string (symbol-name name)))))
|
||||
|
||||
(defun string-symbol (string)
|
||||
(intern (string-upcase (add-as2-postfix (camel-kebab string))) :ml-as2))
|
||||
|
||||
(defun object-from-hash (hash-table)
|
||||
"Create an AS2 object instance based on the 'type' field in the hash-table."
|
||||
(let* ((type-string (gethash "type" hash-table "Object"))
|
||||
(object-type-symbol (string-symbol type-string)))
|
||||
(if (find-class object-type-symbol nil)
|
||||
(let ((object (make-instance object-type-symbol)))
|
||||
(setf (slot-value object 'properties) hash-table)
|
||||
object)
|
||||
(error "Unknown AS2 object type: ~A" type-string))))
|
||||
;;; CLOS related stuff
|
||||
|
||||
(defclass property-container ()
|
||||
((properties :initform (make-hash-table :test #'equal)
|
||||
|
|
Loading…
Add table
Reference in a new issue