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

Add shared GetRecord

parent 937a8e0a
No related branches found
No related tags found
No related merge requests found
package handler_utils
import (
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"gitlab.com/uafrica/go-utils/errors"
"gitlab.com/uafrica/go-utils/logs"
"reflect"
)
// ValidateSQSEndpoints checks that all SQS endpoints are correctly defined using one of the supported handler types
......@@ -36,3 +38,23 @@ func ValidateSQSEndpoints(endpoints map[string]interface{}) (map[string]interfac
logs.Info("Checked %d legacy and %d new handlers\n", countLegacy, countHandler)
return endpoints, nil
}
func GetRecord(message events.SQSMessage, recordType reflect.Type) (interface{}, error) {
recordValuePtr := reflect.New(recordType)
err := json.Unmarshal([]byte(message.Body), recordValuePtr.Interface())
if err != nil {
return nil, errors.Wrapf(err, "failed to JSON decode message body")
}
if validator, ok := recordValuePtr.Interface().(IValidator); ok {
if err := validator.Validate(); err != nil {
return nil, errors.Wrapf(err, "invalid message body")
}
}
return recordValuePtr.Elem().Interface(), nil
}
type IValidator interface {
Validate() error
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment