-
Notifications
You must be signed in to change notification settings - Fork 20
/
convert.go
56 lines (46 loc) · 1004 Bytes
/
convert.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
package convert
import (
"errors"
"net"
"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
"github.com/Asutorufa/yuhaiin/pkg/protos/statistic"
)
func ToStringMap(addr netapi.Store) map[string]string {
maps := addr.Map()
r := make(map[string]string, len(maps))
for k, v := range maps {
kk, ok := ToString(k)
if !ok {
continue
}
vv, ok := ToString(v)
if !ok {
continue
}
r[kk] = vv
}
return r
}
func ToString(t any) (string, bool) {
switch z := t.(type) {
case string:
return z, true
case interface{ String() string }:
return z.String(), true
default:
return "", false
}
}
func ToProxyAddress(netType statistic.Type, t any) (dstAddr netapi.Address, err error) {
switch z := t.(type) {
case net.Addr:
dstAddr, err = netapi.ParseSysAddr(z)
case string:
dstAddr, err = netapi.ParseAddress(netType, z)
case interface{ String() string }:
dstAddr, err = netapi.ParseAddress(netType, z.String())
default:
err = errors.New("unsupported type")
}
return
}