Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
57 lines (48 sloc) 1.04 KB
package events
import (
"encoding/json"
"fmt"
"strings"
)
// Event represents different events
// in the lifecycle of a Buffalo app
type Event struct {
// Kind is the "type" of event "app:start"
Kind string `json:"kind"`
// Message is optional
Message string `json:"message"`
// Payload is optional
Payload Payload `json:"payload"`
// Error is optional
Error error `json:"-"`
}
func (e Event) String() string {
b, _ := e.MarshalJSON()
return string(b)
}
// MarshalJSON implements the json marshaler for an event
func (e Event) MarshalJSON() ([]byte, error) {
m := map[string]interface{}{
"kind": e.Kind,
}
if len(e.Message) != 0 {
m["message"] = e.Message
}
if e.Error != nil {
m["error"] = e.Error.Error()
}
if len(e.Payload) != 0 {
m["payload"] = e.Payload
}
return json.Marshal(m)
}
// Validate that an event is ready to be emitted
func (e Event) Validate() error {
if len(e.Kind) == 0 {
return fmt.Errorf("kind can not be blank")
}
return nil
}
func (e Event) IsError() bool {
return strings.HasSuffix(e.Kind, ":err")
}
You can’t perform that action at this time.