topology_diff.go 829 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package detailed
  2. import (
  3. "reflect"
  4. )
  5. // Diff is returned by TopoDiff. It represents the changes between two
  6. // NodeSummary maps.
  7. type Diff struct {
  8. Add []NodeSummary `json:"add"`
  9. Update []NodeSummary `json:"update"`
  10. Remove []string `json:"remove"`
  11. Reset bool `json:"reset,omitempty"`
  12. }
  13. // TopoDiff gives you the diff to get from A to B.
  14. func TopoDiff(a, b NodeSummaries) Diff {
  15. diff := Diff{Reset: a == nil}
  16. notSeen := map[string]struct{}{}
  17. for k := range a {
  18. notSeen[k] = struct{}{}
  19. }
  20. for k, node := range b {
  21. if _, ok := a[k]; !ok {
  22. diff.Add = append(diff.Add, node)
  23. } else if !reflect.DeepEqual(node, a[k]) {
  24. diff.Update = append(diff.Update, node)
  25. }
  26. delete(notSeen, k)
  27. }
  28. // leftover keys
  29. for k := range notSeen {
  30. diff.Remove = append(diff.Remove, k)
  31. }
  32. return diff
  33. }