Merge pull request #1830 from stevvooe/define-fieldpath-support-envelope

events: define fieldpath implement for envelope
This commit is contained in:
Phil Estes 2017-11-29 17:21:26 -05:00 committed by GitHub
commit 59bd196711
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"time" "time"
"github.com/containerd/typeurl"
"github.com/gogo/protobuf/types" "github.com/gogo/protobuf/types"
) )
@ -15,6 +16,36 @@ type Envelope struct {
Event *types.Any Event *types.Any
} }
// Field returns the value for the given fieldpath as a string, if defined.
// If the value is not defined, the second value will be false.
func (e *Envelope) Field(fieldpath []string) (string, bool) {
if len(fieldpath) == 0 {
return "", false
}
switch fieldpath[0] {
// unhandled: timestamp
case "namespace":
return string(e.Namespace), len(e.Namespace) > 0
case "topic":
return string(e.Topic), len(e.Topic) > 0
case "event":
decoded, err := typeurl.UnmarshalAny(e.Event)
if err != nil {
return "", false
}
adaptor, ok := decoded.(interface {
Field([]string) (string, bool)
})
if !ok {
return "", false
}
return adaptor.Field(fieldpath[1:])
}
return "", false
}
// Event is a generic interface for any type of event // Event is a generic interface for any type of event
type Event interface{} type Event interface{}