1
0
Fork 0
mirror of https://github.com/Luzifer/cloudbox.git synced 2024-12-23 03:01:18 +00:00
cloudbox/sync/change.go

70 lines
1.1 KiB
Go
Raw Normal View History

2019-06-16 21:03:05 +00:00
package sync
import "strings"
type Change uint8
const (
ChangeLocalAdd Change = 1 << iota
ChangeLocalDelete
ChangeLocalUpdate
ChangeRemoteAdd
ChangeRemoteDelete
ChangeRemoteUpdate
)
var changeNameMap = map[Change]string{
ChangeLocalAdd: "local-add",
ChangeLocalDelete: "local-delete",
ChangeLocalUpdate: "local-update",
ChangeRemoteAdd: "remote-add",
ChangeRemoteDelete: "remote-delete",
ChangeRemoteUpdate: "remote-update",
}
func (c Change) Changed() bool {
return c != 0
}
func (c *Change) Register(add Change) {
*c |= add
2019-06-16 21:03:05 +00:00
}
func (c Change) HasAll(test ...Change) bool {
for _, t := range test {
if c&t == 0 {
return false
}
}
return true
}
func (c Change) HasOne(test ...Change) bool {
for _, t := range test {
if c&t != 0 {
return true
}
}
return false
}
func (c Change) Is(test Change) bool {
return c == test
}
func (c Change) String() string {
if !c.Changed() {
return "none"
}
names := []string{}
for k, v := range changeNameMap {
if c.HasOne(k) {
names = append(names, v)
}
}
return strings.Join(names, ", ")
}