From be8414521ebf815ee228c06ca62e6823be5aa120 Mon Sep 17 00:00:00 2001 From: Nicolas Martin Date: Thu, 20 Feb 2020 18:37:24 +0100 Subject: [PATCH] Bees can now create Events Bees can now create Events based on the name and a map containing the Placeholders with Names and Values. To do this we use the Event&Placeholder-Descriptor information which given in the Bees Factory Namespace. This will lead to a much shorter declaration of occuring Events in the Bees implementation. --- bees/bees.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/bees/bees.go b/bees/bees.go index 31f407e7..b13a99ac 100644 --- a/bees/bees.go +++ b/bees/bees.go @@ -16,6 +16,7 @@ * * Authors: * Christian Muehlhaeuser + * Nicolas Martin */ // Package bees is Beehive's central module system. @@ -325,6 +326,42 @@ func (bee *Bee) LogEvent() { bee.lastEvent = time.Now() } +// CreateEvent creates an Event for the given name and fills it's Placeholders +// from the given map. This map should be structured like this: +// +// properties := map[string]interface{}{ +// "event0": val0, +// "event1": val1, +// "event2": val2, +// // ... +// } +// +// whereas the map keys correspond to the placeholders name as string and the map +// values correspond to the desired values for the specific placeholder. +func (bee *Bee) CreateEvent(name string, prop map[string]interface{}) Event { + event := Event{ + Bee: bee.Name(), + Name: name, + Options: []Placeholder{}, + } + + // iterate over the Events provided in the Namespace of the bee and match on + // the provided name + for _, ev := range (*factories[bee.Namespace()]).Events() { + if ev.Name == name { + // and create the corresponding Placholder for the Event + for _, pl := range ev.Options { + event.Options = append(event.Options, Placeholder{ + Name: pl.Name, + Value: prop[pl.Name], + Type: pl.Type, + }) + } + } + } + return event +} + // LogAction logs the last triggered action. func (bee *Bee) LogAction() { bee.lastAction = time.Now()