Skip to content

Commit

Permalink
Merge pull request #22 from automerge/v0.2
Browse files Browse the repository at this point in the history
v0.2
  • Loading branch information
ConradIrwin authored Sep 3, 2023
2 parents 2df33bf + 996dffb commit 43b2624
Show file tree
Hide file tree
Showing 10 changed files with 517 additions and 108 deletions.
17 changes: 17 additions & 0 deletions automerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ assume the path is of the type you say it is:
When you do this, any errors caused by traversing the path will be returned from
methods called on the returned objects.
# Controling formatting of structs
By default automerge will convert your struct to a map. For each public field in the
struct (the name starts with an uppercase letter) automerge will add an entry to the
map with the name of the field and the fields value converted to an automerge value
recursively.
You can control this behaviour using struct tags:
struct Example {
Renamed bool `automerge:"newname"`
Private bool `automerge:"-"`
}
If the tag is present and equal to "-" the field will be ignored by automerge,
otherwise the fields name will be set to the value of the tag.
# Syncing and concurrency
You can access methods on [*Doc] from multiple goroutines and access is mediated
Expand Down
17 changes: 8 additions & 9 deletions automerge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestMap(t *testing.T) {

require.Equal(t, "&automerge.Map{\"string\": \"hello\"}", n.GoString())

require.NoError(t, m.Del("map"))
require.NoError(t, m.Delete("map"))
v, err := m.Get("map")
require.NoError(t, err)
require.True(t, v.IsVoid())
Expand Down Expand Up @@ -680,7 +680,7 @@ func TestSyncState(t *testing.T) {

resync := func() {
var valid1, valid2 bool
var m []byte
var m *automerge.SyncMessage
var err error

for {
Expand All @@ -689,15 +689,16 @@ func TestSyncState(t *testing.T) {
if !valid1 {
break
}
require.NoError(t, sState.ReceiveMessage(m))
_, err = sState.ReceiveMessage(m.Bytes())
require.NoError(t, err)

m, valid2 = sState.GenerateMessage()
require.NoError(t, err)
if !valid2 {
break
}

require.NoError(t, cState.ReceiveMessage(m))
_, err = cState.ReceiveMessage(m.Bytes())
require.NoError(t, err)
}
}

Expand All @@ -721,10 +722,8 @@ func TestSyncState(t *testing.T) {
require.Equal(t, cV, sV)
require.Equal(t, map[string]int{"s": 15, "c": 15}, cV)

cBytes, err := cState.Save()
require.NoError(t, err)
sBytes, err := sState.Save()
require.NoError(t, err)
cBytes := cState.Save()
sBytes := sState.Save()
cState, err = automerge.LoadSyncState(cDoc, cBytes)
require.NoError(t, err)
sState, err = automerge.LoadSyncState(sDoc, sBytes)
Expand Down
6 changes: 3 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ExampleAs() {
fmt.Println("foo-bar:", v)

type S struct {
IsValid bool `json:"isValid"`
IsValid bool `automerge:"isValid"`
}
s, err := automerge.As[*S](doc.Root())
if err != nil {
Expand Down Expand Up @@ -52,7 +52,7 @@ loop:
for {
msg, valid := syncState.GenerateMessage()
if valid {
send <- msg
send <- msg.Bytes()
}

select {
Expand All @@ -61,7 +61,7 @@ loop:
break loop
}

err := syncState.ReceiveMessage(msg)
_, err := syncState.ReceiveMessage(msg)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ func (i *item) syncState() *SyncState {
return ss
}

func (i *item) syncMessage() *syncMessage {
func (i *item) syncMessage() *SyncMessage {
defer runtime.KeepAlive(i)

ss := &syncMessage{item: i}
ss := &SyncMessage{item: i}
if !C.AMitemToSyncMessage(i.cItem, &ss.cSyncMessage) {
if i.Kind() == KindVoid {
return nil
Expand Down
4 changes: 2 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (m *Map) Len() int {
return int(C.AMobjSize(cDoc, cObj, nil))
}

// Del deletes a key and its corresponding value from the map
func (m *Map) Del(key string) error {
// Delete deletes a key and its corresponding value from the map
func (m *Map) Delete(key string) error {
if m.doc == nil {
return fmt.Errorf("automerge.Map: tried to write to detached map")
}
Expand Down
Loading

0 comments on commit 43b2624

Please sign in to comment.