Change-Id: Ib34b84b8267c95ce3bacef4806cc52a6c7c24f05
Signed-off-by: Aloys Augustin <aloaugus@cisco.com>
copy(macaddr[:], mac[:])
return macaddr, nil
}
copy(macaddr[:], mac[:])
return macaddr, nil
}
func (x MacAddress) ToMAC() net.HardwareAddr {
return net.HardwareAddr(x[:])
}
func (x MacAddress) ToMAC() net.HardwareAddr {
return net.HardwareAddr(x[:])
}
func (x MacAddress) String() string {
return x.ToMAC().String()
}
func (x MacAddress) String() string {
return x.ToMAC().String()
}
func (x *MacAddress) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *MacAddress) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *MacAddress) UnmarshalText(text []byte) error {
mac, err := ParseMacAddress(string(text))
if err != nil {
func (x *MacAddress) UnmarshalText(text []byte) error {
mac, err := ParseMacAddress(string(text))
if err != nil {
}
return AddressWithPrefix(prefix), nil
}
}
return AddressWithPrefix(prefix), nil
}
func (x AddressWithPrefix) String() string {
return Prefix(x).String()
}
func (x AddressWithPrefix) String() string {
return Prefix(x).String()
}
func (x *AddressWithPrefix) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *AddressWithPrefix) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *AddressWithPrefix) UnmarshalText(text []byte) error {
prefix, err := ParseAddressWithPrefix(string(text))
if err != nil {
func (x *AddressWithPrefix) UnmarshalText(text []byte) error {
prefix, err := ParseAddressWithPrefix(string(text))
if err != nil {
func (x IP4Address) ToIP() net.IP {
return net.IP(x[:]).To4()
}
func (x IP4Address) ToIP() net.IP {
return net.IP(x[:]).To4()
}
func (x IP4Address) String() string {
return x.ToIP().String()
}
func (x IP4Address) String() string {
return x.ToIP().String()
}
func (x *IP4Address) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *IP4Address) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *IP4Address) UnmarshalText(text []byte) error {
ipaddr, err := ParseIP4Address(string(text))
if err != nil {
func (x *IP4Address) UnmarshalText(text []byte) error {
ipaddr, err := ParseIP4Address(string(text))
if err != nil {
func (x IP6Address) ToIP() net.IP {
return net.IP(x[:]).To16()
}
func (x IP6Address) ToIP() net.IP {
return net.IP(x[:]).To16()
}
func (x IP6Address) String() string {
return x.ToIP().String()
}
func (x IP6Address) String() string {
return x.ToIP().String()
}
func (x *IP6Address) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *IP6Address) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *IP6Address) UnmarshalText(text []byte) error {
ipaddr, err := ParseIP6Address(string(text))
if err != nil {
func (x *IP6Address) UnmarshalText(text []byte) error {
ipaddr, err := ParseIP6Address(string(text))
if err != nil {
if ip == nil {
return Address{}, fmt.Errorf("invalid address: %s", s)
}
if ip == nil {
return Address{}, fmt.Errorf("invalid address: %s", s)
}
+ return AddressFromIP(ip), nil
+}
+
+func AddressFromIP(ip net.IP) Address {
var addr Address
if ip.To4() == nil {
addr.Af = ADDRESS_IP6
var addr Address
if ip.To4() == nil {
addr.Af = ADDRESS_IP6
copy(ip4[:], ip.To4())
addr.Un.SetIP4(ip4)
}
copy(ip4[:], ip.To4())
addr.Un.SetIP4(ip4)
}
func (x Address) ToIP() net.IP {
if x.Af == ADDRESS_IP6 {
ip6 := x.Un.GetIP6()
func (x Address) ToIP() net.IP {
if x.Af == ADDRESS_IP6 {
ip6 := x.Un.GetIP6()
return net.IP(ip4[:]).To4()
}
}
return net.IP(ip4[:]).To4()
}
}
func (x Address) String() string {
return x.ToIP().String()
}
func (x Address) String() string {
return x.ToIP().String()
}
func (x *Address) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *Address) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *Address) UnmarshalText(text []byte) error {
addr, err := ParseAddress(string(text))
if err != nil {
func (x *Address) UnmarshalText(text []byte) error {
addr, err := ParseAddress(string(text))
if err != nil {
func (x IP4Prefix) ToIPNet() *net.IPNet {
mask := net.CIDRMask(int(x.Len), 32)
ipnet := &net.IPNet{IP: x.Address.ToIP(), Mask: mask}
return ipnet
}
func (x IP4Prefix) ToIPNet() *net.IPNet {
mask := net.CIDRMask(int(x.Len), 32)
ipnet := &net.IPNet{IP: x.Address.ToIP(), Mask: mask}
return ipnet
}
func (x IP4Prefix) String() string {
ip := x.Address.String()
return ip + "/" + strconv.Itoa(int(x.Len))
}
func (x IP4Prefix) String() string {
ip := x.Address.String()
return ip + "/" + strconv.Itoa(int(x.Len))
}
func (x *IP4Prefix) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *IP4Prefix) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *IP4Prefix) UnmarshalText(text []byte) error {
prefix, err := ParseIP4Prefix(string(text))
if err != nil {
func (x *IP4Prefix) UnmarshalText(text []byte) error {
prefix, err := ParseIP4Prefix(string(text))
if err != nil {
func (x IP6Prefix) ToIPNet() *net.IPNet {
mask := net.CIDRMask(int(x.Len), 128)
ipnet := &net.IPNet{IP: x.Address.ToIP(), Mask: mask}
return ipnet
}
func (x IP6Prefix) ToIPNet() *net.IPNet {
mask := net.CIDRMask(int(x.Len), 128)
ipnet := &net.IPNet{IP: x.Address.ToIP(), Mask: mask}
return ipnet
}
func (x IP6Prefix) String() string {
ip := x.Address.String()
return ip + "/" + strconv.Itoa(int(x.Len))
}
func (x IP6Prefix) String() string {
ip := x.Address.String()
return ip + "/" + strconv.Itoa(int(x.Len))
}
func (x *IP6Prefix) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *IP6Prefix) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *IP6Prefix) UnmarshalText(text []byte) error {
prefix, err := ParseIP6Prefix(string(text))
if err != nil {
func (x *IP6Prefix) UnmarshalText(text []byte) error {
prefix, err := ParseIP6Prefix(string(text))
if err != nil {
func (x Prefix) ToIPNet() *net.IPNet {
var mask net.IPMask
if x.Address.Af == ADDRESS_IP4 {
func (x Prefix) ToIPNet() *net.IPNet {
var mask net.IPMask
if x.Address.Af == ADDRESS_IP4 {
ipnet := &net.IPNet{IP: x.Address.ToIP(), Mask: mask}
return ipnet
}
ipnet := &net.IPNet{IP: x.Address.ToIP(), Mask: mask}
return ipnet
}
func (x Prefix) String() string {
ip := x.Address.String()
return ip + "/" + strconv.Itoa(int(x.Len))
}
func (x Prefix) String() string {
ip := x.Address.String()
return ip + "/" + strconv.Itoa(int(x.Len))
}
func (x *Prefix) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *Prefix) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
func (x *Prefix) UnmarshalText(text []byte) error {
prefix, err := ParsePrefix(string(text))
if err != nil {
func (x *Prefix) UnmarshalText(text []byte) error {
prefix, err := ParsePrefix(string(text))
if err != nil {
ns := float64(sec) + float64(nsec/1e9)
return Timestamp(ns)
}
ns := float64(sec) + float64(nsec/1e9)
return Timestamp(ns)
}
func (x Timestamp) ToTime() time.Time {
ns := int64(x * 1e9)
sec := ns / 1e9
nsec := ns % 1e9
return time.Unix(sec, nsec)
}
func (x Timestamp) ToTime() time.Time {
ns := int64(x * 1e9)
sec := ns / 1e9
nsec := ns % 1e9
return time.Unix(sec, nsec)
}
func (x Timestamp) String() string {
return x.ToTime().String()
}
func (x Timestamp) String() string {
return x.ToTime().String()
}
func (x *Timestamp) MarshalText() ([]byte, error) {
return []byte(x.ToTime().Format(time.RFC3339Nano)), nil
}
func (x *Timestamp) MarshalText() ([]byte, error) {
return []byte(x.ToTime().Format(time.RFC3339Nano)), nil
}
func (x *Timestamp) UnmarshalText(text []byte) error {
t, err := time.Parse(time.RFC3339Nano, string(text))
if err != nil {
func (x *Timestamp) UnmarshalText(text []byte) error {
t, err := time.Parse(time.RFC3339Nano, string(text))
if err != nil {
g.P(" return ", netPkg.Ident("IP"), "(x[:]).To16()")
}
g.P("}")
g.P(" return ", netPkg.Ident("IP"), "(x[:]).To16()")
}
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToIP().String()")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToIP().String()")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
g.P(" if ip == nil {")
g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid address: %s\", s)")
g.P(" }")
g.P(" if ip == nil {")
g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid address: %s\", s)")
g.P(" }")
+ g.P(" return ", structName, "FromIP(ip), nil")
+ g.P("}")
+ g.P()
+
+ // AddressFromIP method
+ g.P("func ", structName, "FromIP(ip ", netPkg.Ident("IP"), ") ", structName, " {")
g.P(" var addr ", structName)
g.P(" if ip.To4() == nil {")
g.P(" addr.Af = ADDRESS_IP6")
g.P(" var addr ", structName)
g.P(" if ip.To4() == nil {")
g.P(" addr.Af = ADDRESS_IP6")
g.P(" copy(ip4[:], ip.To4())")
g.P(" addr.Un.SetIP4(ip4)")
g.P(" }")
g.P(" copy(ip4[:], ip.To4())")
g.P(" addr.Un.SetIP4(ip4)")
g.P(" }")
- g.P(" return addr, nil")
// ToIP method
g.P("func (x ", structName, ") ToIP() ", netPkg.Ident("IP"), " {")
// ToIP method
g.P("func (x ", structName, ") ToIP() ", netPkg.Ident("IP"), " {")
g.P(" return ", netPkg.Ident("IP"), "(ip4[:]).To4()")
g.P(" }")
g.P("}")
g.P(" return ", netPkg.Ident("IP"), "(ip4[:]).To4()")
g.P(" }")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToIP().String()")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToIP().String()")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
g.P(" }")
g.P(" return prefix, nil")
g.P("}")
g.P(" }")
g.P(" return prefix, nil")
g.P("}")
// ToIPNet method
g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
// ToIPNet method
g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
g.P(" return ipnet")
g.P("}")
g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
g.P(" return ipnet")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" ip := x.Address.String()")
g.P(" return ip + \"/\" + ", strconvPkg.Ident("Itoa"), "(int(x.Len))")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" ip := x.Address.String()")
g.P(" return ip + \"/\" + ", strconvPkg.Ident("Itoa"), "(int(x.Len))")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
g.P(" }")
g.P(" return prefix, nil")
g.P("}")
g.P(" }")
g.P(" return prefix, nil")
g.P("}")
// ToIPNet method
g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
// ToIPNet method
g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
g.P(" return ipnet")
g.P("}")
g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
g.P(" return ipnet")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" ip := x.Address.String()")
g.P(" return ip + \"/\" + ", strconvPkg.Ident("Itoa"), "(int(x.Len))")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" ip := x.Address.String()")
g.P(" return ip + \"/\" + ", strconvPkg.Ident("Itoa"), "(int(x.Len))")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
g.P(" }")
g.P(" return ", structName, "(prefix), nil")
g.P("}")
g.P(" }")
g.P(" return ", structName, "(prefix), nil")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return Prefix(x).String()")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return Prefix(x).String()")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
g.P(" copy(macaddr[:], mac[:])")
g.P(" return macaddr, nil")
g.P("}")
g.P(" copy(macaddr[:], mac[:])")
g.P(" return macaddr, nil")
g.P("}")
// ToMAC method
g.P("func (x ", structName, ") ToMAC() ", netPkg.Ident("HardwareAddr"), " {")
g.P(" return ", netPkg.Ident("HardwareAddr"), "(x[:])")
g.P("}")
// ToMAC method
g.P("func (x ", structName, ") ToMAC() ", netPkg.Ident("HardwareAddr"), " {")
g.P(" return ", netPkg.Ident("HardwareAddr"), "(x[:])")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToMAC().String()")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToMAC().String()")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
g.P(" ns := float64(sec) + float64(nsec / 1e9)")
g.P(" return ", structName, "(ns)")
g.P("}")
g.P(" ns := float64(sec) + float64(nsec / 1e9)")
g.P(" return ", structName, "(ns)")
g.P("}")
// ToTime method
g.P("func (x ", structName, ") ToTime() ", timePkg.Ident("Time"), " {")
// ToTime method
g.P("func (x ", structName, ") ToTime() ", timePkg.Ident("Time"), " {")
g.P(" nsec := ns % 1e9")
g.P(" return ", timePkg.Ident("Unix"), "(sec, nsec)")
g.P("}")
g.P(" nsec := ns % 1e9")
g.P(" return ", timePkg.Ident("Unix"), "(sec, nsec)")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToTime().String()")
g.P("}")
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToTime().String()")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.ToTime().Format(", timePkg.Ident("RFC3339Nano"), ")), nil")
g.P("}")
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.ToTime().Format(", timePkg.Ident("RFC3339Nano"), ")), nil")
g.P("}")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")