-
Notifications
You must be signed in to change notification settings - Fork 405
/
config.go
197 lines (167 loc) · 3.75 KB
/
config.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package models
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"net/http"
"strconv"
)
type Config map[string]string
func (c *Config) Validate() error {
return nil
}
func (c1 Config) Equals(c2 Config) bool {
if len(c1) != len(c2) {
return false
}
for k1, v1 := range c1 {
if v2, ok := c2[k1]; !ok || v1 != v2 {
return false
}
}
return true
}
// implements sql.Valuer, returning a string
func (c Config) Value() (driver.Value, error) {
if len(c) < 1 {
return driver.Value(string("")), nil
}
var b bytes.Buffer
err := json.NewEncoder(&b).Encode(c)
// return a string type
return driver.Value(b.String()), err
}
// implements sql.Scanner
func (c *Config) Scan(value interface{}) error {
if value == nil {
*c = nil
return nil
}
bv, err := driver.String.ConvertValue(value)
if err == nil {
var b []byte
switch x := bv.(type) {
case []byte:
b = x
case string:
b = []byte(x)
}
if len(b) > 0 {
return json.Unmarshal(b, c)
}
*c = nil
return nil
}
// otherwise, return an error
return fmt.Errorf("config invalid db format: %T %T value, err: %v", value, bv, err)
}
// Headers is an http.Header that implements additional methods.
type Headers http.Header
func (h1 Headers) Equals(h2 Headers) bool {
if len(h1) != len(h2) {
return false
}
for k1, v1s := range h1 {
v2s, _ := h2[k1]
if len(v2s) != len(v1s) {
return false
}
for i, v1 := range v1s {
if v2s[i] != v1 {
return false
}
}
}
return true
}
// implements sql.Valuer, returning a string
func (h Headers) Value() (driver.Value, error) {
if len(h) < 1 {
return driver.Value(string("")), nil
}
var b bytes.Buffer
err := json.NewEncoder(&b).Encode(h)
// return a string type
return driver.Value(b.String()), err
}
// implements sql.Scanner
func (h *Headers) Scan(value interface{}) error {
if value == nil {
*h = nil
return nil
}
bv, err := driver.String.ConvertValue(value)
if err == nil {
var b []byte
switch x := bv.(type) {
case []byte:
b = x
case string:
b = []byte(x)
}
if len(b) > 0 {
return json.Unmarshal(b, h)
}
*h = nil
return nil
}
// otherwise, return an error
return fmt.Errorf("headers invalid db format: %T %T value, err: %v", value, bv, err)
}
// MilliCPU units
type MilliCPUs uint64
const (
MinMilliCPUs = 0 // 0 is unlimited
MaxMilliCPUs = 1024000 // 1024 CPUs
)
// implements fmt.Stringer
func (c MilliCPUs) String() string {
if c == 0 {
return ""
}
return fmt.Sprintf("%dm", c)
}
// implements json.Unmarshaler
func (c *MilliCPUs) UnmarshalJSON(data []byte) error {
outer := bytes.TrimSpace(data)
if bytes.Equal(outer, []byte("null")) {
*c = MilliCPUs(0)
return nil
}
if !bytes.HasSuffix(outer, []byte("\"")) || !bytes.HasPrefix(outer, []byte("\"")) {
return ErrInvalidJSON
}
outer = bytes.TrimPrefix(outer, []byte("\""))
outer = bytes.TrimSuffix(outer, []byte("\""))
outer = bytes.TrimSpace(outer)
if len(outer) == 0 {
*c = 0
return nil
}
if bytes.HasSuffix(outer, []byte("m")) {
// Support milli cores as "100m"
outer = bytes.TrimSuffix(outer, []byte("m"))
mCPU, err := strconv.ParseUint(string(outer), 10, 64)
if err != nil || mCPU > MaxMilliCPUs || mCPU < MinMilliCPUs {
return ErrInvalidCPUs
}
*c = MilliCPUs(mCPU)
} else {
// Support for floating point "0.1" style CPU units
fCPU, err := strconv.ParseFloat(string(outer), 64)
if err != nil || fCPU < MinMilliCPUs/1000 || fCPU > MaxMilliCPUs/1000 {
return ErrInvalidCPUs
}
*c = MilliCPUs(fCPU * 1000)
}
return nil
}
// implements json.Marshaler
func (c *MilliCPUs) MarshalJSON() ([]byte, error) {
if *c < MinMilliCPUs || *c > MaxMilliCPUs {
return nil, ErrInvalidCPUs
}
// always use milli cpus "1000m" format
return []byte(fmt.Sprintf("\"%s\"", c.String())), nil
}