本文整理汇总了Golang中github.com/tideland/golib/cells.Event.Payload方法的典型用法代码示例。如果您正苦于以下问题:Golang Event.Payload方法的具体用法?Golang Event.Payload怎么用?Golang Event.Payload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/tideland/golib/cells.Event
的用法示例。
在下文中一共展示了Event.Payload方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: payloadCents
// cents retrieves the cents out of the payload of an event.
func payloadCents(event cells.Event) int {
cents, ok := event.Payload().Get(cells.DefaultPayload)
if !ok {
return -1
}
return cents.(int)
}
示例2: ProcessEvent
// ProcessEvent reads, validates and emits a configuration.
func (b *configuratorBehavior) ProcessEvent(event cells.Event) error {
switch event.Topic() {
case ReadConfigurationTopic:
// Read configuration
filename, ok := event.Payload().GetString(ConfigurationFilenamePayload)
if !ok {
logger.Errorf("cannot read configuration without filename payload")
return nil
}
logger.Infof("reading configuration from %q", filename)
config, err := configuration.ReadFile(filename)
if err != nil {
return errors.Annotate(err, ErrCannotReadConfiguration, errorMessages)
}
// If wanted then validate it.
if b.validate != nil {
err = b.validate(config)
if err != nil {
return errors.Annotate(err, ErrCannotValidateConfiguration, errorMessages)
}
}
// All done, emit it.
pvs := cells.PayloadValues{
ConfigurationPayload: config,
}
b.ctx.EmitNew(ConfigurationTopic, pvs, event.Scene())
}
return nil
}
示例3: newEventData
// newEventData returns the passed event as event data to collect.
func newEventData(event cells.Event) EventData {
data := EventData{
Topic: event.Topic(),
Payload: event.Payload(),
}
return data
}
示例4: ProcessEvent
// ProcessEvent calls a callback functions with the event data.
func (b *callbackBehavior) ProcessEvent(event cells.Event) error {
for _, callbackFunc := range b.callbackFuncs {
if err := callbackFunc(event.Topic(), event.Payload()); err != nil {
return err
}
}
return nil
}
示例5: ProcessEvent
func (lu *logUser) ProcessEvent(event cells.Event) (err error) {
// TODO: would be nice to have an event.Source()
if event.Topic() == SAYS_ALL {
user, _ := event.Payload().Get("from")
msg, _ := event.Payload().Get("message")
log.Infof("%s heard %v say \"%v\"", lu.name, user, msg)
}
return
}
示例6: ProcessEvent
// ProcessEvent stores and flags the event in the event scene.
// So other code parts using the same scene can wait for the signal.
func (b *sceneBehavior) ProcessEvent(event cells.Event) error {
scn := event.Scene()
if scn != nil {
err := scn.StoreAndFlag(event.Topic(), event.Payload())
if err != nil {
return err
}
}
return nil
}
示例7: filter
func (c *censor) filter(event cells.Event) bool {
message, _ := event.Payload().Get("message")
user, _ := event.Payload().Get("from")
for _, w := range strings.Split(message.(string), " ") {
if _, ok := c.words[w]; ok {
log.Infof("%s said a bad word: %s", user, w)
c.warnings[user.(string)]++
return false
}
}
return true
}
示例8: Configuration
// Configuration returns the configuration payload
// of the passed event or an empty configuration.
func Configuration(event cells.Event) configuration.Configuration {
payload, ok := event.Payload().Get(ConfigurationPayload)
if !ok {
logger.Warningf("event does not contain configuration payload")
config, _ := configuration.ReadString("{config}")
return config
}
config, ok := payload.(configuration.Configuration)
if !ok {
logger.Warningf("configuration payload has illegal type")
config, _ := configuration.ReadString("{config}")
return config
}
return config
}
示例9: ProcessEvent
func (c *censor) ProcessEvent(event cells.Event) (err error) {
switch event.Topic() {
case CENSOR:
ok := c.filter(event)
userID, _ := event.Payload().Get("from")
event.Respond(ok)
if _, ok := c.warnings[userID.(string)]; !ok {
c.warnings[userID.(string)] = 0
}
if !ok {
return c.ctx.EmitNew(SAYS_TO, cells.PayloadValues{
"message": fmt.Sprintf("You can't say that! You've been warned %d times.", c.warnings[userID.(string)]),
"from": c.id,
"to": userID.(string),
}, nil)
}
}
return
}
示例10: ProcessEvent
func (b *building) ProcessEvent(event cells.Event) (err error) {
switch event.Topic() {
case ROOM_ADDED:
room, _ := event.Payload().Get("room")
b.rooms[room.(string)] = true
log.Infof("added %v", room)
case LIST_ROOMS:
rooms := []string{}
for room := range b.rooms {
rooms = append(rooms, room)
}
event.Respond(rooms)
case SAYS_ALL:
user, ok := event.Payload().Get("from")
if !ok || user.(string) != PublicAddressUserID {
return fmt.Errorf("invalid public address request")
}
b.ctx.Emit(event)
}
return
}
示例11: ProcessEvent
func (r *room) ProcessEvent(event cells.Event) (err error) {
switch event.Topic() {
case SAYS_ALL, SAYS_TO:
user, _ := event.Payload().Get("from")
switch user.(string) {
case PublicAddressUserID, r.censorID:
r.ctx.Emit(event)
default:
env := r.ctx.Environment()
ok, err := env.Request(r.censorID, CENSOR, event.Payload(), nil, time.Second)
if err != nil {
log.Error(err)
return err
}
if ok.(bool) {
r.ctx.Emit(event)
}
}
case IN_ROOM:
userID, _ := event.Payload().Get("user")
ok, _ := r.users[userID.(string)]
event.Respond(ok)
case USER_ADDED:
userID, _ := event.Payload().Get("user")
if ok, _ := r.users[userID.(string)]; !ok {
r.users[userID.(string)] = true
r.ctx.Environment().EmitNew(r.ctx.ID(), SAYS_ALL, cells.PayloadValues{
"message": fmt.Sprintf("%s has entered the room", userID),
"from": PublicAddressUserID,
}, nil)
}
case USER_COUNT:
event.Respond(len(r.users))
}
return
}
示例12: ProcessEvent
func (wsb *webSocketBehavior) ProcessEvent(event cells.Event) (err error) {
switch event.Topic() {
case SAYS_TO:
to, ok := event.Payload().Get("to")
if !ok || to.(string) != makeUserID(wsb.user) {
return
}
msg, err := json.Marshal(payloadToValues(event.Payload()))
if err != nil {
log.Fatal(err)
}
wsb.output <- msg
case SAYS_ALL:
msg, err := json.Marshal(payloadToValues(event.Payload()))
if err != nil {
log.Fatal(err)
}
wsb.output <- msg
}
return
}
示例13: ProcessEvent
func (b *emitBehavior) ProcessEvent(event cells.Event) error {
return b.ctx.EmitNew(sleepTopic, event.Payload(), nil)
}