/
type_hostport.go
63 lines (49 loc) · 1.13 KB
/
type_hostport.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
package config
import (
"fmt"
"net"
"strconv"
)
type TypeHostPort struct {
Value string
Host string
Port uint
}
func (t *TypeHostPort) Set(value string) error {
host, port, err := net.SplitHostPort(value)
if err != nil {
return fmt.Errorf("incorrect host:port value (%v): %w", value, err)
}
portValue, err := strconv.ParseUint(port, 10, 16) //nolint: gomnd
if err != nil {
return fmt.Errorf("incorrect port number (%v): %w", value, err)
}
if portValue == 0 {
return fmt.Errorf("incorrect port number (%s)", value)
}
if host == "" {
return fmt.Errorf("empty host: %s", value)
}
if net.ParseIP(host) == nil {
return fmt.Errorf("host is not an IP address: %s", value)
}
t.Value = net.JoinHostPort(host, port)
t.Port = uint(portValue)
t.Host = host
return nil
}
func (t TypeHostPort) Get(defaultValue string) string {
if t.Value == "" {
return defaultValue
}
return t.Value
}
func (t *TypeHostPort) UnmarshalText(data []byte) error {
return t.Set(string(data))
}
func (t TypeHostPort) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (t TypeHostPort) String() string {
return t.Value
}