fisxoj / sanity-clause

A data serialization/contract library for common lisp
GNU Lesser General Public License v3.0
49 stars 2 forks source link

Automatic persistence / serialization #12

Open Ambrevar opened 5 years ago

Ambrevar commented 5 years ago

Since sanity-clause supports serialization, would it be possible to have it automatically serialize and persist an object to a file on modification? More or less like ubiquitous does it?

Should we go down this road, wouldn't sanity-clause completely supersede Ubiquitous? What about mering the efforts?

fisxoj commented 5 years ago

Hi @Ambrevar , thanks for the interest!

I'm not familiar with ubiquitous, but it looks like there's room for the two libraries to coexist - sanity-clause could validate data and ubiquitious persists it. Maybe if I make something useful enough, it could be used by ubiquitous as the serialization layer. It looks like perhaps it already supports serializing lisp objects, so you might be able to create validated objects and load/save them already!

But also - I have added a basic interface for serializing/deserializing things to sanity-clause. I don't think adding the persistence logic feels in-scope of the library to me, but I imagine you could do something with (setf slot-value-using-class), maybe an after method that serializes for you?

(defmethod :after (setf slot-value-using-class) (new-value (class my-class) object slotd)
  (write-object-to-file object))

If you come up with something you think is sufficiently useful, I'd be happy to make/accept a helper package that integrates sanity-clause with another library.

Ambrevar commented 5 years ago

This could work, but then the next obvious goal would be to have such :after methods automatically set for all slots :)

To clarify: setting any slot of such a class would automatically be persisted to disk. Very much in the line of what some database managers do (like Mito).

Could be doable with a meta-class, what do you think?

With regard to the coexistence between sanity-clause and ubiquitous: Sanity-clause can a expose a configurable meta-class which sets the setf :after method to something the user decides, e.q. ubiquitous.

Would there still be room for sanity-clause's serialization layer then?

Ambrevar commented 4 years ago

By the way I tried

(defmethod (setf c2mop:slot-value-using-class) :after (new-value (class MY-CLASS) object slotd)
  (format t "Hacking ~a here" object))

and it does not seem to work. Hmmm, am I missing something?