Skip to content
Snippets Groups Projects
Commit abf53fa3 authored by Francé Wilke's avatar Francé Wilke
Browse files

Add SQS handler

parent 1a27faeb
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,11 @@ type Handler struct { ...@@ -13,6 +13,11 @@ type Handler struct {
FuncValue reflect.Value FuncValue reflect.Value
} }
type SQSHandler struct {
RecordType reflect.Type
FuncValue reflect.Value
}
func NewHandler(handlerFunction interface{}) (Handler, error) { func NewHandler(handlerFunction interface{}) (Handler, error) {
h := Handler{} h := Handler{}
...@@ -81,3 +86,29 @@ func validateStructType(t reflect.Type) error { ...@@ -81,3 +86,29 @@ func validateStructType(t reflect.Type) error {
} }
return nil return nil
} }
func NewSQSHandler(fnc interface{}) (SQSHandler, error) {
h := SQSHandler{}
fncType := reflect.TypeOf(fnc)
if fncType.NumIn() != 1 {
return h, errors.Errorf("takes %d args instead of (Record)", fncType.NumIn())
}
if fncType.NumOut() != 1 {
return h, errors.Errorf("returns %d results instead of (error)", fncType.NumOut())
}
// Arg[0] must be a struct for the message record body.
if fncType.In(0).Kind() != reflect.Struct {
return h, errors.Errorf("first arg %v is not valid record struct type", fncType.In(0))
}
h.RecordType = fncType.In(0)
// Result must be error
if _, ok := reflect.New(fncType.Out(0)).Interface().(*error); !ok {
return h, errors.Errorf("result %v is not error type", fncType.Out(0))
}
h.FuncValue = reflect.ValueOf(fnc)
return h, nil
}
...@@ -23,13 +23,13 @@ func ValidateSQSEndpoints(endpoints map[string]interface{}) (map[string]interfac ...@@ -23,13 +23,13 @@ func ValidateSQSEndpoints(endpoints map[string]interface{}) (map[string]interfac
// ok - leave as is - we support this legacyHandler // ok - leave as is - we support this legacyHandler
countLegacy++ countLegacy++
} else { } else {
handler, err := NewHandler(handlerFunc) handler, err := NewSQSHandler(handlerFunc)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "%v has invalid handler %T", messageType, handlerFunc) return nil, errors.Wrapf(err, "%v has invalid handler %T", messageType, handlerFunc)
} }
// replace the endpoint value so we can quickly call this handler // replace the endpoint value so we can quickly call this handler
endpoints[messageType] = handler endpoints[messageType] = handler
logs.Info("%s: OK (params: %v, request: %v)\n", messageType, handler.RequestParamsType, handler.RequestBodyType) logs.Info("%s: OK (request: %v)\n", messageType, handler.RecordType)
countHandler++ countHandler++
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment