v1.16.7
ADDED: * uuidx FIXED: * sprigx docs consistency
This commit is contained in:
82
tplx/sprigx/funcs_tpl_net.go
Normal file
82
tplx/sprigx/funcs_tpl_net.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package sprigx
|
||||
|
||||
import (
|
||||
`math`
|
||||
`net`
|
||||
`strconv`
|
||||
)
|
||||
|
||||
// netExtractAddr calls net.ParseCIDR and returns the net.IP from it.
|
||||
func netExtractAddr(s string) (addr net.IP, err error) {
|
||||
|
||||
if addr, _, err = net.ParseCIDR(s); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// netExtractHost extracts the host component from hostPort.
|
||||
func netExtractHost(hostPort string) (host string, err error) {
|
||||
|
||||
if host, _, err = net.SplitHostPort(hostPort); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// netExtractIpnet calls net.ParseCIDR and returns the net.IPNet from it.
|
||||
func netExtractIpnet(s string) (ipnet *net.IPNet, err error) {
|
||||
|
||||
if _, ipnet, err = net.ParseCIDR(s); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// netExtractPort extracts the port component from hostPort.
|
||||
func netExtractPort(hostPort string) (port uint16, err error) {
|
||||
|
||||
var portStr string
|
||||
var u64 uint64
|
||||
|
||||
if _, portStr, err = net.SplitHostPort(hostPort); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if u64, err = strconv.ParseUint(portStr, 10, 16); err != nil {
|
||||
return
|
||||
}
|
||||
port = uint16(u64)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// netIp4Mask is a more flexible wrapper around net.IPv4Mask.
|
||||
func netIp4Mask(a, b, c, d any) (mask net.IPMask, err error) {
|
||||
|
||||
var idx int
|
||||
var elem any
|
||||
var elemInt int
|
||||
var mBytes [4]byte
|
||||
var orig [4]any = [4]any{a, b, c, d}
|
||||
|
||||
for idx, elem = range orig {
|
||||
if elemInt, _, err = toPosInt(elem); err != nil {
|
||||
return
|
||||
}
|
||||
if elemInt > math.MaxUint8 {
|
||||
err = ErrOverflow
|
||||
return
|
||||
}
|
||||
mBytes[idx] = byte(uint8(elemInt))
|
||||
}
|
||||
|
||||
mask = net.IPv4Mask(
|
||||
mBytes[0], mBytes[1], mBytes[2], mBytes[3],
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user