mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-15 11:50:39 +08:00
feat: add example
This commit is contained in:
40
options.go
40
options.go
@@ -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
|
||||
|
Reference in New Issue
Block a user