json.go 953 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright (c) 2017 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package gnmi
  5. import (
  6. "github.com/openconfig/gnmi/proto/gnmi"
  7. )
  8. // NotificationToMap converts a Notification into a map[string]interface{}
  9. func NotificationToMap(notif *gnmi.Notification) (map[string]interface{}, error) {
  10. m := make(map[string]interface{}, 1)
  11. m["timestamp"] = notif.Timestamp
  12. m["path"] = StrPath(notif.Prefix)
  13. if len(notif.Update) != 0 {
  14. updates := make(map[string]interface{}, len(notif.Update))
  15. var err error
  16. for _, update := range notif.Update {
  17. updates[StrPath(update.Path)] = StrUpdateVal(update)
  18. if err != nil {
  19. return nil, err
  20. }
  21. }
  22. m["updates"] = updates
  23. }
  24. if len(notif.Delete) != 0 {
  25. deletes := make([]string, len(notif.Delete))
  26. for i, del := range notif.Delete {
  27. deletes[i] = StrPath(del)
  28. }
  29. m["deletes"] = deletes
  30. }
  31. return m, nil
  32. }