Add utility methods for handling queue handler results and payload mapping
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@ func StructToMap(data interface{}) (JSONB, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MapToStruct(data JSONB, result interface{}) error {
|
func MapToStruct(data JSONB, result any) error {
|
||||||
// Marshal map to JSON bytes
|
// Marshal map to JSON bytes
|
||||||
jsonBytes, err := json.Marshal(data)
|
jsonBytes, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -33,11 +33,71 @@ type GeneralQueueJob struct {
|
|||||||
ResultPayload map[string]interface{}
|
ResultPayload map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (j GeneralQueueJob) LoadPayloadStruct(target any) error {
|
||||||
|
return MapToStruct(j.Payload, target)
|
||||||
|
}
|
||||||
|
|
||||||
type GeneralQueueHandlerResult struct {
|
type GeneralQueueHandlerResult struct {
|
||||||
ResultPayload map[string]interface{}
|
ResultPayload map[string]interface{}
|
||||||
FailurePayload 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 GeneralQueueHandler func(ctx context.Context, job GeneralQueueJob) (GeneralQueueHandlerResult, error)
|
||||||
|
|
||||||
type GeneralQueueParams struct {
|
type GeneralQueueParams struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user