From abf53fa3442a551fadad462019e4ad36df1b6e79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?France=CC=81=20Wilke?= <francewilke@gmail.com>
Date: Tue, 30 Nov 2021 13:53:29 +0200
Subject: [PATCH] Add SQS handler

---
 handler_utils/handler.go | 31 +++++++++++++++++++++++++++++++
 handler_utils/sqs.go     |  4 ++--
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/handler_utils/handler.go b/handler_utils/handler.go
index 0268f78..89c8c8a 100644
--- a/handler_utils/handler.go
+++ b/handler_utils/handler.go
@@ -13,6 +13,11 @@ type Handler struct {
 	FuncValue         reflect.Value
 }
 
+type SQSHandler struct {
+	RecordType reflect.Type
+	FuncValue  reflect.Value
+}
+
 func NewHandler(handlerFunction interface{}) (Handler, error) {
 	h := Handler{}
 
@@ -81,3 +86,29 @@ func validateStructType(t reflect.Type) error {
 	}
 	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
+}
diff --git a/handler_utils/sqs.go b/handler_utils/sqs.go
index fe14526..fdc8a90 100644
--- a/handler_utils/sqs.go
+++ b/handler_utils/sqs.go
@@ -23,13 +23,13 @@ func ValidateSQSEndpoints(endpoints map[string]interface{}) (map[string]interfac
 			// ok - leave as is - we support this legacyHandler
 			countLegacy++
 		} else {
-			handler, err := NewHandler(handlerFunc)
+			handler, err := NewSQSHandler(handlerFunc)
 			if err != nil {
 				return nil, errors.Wrapf(err, "%v has invalid handler %T", messageType, handlerFunc)
 			}
 			// replace the endpoint value so we can quickly call this 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++
 		}
 	}
-- 
GitLab