add jsn format using FormatMsg
@@ -212,13 +212,15 @@ func (c *Collector) Start() {
}
// FormatMsg formats the gnmi.SubscribeResponse and returns a []byte and an error
-func (c *Collector) FormatMsg(meta map[string]interface{}, rsp *gnmi.SubscribeResponse) ([]byte, error) {
+func FormatMsg(meta map[string]string, rsp proto.Message) ([]byte, error) {
if rsp == nil {
return nil, nil
}
- if c.Config.Format == "textproto" {
- return []byte(prototext.Format(rsp)), nil
- }
+ // if c.Config.Format == "textproto" {
+ // return []byte(prototext.Format(rsp)), nil
+ // }
+ switch rsp := rsp.ProtoReflect().Interface().(type) {
+ case *gnmi.SubscribeResponse:
switch rsp := rsp.Response.(type) {
case *gnmi.SubscribeResponse_Update:
msg := new(msg)
@@ -226,18 +228,17 @@ func (c *Collector) FormatMsg(meta map[string]interface{}, rsp *gnmi.SubscribeRe
t := time.Unix(0, rsp.Update.Timestamp)
msg.Time = &t
if meta == nil {
- meta = make(map[string]interface{})
+ meta = make(map[string]string)
}
msg.Prefix = gnmiPathToXPath(rsp.Update.Prefix)
- var ok bool
- if _, ok = meta["source"]; ok {
- msg.Source = fmt.Sprintf("%s", meta["source"])
+ if s, ok := meta["source"]; ok {
+ msg.Source = s
}
- if _, ok = meta["system-name"]; ok {
- msg.SystemName = fmt.Sprintf("%s", meta["system-name"])
+ if s, ok := meta["system-name"]; ok {
+ msg.SystemName = s
}
- if _, ok = meta["subscription-name"]; ok {
- msg.SubscriptionName = fmt.Sprintf("%s", meta["subscription-name"])
+ if s, ok := meta["subscription-name"]; ok {
+ msg.SubscriptionName = s
}
for i, upd := range rsp.Update.Update {
pathElems := make([]string, 0, len(upd.Path.Elem))
@@ -246,7 +247,7 @@ func (c *Collector) FormatMsg(meta map[string]interface{}, rsp *gnmi.SubscribeRe
}
value, err := getValue(upd.Val)
if err != nil {
- c.Logger.Println(err)
+ return nil, err
}
msg.Updates = append(msg.Updates,
&update{
@@ -264,6 +265,7 @@ func (c *Collector) FormatMsg(meta map[string]interface{}, rsp *gnmi.SubscribeRe
}
return data, nil
}
+ }
return nil, nil
}
@@ -320,11 +322,13 @@ func Marshal(msg proto.Message, format string, meta map[string]string, multiline
b := make([]byte, 0)
var err error
switch format {
+ case "json":
+ b, err = FormatMsg(meta, msg)
case "proto":
b, err = proto.Marshal(msg)
- case "json":
+ case "protojson":
b, err = protojson.Marshal(msg)
- case "textproto":
+ case "prototext":
b, err = prototext.MarshalOptions{Multiline: multiline, Indent: indent}.Marshal(msg)
case "event":
switch msg := msg.ProtoReflect().Interface().(type) {
