FILE / ScuroNeko/mtg

config/type_ip.go

Исходный файл и его история в репозитории.
FILE 2cc81219b179f7bf0bb582850a0e7568ffda0346
Files
mtg/config/type_ip.go
T

46 lines
617 B
Go

package config
import (
"fmt"
"net"
)
type TypeIP struct {
value net.IP
}
func (c *TypeIP) UnmarshalText(data []byte) error {
if len(data) == 0 {
return nil
}
ip := net.ParseIP(string(data))
if ip == nil {
return fmt.Errorf("incorrect ip address: %s", string(data))
}
c.value = ip
return nil
}
func (c *TypeIP) MarshalText() ([]byte, error) {
return []byte(c.String()), nil
}
func (c TypeIP) String() string {
if len(c.value) > 0 {
return c.value.String()
}
return ""
}
func (c TypeIP) Value(defaultValue net.IP) net.IP {
if c.value == nil {
return defaultValue
}
return c.value
}