mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-08 05:47:07 +09:00
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package pbtypes
|
|
|
|
import "github.com/anyproto/anytype-heart/pkg/lib/pb/model"
|
|
|
|
// TODO Add domaain model for link
|
|
type RelationLinks []*model.RelationLink
|
|
|
|
func (rl RelationLinks) Get(key string) *model.RelationLink {
|
|
for _, l := range rl {
|
|
if l.Key == key {
|
|
return l
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rl RelationLinks) Has(key string) bool {
|
|
for _, l := range rl {
|
|
if l.Key == key {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (rl RelationLinks) Append(l *model.RelationLink) RelationLinks {
|
|
return append(rl, l)
|
|
}
|
|
|
|
func (rl RelationLinks) Remove(id string) RelationLinks {
|
|
var n int
|
|
for _, x := range rl {
|
|
if x.Key != id {
|
|
rl[n] = x
|
|
n++
|
|
}
|
|
}
|
|
return rl[:n]
|
|
}
|
|
|
|
func (rl RelationLinks) Copy() RelationLinks {
|
|
res := make(RelationLinks, 0, len(rl))
|
|
for _, l := range rl {
|
|
res = append(res, &model.RelationLink{
|
|
Format: l.Format,
|
|
Key: l.Key,
|
|
})
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (rl RelationLinks) Diff(prev RelationLinks) (added []*model.RelationLink, removed []string) {
|
|
var common = make(map[string]struct{})
|
|
for _, l := range rl {
|
|
if !prev.Has(l.Key) {
|
|
added = append(added, l)
|
|
} else {
|
|
common[l.Key] = struct{}{}
|
|
}
|
|
}
|
|
for _, l := range prev {
|
|
if _, ok := common[l.Key]; !ok {
|
|
removed = append(removed, l.Key)
|
|
}
|
|
}
|
|
return
|
|
}
|