aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAloys Augustin <aloaugus@cisco.com>2021-10-18 14:16:59 +0200
committerOndrej Fabry <ofabry@cisco.com>2021-11-29 07:16:05 +0000
commit0a0c03d459547aa9004cbbc1ed9a358d831adf03 (patch)
treee461f1100fa7e5faf6db331708f9cbbad4c0e829
parent4cc53fc9a836df099dde983d506eb892dc2fdef1 (diff)
Add "FromIP" helper for addresses
Change-Id: Ib34b84b8267c95ce3bacef4806cc52a6c7c24f05 Signed-off-by: Aloys Augustin <aloaugus@cisco.com>
-rw-r--r--binapi/ethernet_types/ethernet_types.ba.go4
-rw-r--r--binapi/ip_types/ip_types.ba.go31
-rw-r--r--binapi/vpe_types/vpe_types.ba.go4
-rw-r--r--binapigen/gen_helpers.go34
4 files changed, 71 insertions, 2 deletions
diff --git a/binapi/ethernet_types/ethernet_types.ba.go b/binapi/ethernet_types/ethernet_types.ba.go
index dd18d9e..cce8de4 100644
--- a/binapi/ethernet_types/ethernet_types.ba.go
+++ b/binapi/ethernet_types/ethernet_types.ba.go
@@ -35,15 +35,19 @@ func ParseMacAddress(s string) (MacAddress, error) {
copy(macaddr[:], mac[:])
return macaddr, nil
}
+
func (x MacAddress) ToMAC() net.HardwareAddr {
return net.HardwareAddr(x[:])
}
+
func (x MacAddress) String() string {
return x.ToMAC().String()
}
+
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 {
diff --git a/binapi/ip_types/ip_types.ba.go b/binapi/ip_types/ip_types.ba.go
index bebbd57..747f56a 100644
--- a/binapi/ip_types/ip_types.ba.go
+++ b/binapi/ip_types/ip_types.ba.go
@@ -280,12 +280,15 @@ func ParseAddressWithPrefix(s string) (AddressWithPrefix, error) {
}
return AddressWithPrefix(prefix), nil
}
+
func (x AddressWithPrefix) String() string {
return Prefix(x).String()
}
+
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 {
@@ -311,12 +314,15 @@ func ParseIP4Address(s string) (IP4Address, error) {
func (x IP4Address) ToIP() net.IP {
return net.IP(x[:]).To4()
}
+
func (x IP4Address) String() string {
return x.ToIP().String()
}
+
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 {
@@ -345,12 +351,15 @@ func ParseIP6Address(s string) (IP6Address, error) {
func (x IP6Address) ToIP() net.IP {
return net.IP(x[:]).To16()
}
+
func (x IP6Address) String() string {
return x.ToIP().String()
}
+
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 {
@@ -374,6 +383,10 @@ func ParseAddress(s string) (Address, error) {
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
@@ -386,8 +399,9 @@ func ParseAddress(s string) (Address, error) {
copy(ip4[:], ip.To4())
addr.Un.SetIP4(ip4)
}
- return addr, nil
+ return addr
}
+
func (x Address) ToIP() net.IP {
if x.Af == ADDRESS_IP6 {
ip6 := x.Un.GetIP6()
@@ -397,12 +411,15 @@ func (x Address) ToIP() net.IP {
return net.IP(ip4[:]).To4()
}
}
+
func (x Address) String() string {
return x.ToIP().String()
}
+
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 {
@@ -451,18 +468,22 @@ func ParseIP4Prefix(s string) (prefix IP4Prefix, err error) {
}
return prefix, 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) 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) UnmarshalText(text []byte) error {
prefix, err := ParseIP4Prefix(string(text))
if err != nil {
@@ -511,18 +532,22 @@ func ParseIP6Prefix(s string) (prefix IP6Prefix, err error) {
}
return prefix, 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) 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) UnmarshalText(text []byte) error {
prefix, err := ParseIP6Prefix(string(text))
if err != nil {
@@ -573,6 +598,7 @@ func ParsePrefix(ip string) (prefix Prefix, err error) {
}
return prefix, nil
}
+
func (x Prefix) ToIPNet() *net.IPNet {
var mask net.IPMask
if x.Address.Af == ADDRESS_IP4 {
@@ -583,13 +609,16 @@ func (x Prefix) ToIPNet() *net.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) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
+
func (x *Prefix) UnmarshalText(text []byte) error {
prefix, err := ParsePrefix(string(text))
if err != nil {
diff --git a/binapi/vpe_types/vpe_types.ba.go b/binapi/vpe_types/vpe_types.ba.go
index 64e988f..779cbdb 100644
--- a/binapi/vpe_types/vpe_types.ba.go
+++ b/binapi/vpe_types/vpe_types.ba.go
@@ -86,18 +86,22 @@ func NewTimestamp(t time.Time) Timestamp {
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) String() string {
return x.ToTime().String()
}
+
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 {
diff --git a/binapigen/gen_helpers.go b/binapigen/gen_helpers.go
index 944bfe2..0fc303d 100644
--- a/binapigen/gen_helpers.go
+++ b/binapigen/gen_helpers.go
@@ -55,16 +55,19 @@ func genIPConversion(g *GenFile, structName string, ipv int) {
g.P(" return ", netPkg.Ident("IP"), "(x[:]).To16()")
}
g.P("}")
+ g.P()
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToIP().String()")
g.P("}")
+ g.P()
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
+ g.P()
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
@@ -85,6 +88,12 @@ func genAddressConversion(g *GenFile, structName string) {
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")
@@ -97,8 +106,9 @@ func genAddressConversion(g *GenFile, structName string) {
g.P(" copy(ip4[:], ip.To4())")
g.P(" addr.Un.SetIP4(ip4)")
g.P(" }")
- g.P(" return addr, nil")
+ g.P(" return addr")
g.P("}")
+ g.P()
// ToIP method
g.P("func (x ", structName, ") ToIP() ", netPkg.Ident("IP"), " {")
@@ -110,16 +120,19 @@ func genAddressConversion(g *GenFile, structName string) {
g.P(" return ", netPkg.Ident("IP"), "(ip4[:]).To4()")
g.P(" }")
g.P("}")
+ g.P()
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToIP().String()")
g.P("}")
+ g.P()
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
+ g.P()
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
@@ -170,6 +183,7 @@ func genIPPrefixConversion(g *GenFile, structName string, ipv int) {
g.P(" }")
g.P(" return prefix, nil")
g.P("}")
+ g.P()
// ToIPNet method
g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
@@ -181,17 +195,20 @@ func genIPPrefixConversion(g *GenFile, structName string, ipv int) {
g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
g.P(" return ipnet")
g.P("}")
+ 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("}")
+ g.P()
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
+ g.P()
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
@@ -234,6 +251,7 @@ func genPrefixConversion(g *GenFile, structName string) {
g.P(" }")
g.P(" return prefix, nil")
g.P("}")
+ g.P()
// ToIPNet method
g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
@@ -246,17 +264,20 @@ func genPrefixConversion(g *GenFile, structName string) {
g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
g.P(" return ipnet")
g.P("}")
+ 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("}")
+ g.P()
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
+ g.P()
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
@@ -279,16 +300,19 @@ func genAddressWithPrefixConversion(g *GenFile, structName string) {
g.P(" }")
g.P(" return ", structName, "(prefix), nil")
g.P("}")
+ g.P()
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return Prefix(x).String()")
g.P("}")
+ g.P()
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
+ g.P()
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
@@ -313,21 +337,25 @@ func genMacAddressConversion(g *GenFile, structName string) {
g.P(" copy(macaddr[:], mac[:])")
g.P(" return macaddr, nil")
g.P("}")
+ g.P()
// ToMAC method
g.P("func (x ", structName, ") ToMAC() ", netPkg.Ident("HardwareAddr"), " {")
g.P(" return ", netPkg.Ident("HardwareAddr"), "(x[:])")
g.P("}")
+ g.P()
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToMAC().String()")
g.P("}")
+ g.P()
// MarshalText method
g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
g.P(" return []byte(x.String()), nil")
g.P("}")
+ g.P()
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
@@ -349,6 +377,7 @@ func genTimestampConversion(g *GenFile, structName string) {
g.P(" ns := float64(sec) + float64(nsec / 1e9)")
g.P(" return ", structName, "(ns)")
g.P("}")
+ g.P()
// ToTime method
g.P("func (x ", structName, ") ToTime() ", timePkg.Ident("Time"), " {")
@@ -357,16 +386,19 @@ func genTimestampConversion(g *GenFile, structName string) {
g.P(" nsec := ns % 1e9")
g.P(" return ", timePkg.Ident("Unix"), "(sec, nsec)")
g.P("}")
+ g.P()
// String method
g.P("func (x ", structName, ") String() string {")
g.P(" return x.ToTime().String()")
g.P("}")
+ 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("}")
+ g.P()
// UnmarshalText method
g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")