forked from bit4bit/gami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
49 lines (41 loc) · 1.09 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Package event decoder
//This Build Type of Event received
package event
import (
"fmt"
"reflect"
"strconv"
"github.com/deknowhobz/gami"
)
// eventTrap used internal for trap events and cast
var eventTrap = make(map[string]interface{})
//New build a new event Type if not return the AMIEvent
func New(event *gami.AMIEvent) interface{} {
if intf, ok := eventTrap[event.ID]; ok {
return build(event, &intf)
}
return *event
}
func build(event *gami.AMIEvent, klass *interface{}) interface{} {
typ := reflect.TypeOf(*klass)
value := reflect.ValueOf(*klass)
ret := reflect.New(typ).Elem()
for ix := 0; ix < value.NumField(); ix++ {
field := ret.Field(ix)
tfield := typ.Field(ix)
if tfield.Name == "Privilege" {
field.Set(reflect.ValueOf(event.Privilege))
continue
}
switch field.Kind() {
case reflect.String:
field.SetString(event.Params[tfield.Tag.Get("AMI")])
case reflect.Int64:
vint, _ := strconv.Atoi(event.Params[tfield.Tag.Get("AMI")])
field.SetInt(int64(vint))
default:
fmt.Print(ix, tfield.Tag.Get("AMI"), ":", field, "\n")
}
}
return ret.Interface()
}