-
Notifications
You must be signed in to change notification settings - Fork 0
/
changeset.go
45 lines (42 loc) · 974 Bytes
/
changeset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package rkive
import (
"errors"
"fmt"
)
var (
ErrDone = errors.New("done")
)
// PushChangeset pushes a changeset to an object, handling the case
// in which the object has been updated in the database since the last
// local fetch. The 'chng' function should check if the change that it wanted
// already happened, and return ErrDone in that case. The 'chng' function is allowed
// to type-assert its argument to the underlying type of 'o'.
func (c *Client) PushChangeset(o Object, chng func(Object) error, opts *WriteOpts) error {
err := chng(o)
if err != nil {
return err
}
nmerge := 0
push:
err = c.Push(o, opts)
if err == ErrModified {
var upd bool
nmerge++
if nmerge > maxMerges {
return fmt.Errorf("exceeded max merges: %s", err)
}
upd, err = c.Update(o, nil)
if err != nil {
return err
}
if !upd {
return errors.New("failure updating...")
}
err = chng(o)
if err == ErrDone {
return nil
}
goto push
}
return err
}