-
Notifications
You must be signed in to change notification settings - Fork 108
/
common.go
124 lines (110 loc) · 3.75 KB
/
common.go
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
120
121
122
123
124
package rabbithole
import (
"encoding/json"
"errors"
"strconv"
)
// Properties are extra arguments as a map (on queues, bindings, etc)
type Properties map[string]interface{}
// Port used by RabbitMQ or clients
type Port int
// UnmarshalJSON deserialises a port that can be an integer or string
func (p *Port) UnmarshalJSON(b []byte) error {
stringValue := string(b)
var parsed int64
var err error
if stringValue == `"undefined"` {
parsed = 0
} else if stringValue[0] == '"' && stringValue[len(stringValue)-1] == '"' {
parsed, err = strconv.ParseInt(stringValue[1:len(stringValue)-1], 10, 32)
} else {
parsed, err = strconv.ParseInt(stringValue, 10, 32)
}
if err == nil {
*p = Port(int(parsed))
}
return err
}
// RateDetailSample single touple
type RateDetailSample struct {
Sample int64 `json:"sample"`
Timestamp int64 `json:"timestamp"`
}
// RateDetails fields represent rate of change of a numerical value
type RateDetails struct {
Rate float32 `json:"rate"`
Samples []RateDetailSample `json:"samples"`
}
// BrokerContext represents a context (Erlang application) running on a node
// a node
type BrokerContext struct {
Node string `json:"node"`
Description string `json:"description"`
Path string `json:"path"`
Port Port `json:"port"`
Ignore bool `json:"ignore_in_use"`
}
// MessageStats fields repsent a number of metrics related to published messages
type MessageStats struct {
Publish int64 `json:"publish"`
PublishDetails RateDetails `json:"publish_details"`
Deliver int64 `json:"deliver"`
DeliverDetails RateDetails `json:"deliver_details"`
DeliverNoAck int64 `json:"deliver_noack"`
DeliverNoAckDetails RateDetails `json:"deliver_noack_details"`
DeliverGet int64 `json:"deliver_get"`
DeliverGetDetails RateDetails `json:"deliver_get_details"`
Redeliver int64 `json:"redeliver"`
RedeliverDetails RateDetails `json:"redeliver_details"`
Get int64 `json:"get"`
GetDetails RateDetails `json:"get_details"`
GetNoAck int64 `json:"get_no_ack"`
GetNoAckDetails RateDetails `json:"get_no_ack_details"`
Ack int64 `json:"ack"`
AckDetails RateDetails `json:"ack_details"`
ReturnUnroutable int64 `json:"return_unroutable"`
ReturnUnroutableDetails RateDetails `json:"return_unroutable_details"`
DropUnroutable int64 `json:"drop_unroutable"`
DropUnroutableDetails RateDetails `json:"drop_unroutable_details"`
}
// URISet represents a set of URIs used by Shovel, Federation, and so on.
// The URIs from this set are tried until one of them succeeds
// (a shovel or federation link successfully connects and authenticates with it)
type URISet []string
// UnmarshalJSON can unmarshal a single URI string or a list of
// URI strings
func (s *URISet) UnmarshalJSON(b []byte) error {
// the value is a single URI, a string
if b[0] == '"' {
var uri string
if err := json.Unmarshal(b, &uri); err != nil {
return err
}
*s = []string{uri}
return nil
}
// the value is a list
var uris []string
if err := json.Unmarshal(b, &uris); err != nil {
return err
}
*s = uris
return nil
}
// AutoDelete is a boolean but RabbitMQ may return the string "undefined"
type AutoDelete bool
// UnmarshalJSON can unmarshal a string or a boolean
func (d *AutoDelete) UnmarshalJSON(b []byte) error {
switch string(b) {
case "\"undefined\"":
// auto_delete is "undefined", map it to false
*d = AutoDelete(false)
case "true":
*d = AutoDelete(true)
case "false":
*d = AutoDelete(false)
default:
return errors.New("Unknown value of auto_delete")
}
return nil
}