87 lines
1.3 KiB
Go
87 lines
1.3 KiB
Go
package tunnelbroker
|
|
|
|
import (
|
|
`database/sql/driver`
|
|
`net/netip`
|
|
)
|
|
|
|
// MarshalText returns a text representation (as bytes) of a TunPrefix.
|
|
func (t *TunPrefix) MarshalText() (b []byte, err error) {
|
|
|
|
if t == nil {
|
|
return
|
|
}
|
|
|
|
b = []byte(t.ToPrefix().String())
|
|
|
|
return
|
|
}
|
|
|
|
// Scan conforms a TunPrefix to a sql.Scanner. It populates t with val.
|
|
func (t *TunPrefix) Scan(val interface{}) (err error) {
|
|
|
|
var pfx netip.Prefix
|
|
var s string
|
|
|
|
if val == nil {
|
|
return
|
|
}
|
|
|
|
switch v := val.(type) {
|
|
case string:
|
|
s = v
|
|
case []byte:
|
|
s = string(v)
|
|
default:
|
|
err = ErrBadPrefixValue
|
|
return
|
|
}
|
|
|
|
if pfx, err = netip.ParsePrefix(s); err != nil {
|
|
return
|
|
}
|
|
|
|
*t = TunPrefix(pfx)
|
|
|
|
return
|
|
}
|
|
|
|
// ToPrefix returns a netip.Prefix from a TunPrefix.
|
|
func (t *TunPrefix) ToPrefix() (pfx *netip.Prefix) {
|
|
|
|
if t == nil {
|
|
return
|
|
}
|
|
|
|
pfx = new(netip.Prefix)
|
|
*pfx = netip.Prefix(*t)
|
|
|
|
return
|
|
}
|
|
|
|
// UnmarshalText populates a TunPrefix from a text representation.
|
|
func (t *TunPrefix) UnmarshalText(b []byte) (err error) {
|
|
|
|
var pfx netip.Prefix
|
|
|
|
if b == nil || len(b) == 0 {
|
|
return
|
|
}
|
|
|
|
if pfx, err = netip.ParsePrefix(string(b)); err != nil {
|
|
return
|
|
}
|
|
|
|
*t = TunPrefix(pfx)
|
|
|
|
return
|
|
}
|
|
|
|
// Value conforms a TunPrefix to a sql/driver.Valuer interface. It returns val from t.
|
|
func (t TunPrefix) Value() (val driver.Value, err error) {
|
|
|
|
val = t.ToPrefix().String()
|
|
|
|
return
|
|
}
|