Add utility methods for handling queue handler results and payload mapping

This commit is contained in:
Achim Rohn
2026-04-08 14:50:45 +02:00
parent f49871cc1c
commit a383129abb
2 changed files with 61 additions and 1 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ func StructToMap(data interface{}) (JSONB, error) {
return result, nil
}
func MapToStruct(data JSONB, result interface{}) error {
func MapToStruct(data JSONB, result any) error {
// Marshal map to JSON bytes
jsonBytes, err := json.Marshal(data)
if err != nil {
+60
View File
@@ -33,11 +33,71 @@ type GeneralQueueJob struct {
ResultPayload map[string]interface{}
}
func (j GeneralQueueJob) LoadPayloadStruct(target any) error {
return MapToStruct(j.Payload, target)
}
type GeneralQueueHandlerResult struct {
ResultPayload map[string]interface{}
FailurePayload map[string]interface{}
}
func NewSuccessGeneralQueueHandlerResult(result any) (GeneralQueueHandlerResult, error) {
resultMap, err := StructToMap(result)
if err != nil {
return returnStructToMapError(err), err
}
return GeneralQueueHandlerResult{
ResultPayload: resultMap,
FailurePayload: nil,
}, nil
}
const structToMapErrorKey = "structToMapError"
func returnStructToMapError(err error) GeneralQueueHandlerResult {
return GeneralQueueHandlerResult{
FailurePayload: map[string]interface{}{
structToMapErrorKey: err.Error(),
},
}
}
func FailureGeneralQueueHandlerResult(message string, err error) GeneralQueueHandlerResult {
return GeneralQueueHandlerResult{
FailurePayload: map[string]interface{}{
"message": message,
"error": err.Error(),
},
}
}
func NewFailureGeneralQueueHandlerResult(failure any) (GeneralQueueHandlerResult, error) {
failureMap, err := StructToMap(failure)
if err != nil {
return returnStructToMapError(err), err
}
return GeneralQueueHandlerResult{
ResultPayload: nil,
FailurePayload: failureMap,
}, nil
}
func NewGeneralQueueHandlerResult(result any, failure any) (GeneralQueueHandlerResult, error) {
resultMap, err := StructToMap(result)
if err != nil {
return returnStructToMapError(err), err
}
failureMap, err := StructToMap(failure)
if err != nil {
return returnStructToMapError(err), err
}
return GeneralQueueHandlerResult{
ResultPayload: resultMap,
FailurePayload: failureMap,
}, nil
}
type GeneralQueueHandler func(ctx context.Context, job GeneralQueueJob) (GeneralQueueHandlerResult, error)
type GeneralQueueParams struct {