github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/gnmi/json.go (about)

     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  
     5  package gnmi
     6  
     7  import (
     8  	"github.com/openconfig/gnmi/proto/gnmi"
     9  )
    10  
    11  // NotificationToMap converts a Notification into a map[string]interface{}
    12  func NotificationToMap(notif *gnmi.Notification) (map[string]interface{}, error) {
    13  	m := make(map[string]interface{}, 1)
    14  	m["timestamp"] = notif.Timestamp
    15  	m["path"] = StrPath(notif.Prefix)
    16  	if len(notif.Update) != 0 {
    17  		updates := make(map[string]interface{}, len(notif.Update))
    18  		var err error
    19  		for _, update := range notif.Update {
    20  			updates[StrPath(update.Path)] = StrUpdateVal(update)
    21  			if err != nil {
    22  				return nil, err
    23  			}
    24  		}
    25  		m["updates"] = updates
    26  	}
    27  	if len(notif.Delete) != 0 {
    28  		deletes := make([]string, len(notif.Delete))
    29  		for i, del := range notif.Delete {
    30  			deletes[i] = StrPath(del)
    31  		}
    32  		m["deletes"] = deletes
    33  	}
    34  	return m, nil
    35  }