-
Notifications
You must be signed in to change notification settings - Fork 1
/
method.go
54 lines (44 loc) · 870 Bytes
/
method.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
package immune
import (
"strings"
"github.com/pkg/errors"
)
type Method string
const (
MethodPost Method = "POST"
MethodPUT Method = "PUT"
MethodGet Method = "GET"
MethodPatch Method = "PATCH"
MethodHead Method = "HEAD"
MethodDelete Method = "DELETE"
MethodConnect Method = "CONNECT"
MethodOptions Method = "OPTIONS"
MethodTrace Method = "TRACE"
)
func (m Method) IsValid() bool {
switch m {
case MethodPost,
MethodPUT,
MethodGet,
MethodPatch,
MethodHead,
MethodDelete,
MethodConnect,
MethodOptions,
MethodTrace:
return true
default:
return false
}
}
func (m Method) String() string {
return string(m)
}
func (m *Method) UnmarshalJSON(b []byte) error {
str := strings.Trim(string(b), `"`)
*m = Method(str)
if !m.IsValid() {
return errors.Errorf("unknown http method %s", m.String())
}
return nil
}