Parse even unknown ActivityStreams classes

By means of a *default-class* that is a catch-all.
This commit is contained in:
Jaidyn Ann 2024-06-23 22:47:55 -05:00
parent a2aec426ab
commit bb361465c3
2 changed files with 17 additions and 4 deletions

View File

@ -23,7 +23,7 @@
:parse :encode
:define-class-encoders
;; Globals
:*ap-packages*
:*ap-packages* :*default-class*
;; Classes
:object
;; Slots
@ -41,6 +41,12 @@ during JSON parsing. The class-name searched for is simply the value of the JSON
objects type key. The package first in the list to export such a symbol
is the winner.")
(defparameter *default-class* 'activity-servist/activity-streams:object
"The class used for ActivityStreams objects found during parsing that dont
have a corresponding class defined. Notably, all keys and values without
corresponding slots are placed in the MISC slot.
The class you choose should inherit ACTIVITY-SERVIST/ACTIVITY-STREAMS:OBJECT.")
;; Private, internal variable.
(defparameter *@context* nil
"Used in YASON:ENCODE to ensure that a single top-level @context can be
@ -96,7 +102,8 @@ again and again, by YASON:ENCODE-SLOTS."
(defun parse-table (table)
"Parse a hash-table corresponding to YASON-parsed JSON into an ActivityPub object."
(let* ((class (car (find-registered-classes (param-case (gethash "type" table)))))
(let* ((found-class (car (find-registered-classes (param-case (gethash "type" table)))))
(class (or found-class (find-class *default-class*)))
(obj (make-instance class)))
(loop for key being each hash-key of table
for val being each hash-value of table
@ -232,7 +239,11 @@ items in each will be contained in the resultant list."
*ap-packages* list."
(remove-if
#'not
(mapcar (lambda (package) (find-symbol (string-upcase str) package))
(mapcar (lambda (package)
(multiple-value-bind (sym context)
(find-symbol (string-upcase str) package)
(unless (eq context :inherited)
sym)))
*ap-packages*)))
(defun find-registered-classes (str)

View File

@ -54,6 +54,8 @@
(in-package #:activity-servist/activity-vocabulary)
(setq activity-servist/activity-streams:*default-class*
'activity-servist/activity-vocabulary:object)
;;; Macros