From 7b9d39d2dffe66eac0c72a635301209d43252425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?France=CC=81=20Wilke?= <francewilke@gmail.com> Date: Fri, 17 Dec 2021 08:04:52 +0200 Subject: [PATCH] Remove "legacy" handler checks --- handler_utils/api.go | 22 +++++----------------- handler_utils/sqs.go | 21 ++++++--------------- struct_utils/map_params.go | 12 +++++------- 3 files changed, 16 insertions(+), 39 deletions(-) diff --git a/handler_utils/api.go b/handler_utils/api.go index ce89f0c..4778a75 100644 --- a/handler_utils/api.go +++ b/handler_utils/api.go @@ -3,7 +3,6 @@ package handler_utils import ( "github.com/aws/aws-lambda-go/events" "gitlab.com/uafrica/go-utils/errors" - "gitlab.com/uafrica/go-utils/logs" "reflect" "strconv" "strings" @@ -12,8 +11,6 @@ import ( // ValidateAPIEndpoints checks that all API endpoints are correctly defined using one of the supported handler types // return updated endpoints with additional information func ValidateAPIEndpoints(endpoints map[string]map[string]interface{}) (map[string]map[string]interface{}, error) { - countLegacy := 0 - countHandler := 0 for resource, methodHandlers := range endpoints { if resource == "" { return nil, errors.Errorf("blank resource") @@ -35,21 +32,14 @@ func ValidateAPIEndpoints(endpoints map[string]map[string]interface{}) (map[stri return nil, errors.Errorf("nil handler on %s %s", method, resource) } - if _, ok := handlerFunc.(func(req events.APIGatewayProxyRequest) (response events.APIGatewayProxyResponse, err error)); ok { - //ok - leave as is - we support this legacyHandler - countLegacy++ - } else { - handler, err := NewHandler(handlerFunc) - if err != nil { - return nil, errors.Wrapf(err, "%s %s has invalid handler %T", method, resource, handlerFunc) - } - //replace the endpoint value so that we can quickly call this handler - endpoints[resource][method] = handler - countHandler++ + handler, err := NewHandler(handlerFunc) + if err != nil { + return nil, errors.Wrapf(err, "%s %s has invalid handler %T", method, resource, handlerFunc) } + //replace the endpoint value so that we can quickly call this handler + endpoints[resource][method] = handler } } - logs.Info("Checked %d legacy and %d new handlers\n", countLegacy, countHandler) return endpoints, nil } @@ -106,8 +96,6 @@ func ValidateRequestParams(request *events.APIGatewayProxyRequest, paramsStructT return paramsStructValuePtr, nil } - - func setParamFromStr(fieldValue reflect.Value, paramStrValue string) error { switch fieldValue.Type().Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: diff --git a/handler_utils/sqs.go b/handler_utils/sqs.go index 837ad9c..5a92358 100644 --- a/handler_utils/sqs.go +++ b/handler_utils/sqs.go @@ -11,8 +11,6 @@ import ( // ValidateSQSEndpoints checks that all SQS endpoints are correctly defined using one of the supported handler types // return updated endpoints with additional information func ValidateSQSEndpoints(endpoints map[string]interface{}) (map[string]interface{}, error) { - countLegacy := 0 - countHandler := 0 for messageType, handlerFunc := range endpoints { if messageType == "" { return nil, errors.Errorf("blank messageType") @@ -21,21 +19,14 @@ func ValidateSQSEndpoints(endpoints map[string]interface{}) (map[string]interfac return nil, errors.Errorf("nil handler on %s", messageType) } - if _, ok := handlerFunc.(func(req events.SQSEvent) (err error)); ok { - // ok - leave as is - we support this legacyHandler - countLegacy++ - } else { - 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 (request: %v)\n", messageType, handler.RecordType) - countHandler++ + 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 (request: %v)\n", messageType, handler.RecordType) } - logs.Info("Checked %d legacy and %d new handlers\n", countLegacy, countHandler) return endpoints, nil } diff --git a/struct_utils/map_params.go b/struct_utils/map_params.go index a7830fd..6609553 100644 --- a/struct_utils/map_params.go +++ b/struct_utils/map_params.go @@ -7,16 +7,15 @@ import ( "strings" ) -//convert fields in a struct to a map of parameters, as if defined in a URL -//we use this mainly for legacy functions that expect params to be defined in a map[string]string -//to convert the new params struct into such a map +// MapParams convert fields in a struct to a map of parameters, as if defined in a URL +// to convert the new params struct into a map[string]string func MapParams(data interface{}) map[string]string { params := map[string]string{} addStructParams(params, reflect.ValueOf(data)) return params } -//recursive function +// recursive function func addStructParams(params map[string]string, structValue reflect.Value) { t := structValue.Type() if t.Kind() != reflect.Struct { @@ -25,7 +24,7 @@ func addStructParams(params map[string]string, structValue reflect.Value) { for i := 0; i < t.NumField(); i++ { tf := t.Field(i) - //recurse for embedded structs + // recurse for embedded structs if tf.Anonymous { addStructParams(params, structValue.Field(i)) } else { @@ -41,8 +40,7 @@ func addStructParams(params map[string]string, structValue reflect.Value) { } } if !skip { - //lists must be written as JSON lists so they can be unmarshalled - //jsut because that is how the legacy code did it + // lists must be written as JSON lists so they can be unmarshalled if t.Field(i).Type.Kind() == reflect.Slice { jsonValue, _ := json.Marshal(structValue.Field(i).Interface()) params[jsonTags[0]] = string(jsonValue) -- GitLab