Skip to content

Commit

Permalink
events: define fieldpath implement for envelope
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen J Day <stephen.day@docker.com>
  • Loading branch information
stevvooe committed Nov 29, 2017
1 parent a56e742 commit d7c950e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions events/events.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/containerd/typeurl"
"github.com/gogo/protobuf/types"
)

Expand All @@ -15,6 +16,36 @@ type Envelope struct {
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
type Event interface{}

Expand Down

0 comments on commit d7c950e

Please sign in to comment.