Skip to content

Expose heads raw state representation#191

Merged
heckj merged 4 commits into
automerge:mainfrom
miguelangel-dev:expose-changehash
Jul 13, 2024
Merged

Expose heads raw state representation#191
heckj merged 4 commits into
automerge:mainfrom
miguelangel-dev:expose-changehash

Conversation

@miguelangel-dev

@miguelangel-dev miguelangel-dev commented Jul 11, 2024

Copy link
Copy Markdown
Contributor

Motivation

Sometimes we want to persist in our domain a state representation based on Set<ChangeHash> (aka heads) without coupling to the Automerge library. In this way, we can restore this state by moving back to the Automerge domain. It is usually quite interesting to implement an undo manager based on "commits".

Introducing a raw() method to retrieve the raw representation of heads without any Automerge dependency; and also the go-back action into Set<ChangeHash>.

side-note: as we use an opaque type to encode the raw version of a group of change hashes, I am just checking it contains packages of 32 bytes to retrieve the associated change hash - after retrieving it, in case of not valid ChangeHash document will throw exceptions.

@heckj heckj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the relevant methods are kind of hidden on Set<ChangeHash> and Data, I think it's worth adding something to the overall documentation for ChangeHash, or potentially Document.heads() to reference them - otherwise they'll be really hard to find for anyone searching for this functionality. Not critical for this PR though.

Comment thread Sources/Automerge/ChangeHash.swift Outdated
Co-authored-by: Joseph Heck <heckj@mac.com>
@alexjg

alexjg commented Jul 12, 2024

Copy link
Copy Markdown
Collaborator

One thing that might be worth thinking about. In the automerge crate we have a TryFrom<&[u8]> implementation for ChangeHash. This is the supported way of converting bytes into a ChangeHash and is where we would handle future inconsistencies in hash format (not that I expect any at this point). If possible I think it would be best to expose this method up through the Uniffi UDL rather than assuming that any 32 byte array is a valid change hash. (This also applies to the serialization step, except we're already using ChangeHash::as_bytes when we generate the bytes which we pass to the Swift side).

@heckj

heckj commented Jul 13, 2024

Copy link
Copy Markdown
Collaborator

Created #193 to extend this to expose, and use, the tryFrom logic in the Rust core library within the internal initializer for ChangeHash

@heckj heckj merged commit 90f301a into automerge:main Jul 13, 2024
@miguelangel-dev miguelangel-dev deleted the expose-changehash branch July 15, 2024 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants