forked from ehazlett/stellar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
65 lines (58 loc) · 1.17 KB
/
utils.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
package node
import (
"encoding/json"
"fmt"
"strings"
)
func (m *Endpoint) UnmarshalJSON(data []byte) error {
var v map[string]interface{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
for k, v := range v {
switch k {
case "service":
m.Service = v.(string)
case "host":
m.Host = v.(string)
case "port":
m.Port = uint32(v.(float64))
case "tls":
m.TLS = v.(bool)
case "protocol":
p, err := parseProtocol(v)
if err != nil {
return err
}
m.Protocol = p
}
}
return nil
}
func parseProtocol(v interface{}) (Protocol, error) {
if v, ok := v.(string); ok {
switch strings.ToLower(v) {
case "tcp":
return Protocol_TCP, nil
case "udp":
return Protocol_UDP, nil
case "http":
return Protocol_HTTP, nil
default:
return Protocol_UNKNOWN, fmt.Errorf("unknown protocol %s", v)
}
}
if v, ok := v.(float64); ok {
switch v {
case float64(0):
return Protocol_TCP, nil
case float64(1):
return Protocol_UDP, nil
case float64(2):
return Protocol_HTTP, nil
default:
return Protocol_UNKNOWN, fmt.Errorf("unknown protocol %v", v)
}
}
return Protocol_UNKNOWN, fmt.Errorf("unknown protocol %v", v)
}