Skip to content

Commit

Permalink
exposing AutomergeUtilities product, and including documentation (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj authored Feb 15, 2024
1 parent 7e89039 commit 59e3a1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ let package = Package(
name: "Automerge",
platforms: [.iOS(.v13), .macOS(.v10_15)],
products: [
.library(name: "Automerge", targets: ["Automerge"]),
.library(name: "Automerge", targets: ["Automerge", "AutomergeUtilities"]),
],
targets: [
FFIbinaryTarget,
Expand Down
17 changes: 13 additions & 4 deletions Sources/AutomergeUtilities/Document+schema.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import Automerge

/// A type that mirrors the Automerge internal types
public enum AutomergeValue: Hashable, Equatable {
/// Represents a dictionary or map type.
case dict([String: AutomergeValue])
/// Represents an array or list type.
case array([AutomergeValue])
/// Represents an Automerge Text type.
case text(String)
/// Represents an Automerge scalar value.
case scalar(ScalarValue)
}

extension AutomergeValue: CustomStringConvertible {
/// A text representation of the schema type and value.
public var description: String {
switch self {
case let .dict(dictionary):
Expand All @@ -23,13 +29,16 @@ extension AutomergeValue: CustomStringConvertible {
}

public extension Document {
/// A testing function that dumps the vaguely-typed contents of an Automerge document for the purposes of debugging.
///
/// The output is all through print to STDOUT.
/// A function that returns a tree-based structure of values that represents the current state of the document.
func schema() throws -> AutomergeValue {
try parseToSchema(self, from: ObjId.ROOT)
}


/// A function to walk an Automerge document from an initial object identifier that you provide, returning the schema below as a tree.
/// - Parameters:
/// - doc: The Automerge document to parse.
/// - objId: The object identifier at which to start the parse
/// - Returns: A tree that represents the schema and values.
func parseToSchema(_ doc: Document, from objId: ObjId) throws -> AutomergeValue {
switch doc.objectType(obj: objId) {
case .Map:
Expand Down
6 changes: 2 additions & 4 deletions Sources/AutomergeUtilities/Document+walk.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import Automerge

extension Document {
/// A testing function that dumps the vaguely-typed contents of an Automerge document for the purposes of debugging.
///
/// The output is all through print to STDOUT.
func walk() throws {
/// A testing function that prints the contents of an Automerge document with annotations for the type associated with each object and/or value for the purposes of debugging.
public func walk() throws {
print("{")
try walk(self, from: ObjId.ROOT)
print("}")
Expand Down

0 comments on commit 59e3a1a

Please sign in to comment.