26 lines
734 B
Go
26 lines
734 B
Go
package inventory
|
|
|
|
import "github.com/shcizo/package-updater/internal/protocol"
|
|
|
|
// Diff compares two snapshots. upserts holds services that are new or
|
|
// changed in next; removes holds the keys of services missing from next.
|
|
func Diff(prev, next []protocol.Service) (upserts []protocol.Service, removes []string) {
|
|
old := make(map[string]protocol.Service, len(prev))
|
|
for _, s := range prev {
|
|
old[s.Key()] = s
|
|
}
|
|
seen := make(map[string]struct{}, len(next))
|
|
for _, s := range next {
|
|
seen[s.Key()] = struct{}{}
|
|
if o, ok := old[s.Key()]; !ok || o != s {
|
|
upserts = append(upserts, s)
|
|
}
|
|
}
|
|
for _, s := range prev {
|
|
if _, ok := seen[s.Key()]; !ok {
|
|
removes = append(removes, s.Key())
|
|
}
|
|
}
|
|
return upserts, removes
|
|
}
|