/
config.go
71 lines (59 loc) · 1.35 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
package messagebroker
import (
"fmt"
"strconv"
)
type PulsarAuth int
const (
AUTH_SIMPLE PulsarAuth = iota
AUTH_TOKEN
AUTH_OAUTH2
_maxAuthid
)
var PulsarAuthName = map[int]string{
0: "simple",
1: "token",
2: "oauth2",
}
var PulsarAuthValue = map[string]PulsarAuth{
PulsarAuthName[0]: AUTH_SIMPLE,
PulsarAuthName[1]: AUTH_TOKEN,
PulsarAuthName[2]: AUTH_OAUTH2,
}
func (p PulsarAuth) String() string {
s, ok := PulsarAuthName[int(p)]
if ok {
return s
}
return strconv.Itoa(int(p))
}
// UnmarshalJSON unmarshals b into PulsarAuth.
func (p *PulsarAuth) UnmarshalJSON(b []byte) error {
// From json.Unmarshaler: By convention, to approximate the behavior of
// Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as
// a no-op.
if string(b) == "null" {
return nil
}
if p == nil {
return fmt.Errorf("nil receiver passed to UnmarshalJSON")
}
if ci, err := strconv.ParseUint(string(b), 10, 32); err == nil {
if ci >= uint64(_maxAuthid) {
return fmt.Errorf("invalid code: %q", ci)
}
*p = PulsarAuth(ci)
return nil
}
if ev, ok := PulsarAuthValue[string(b)]; ok {
*p = ev
return nil
}
return fmt.Errorf("invalid code: %q", string(b))
}
func (p *PulsarAuth) Unmarshal(data string) error {
return p.UnmarshalJSON([]byte(data))
}
func (p *PulsarAuth) UnmarshalText(text []byte) error {
return p.UnmarshalJSON(text)
}