-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproperty.go
139 lines (122 loc) · 2.87 KB
/
property.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package lmq
import (
"bytes"
"encoding/json"
"net/http"
"regexp"
"time"
)
func NewProperty() *Property {
p := new(Property)
p.init()
return p
}
type Property struct {
Accum time.Duration
Retry int
Timeout time.Duration
}
func (p *Property) init() {
p.Accum = -1
p.Retry = -1
p.Timeout = -1
}
func (p *Property) MarshalJSON() ([]byte, error) {
v := make(map[string]interface{})
if p.Accum >= 0 {
v["accum"] = p.Accum.Seconds()
}
if p.Retry >= 0 {
v["retry"] = p.Retry
}
if p.Timeout >= 0 {
v["timeout"] = p.Timeout.Seconds()
}
return json.Marshal(v)
}
func (p *Property) UnmarshalJSON(b []byte) error {
var v map[string]interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
p.init()
if accum, ok := v["accum"].(float64); ok {
p.Accum = time.Duration(accum * 1e+9)
}
if retry, ok := v["retry"].(float64); ok {
p.Retry = int(retry)
}
if timeout, ok := v["timeout"].(float64); ok {
p.Timeout = time.Duration(timeout * 1e+9)
}
return nil
}
type DefaultProperty struct {
Pattern string
Property *Property
}
func (p *DefaultProperty) MarshalJSON() ([]byte, error) {
if _, err := regexp.Compile(p.Pattern); err != nil {
return nil, err
}
return json.Marshal([]interface{}{p.Pattern, p.Property})
}
func (p *DefaultProperty) UnmarshalJSON(b []byte) error {
var v []json.RawMessage
if err := json.Unmarshal(b, &v); err != nil {
return err
}
if err := json.Unmarshal(v[0], &p.Pattern); err != nil {
return err
}
p.Property = new(Property)
if err := json.Unmarshal(v[1], p.Property); err != nil {
return err
}
return nil
}
func (c *client) GetProperty(queue string) (*Property, error) {
var p Property
err := c.getProperty("/properties/"+queue, &p)
return &p, err
}
func (c *client) UpdateProperty(queue string, p *Property) error {
return c.setProperty("PATCH", "/properties/"+queue, p)
}
func (c *client) DeleteProperty(queue string) error {
return c.delete("/properties/" + queue)
}
func (c *client) GetDefaultProperty() ([]*DefaultProperty, error) {
var p []*DefaultProperty
err := c.getProperty("/properties", &p)
return p, err
}
func (c *client) SetDefaultProperty(props []*DefaultProperty) error {
return c.setProperty("PUT", "/properties", props)
}
func (c *client) DeleteDefaultProperty() error {
return c.delete("/properties")
}
func (c *client) getProperty(url string, r interface{}) error {
resp, err := c.do("GET", url, "", nil)
if err != nil {
return err
}
defer discard(resp.Body)
if err := checkStatus(resp, http.StatusOK); err != nil {
return err
}
return json.NewDecoder(resp.Body).Decode(&r)
}
func (c *client) setProperty(method, url string, v interface{}) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
resp, err := c.do(method, url, "application/json", bytes.NewReader(b))
if err != nil {
return err
}
defer discard(resp.Body)
return checkStatus(resp, http.StatusNoContent)
}