-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcommand.go
More file actions
119 lines (103 loc) · 3.13 KB
/
command.go
File metadata and controls
119 lines (103 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package kubemq
import (
"fmt"
"time"
)
// Command is an outbound command request. It is NOT safe for concurrent use —
// create a new Command for each send operation.
type Command struct {
Id string
Channel string
Metadata string
Body []byte
Timeout time.Duration
ClientId string
Tags map[string]string
}
// NewCommand creates an empty Command.
func NewCommand() *Command {
return &Command{}
}
// SetId sets the command request ID.
func (c *Command) SetId(id string) *Command {
c.Id = id
return c
}
// SetClientId sets the client identifier for this command.
func (c *Command) SetClientId(clientId string) *Command {
c.ClientId = clientId
return c
}
// SetChannel sets the target channel for this command.
func (c *Command) SetChannel(channel string) *Command {
c.Channel = channel
return c
}
// SetMetadata sets the command metadata.
func (c *Command) SetMetadata(metadata string) *Command {
c.Metadata = metadata
return c
}
// SetBody sets the command body payload.
func (c *Command) SetBody(body []byte) *Command {
c.Body = body
return c
}
// SetTimeout sets the timeout for waiting for a command response.
func (c *Command) SetTimeout(timeout time.Duration) *Command {
c.Timeout = timeout
return c
}
// SetTags replaces all tags on this command.
func (c *Command) SetTags(tags map[string]string) *Command {
c.Tags = make(map[string]string, len(tags))
for key, value := range tags {
c.Tags[key] = value
}
return c
}
// AddTag adds a single key-value tag to this command.
func (c *Command) AddTag(key, value string) *Command {
if c.Tags == nil {
c.Tags = map[string]string{}
}
c.Tags[key] = value
return c
}
// Validate checks all required fields and constraints.
// Called automatically before send operations; can also be called explicitly.
func (c *Command) Validate() error {
return validateCommand(c, nil)
}
// CommandReceive is a received command request delivered to subscription
// callbacks. It is safe to read from multiple goroutines but must not be
// modified after receipt.
type CommandReceive struct {
Id string
ClientId string
Channel string
Metadata string
Body []byte
ResponseTo string
Tags map[string]string
}
// String returns a human-readable representation of the received command.
func (cr *CommandReceive) String() string {
return fmt.Sprintf("Id: %s, ClientId: %s, Channel: %s, Metadata: %s, Body: %s, ResponseTo: %s, Tags: %v",
cr.Id, cr.ClientId, cr.Channel, cr.Metadata, string(cr.Body), cr.ResponseTo, cr.Tags)
}
// CommandResponse contains the result of a command execution.
// Immutable after construction. Safe to read from multiple goroutines.
type CommandResponse struct {
CommandId string
ResponseClientId string
Executed bool
ExecutedAt time.Time
Error string
Tags map[string]string
}
// String returns a human-readable representation of the command response.
func (cr *CommandResponse) String() string {
return fmt.Sprintf("CommandId: %s, ResponseClientId: %s, Executed: %v, ExecutedAt: %s, Error: %s, Tags: %v",
cr.CommandId, cr.ResponseClientId, cr.Executed, cr.ExecutedAt.String(), cr.Error, cr.Tags)
}