feat: add example

This commit is contained in:
sujit
2024-10-08 16:24:52 +05:45
parent a4dff9c77b
commit 612eb535ec
4 changed files with 87 additions and 99 deletions

View File

@@ -3,10 +3,12 @@ package mq
import (
"context"
"encoding/json"
"fmt"
"time"
)
type Result struct {
Ctx context.Context
Payload json.RawMessage `json:"payload"`
Topic string `json:"topic"`
TaskID string `json:"task_id"`
@@ -14,6 +16,44 @@ type Result struct {
Status string `json:"status"`
}
func (r Result) Unmarshal(data any) error {
if r.Payload == nil {
return fmt.Errorf("payload is nil")
}
return json.Unmarshal(r.Payload, data)
}
func (r Result) String() string {
return string(r.Payload)
}
func HandleError(ctx context.Context, err error, status ...string) Result {
st := "Failed"
if len(status) > 0 {
st = status[0]
}
if err == nil {
return Result{}
}
return Result{
Status: st,
Error: err,
Ctx: ctx,
}
}
func (r Result) WithData(status string, data []byte) Result {
if r.Error != nil {
return r
}
return Result{
Status: status,
Payload: data,
Error: nil,
Ctx: r.Ctx,
}
}
type TLSConfig struct {
UseTLS bool
CertPath string