automerge / automerge-swift

Swift language bindings presenting Automerge
https://automerge.org/automerge-swift/documentation/automerge/
MIT License
217 stars 11 forks source link

Splice for with Object elements #102

Open lightsprint09 opened 5 months ago

lightsprint09 commented 5 months ago

Splice is very useful to build all kinds of functionality (append, remove, ...). How would I splice elements which are objects/codable objects.

heckj commented 5 months ago

I'm assuming you're talking about splice and not spliceText. Assuming you are, it applies to any sequence (List) CRDT - so it's a matter of how you'd like to map that back into the base Document.

You can use the custom Codable pieces enabled in Swift Automerge to encode an array into place, or if you want to control the lower-level access, you can convert your own objects into something and drop them into place manually. The Codable conformance was the way I spotted to make that possible. If you want the object itself represented as "schema" in the Automerge document, then using the custom encoder is probably the way to go. If you want to encode something yourself into, say, a binary representation (using protobuf or whatever), then you can do the encoding yourself and then add that as Value type into a List using a binary/data Value representation.

Look through the relevant methods under "Updating lists" at https://automerge.org/automerge-swift/documentation/automerge/document for the controls you have to update/manage lists yourself. (As you know, Automerge lists aren't strictly typed - but standard Swift arrays are - they're all of the same time. That constraint was built into the Codable encoder/decoder - so if you want a dynamic list, I'm afraid you'll have to assume the burden to assemble things yourself and handle the "what if it's not the type I expect" scenarios)