slicediff.go 650 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. type StringSlicer interface {
  12. Strings() []string
  13. }
  14. var ActionString = [3]string{"REMOVE", "ADD", "UPDATE"}
  15. func Diff(dst StringSlicer, src StringSlicer) map[string]*Action {
  16. actions := make(map[string]*Action)
  17. m := make(map[string]bool)
  18. for id, s := range dst.Strings() {
  19. actions[s] = &Action{id, Remove}
  20. }
  21. for id, s := range src.Strings() {
  22. if _, ok := m[s]; !ok {
  23. m[s] = true
  24. _, ok := actions[s]
  25. if !ok {
  26. actions[s] = &Action{id, Add}
  27. continue
  28. }
  29. actions[s] = &Action{id, Update}
  30. }
  31. }
  32. return actions
  33. }