From f7d3750c48cc843b31eb514228fe85e6d38a692f Mon Sep 17 00:00:00 2001 From: Marcus Kammer Date: Sat, 5 Oct 2024 14:13:50 +0200 Subject: [PATCH] Re-structure code and add comments --- src/as2.lisp | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/as2.lisp b/src/as2.lisp index 1ec8887..07cef7a 100644 --- a/src/as2.lisp +++ b/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)