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

add convenience methods for creating new maps, sets, and counters #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions crdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ func (m *CrdtMap) Commit() (*rpb.DtUpdateResp, error) {
return res, err
}

// Returns a new CRDT map
func (m *CrdtMap) NewMap() *CrdtMap {
return m.crdt.NewMap()
}

// Returns a new CRDT set
func (m *CrdtMap) NewSet() *CrdtSet {
return m.crdt.NewSet()
}

// Returns a new CRDT counter
func (m *CrdtMap) NewCounter() *CrdtCounter {
return m.crdt.NewCounter()
}

// Crdt can manually take raw opts via Do() and execute Fetch() or Update(),
// however, it is preferred to call the Counter(), Set(), or Map() methods
// and work with each object through those interfaces.
Expand Down
26 changes: 26 additions & 0 deletions crdt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,29 @@ func TestCrdtMap(t *testing.T) {
t.Fatal(err.Error())
}
}

func TestMapMethods(t *testing.T) {
crdt := &Crdt{}
mp := &CrdtMap{crdt: crdt}

mp1 := crdt.NewMap()
mp2 := mp.NewMap()
if mp1.crdt != mp2.crdt {
t.Error("CrdtMap.NewMap() did not attach to the correct CRDT struct")
}

st1 := crdt.NewSet()
st2 := mp.NewSet()

if st1.crdt != st2.crdt {
t.Error("CrdtMap.NewSet() did not attach to the correct CRDT struct")
}

ct1 := crdt.NewCounter()
ct2 := mp.NewCounter()

if ct1.crdt != ct2.crdt {
t.Error("CrdtMap.NewCounter() did not attach to the correct CRDT struct")
}

}