Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose heads raw state representation #191

Merged
merged 4 commits into from
Jul 13, 2024

Conversation

miguelangel-dev
Copy link
Contributor

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

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.

Copy link
Collaborator

@heckj heckj left a comment

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.

Sources/Automerge/ChangeHash.swift Outdated Show resolved Hide resolved
Co-authored-by: Joseph Heck <heckj@mac.com>
@alexjg
Copy link
Collaborator

alexjg commented Jul 12, 2024

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
Copy link
Collaborator

heckj commented Jul 13, 2024

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
4 checks passed
@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