github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/utils/convert/convert.go (about)

     1  package convert
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  
     7  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
     8  	"github.com/Asutorufa/yuhaiin/pkg/protos/statistic"
     9  )
    10  
    11  func ToStringMap(addr netapi.Store) map[string]string {
    12  	maps := addr.Map()
    13  
    14  	r := make(map[string]string, len(maps))
    15  
    16  	for k, v := range maps {
    17  		kk, ok := ToString(k)
    18  		if !ok {
    19  			continue
    20  		}
    21  
    22  		vv, ok := ToString(v)
    23  		if !ok {
    24  			continue
    25  		}
    26  
    27  		r[kk] = vv
    28  	}
    29  
    30  	return r
    31  }
    32  
    33  func ToString(t any) (string, bool) {
    34  	switch z := t.(type) {
    35  	case string:
    36  		return z, true
    37  	case interface{ String() string }:
    38  		return z.String(), true
    39  	default:
    40  		return "", false
    41  	}
    42  }
    43  
    44  func ToProxyAddress(netType statistic.Type, t any) (dstAddr netapi.Address, err error) {
    45  	switch z := t.(type) {
    46  	case net.Addr:
    47  		dstAddr, err = netapi.ParseSysAddr(z)
    48  	case string:
    49  		dstAddr, err = netapi.ParseAddress(netType, z)
    50  	case interface{ String() string }:
    51  		dstAddr, err = netapi.ParseAddress(netType, z.String())
    52  	default:
    53  		err = errors.New("unsupported type")
    54  	}
    55  	return
    56  }