slicediff.go 587 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package slicediff
  2. const (
  3. Remove = iota + 1
  4. Add
  5. Update
  6. )
  7. type Action struct {
  8. Id int
  9. Type int
  10. }
  11. var ActionString = [3]string{"REMOVE", "ADD", "UPDATE"}
  12. func Diff(dst func() []string, src func() []string) map[string]*Action {
  13. actions := make(map[string]*Action)
  14. m := make(map[string]bool)
  15. for id, s := range dst() {
  16. actions[s] = &Action{id, Remove}
  17. }
  18. for id, s := range src() {
  19. if _, ok := m[s]; !ok {
  20. m[s] = true
  21. _, ok := actions[s]
  22. if !ok {
  23. actions[s] = &Action{id, Add}
  24. continue
  25. }
  26. actions[s] = &Action{id, Update}
  27. }
  28. }
  29. return actions
  30. }