const (
fmtPkg = GoImportPath("fmt")
netPkg = GoImportPath("net")
+ timePkg = GoImportPath("time")
stringsPkg = GoImportPath("strings")
)
-func generateIPConversion(g *GenFile, structName string, ipv int) {
+func genIPConversion(g *GenFile, structName string, ipv int) {
// ParseIPXAddress method
g.P("func Parse", structName, "(s string) (", structName, ", error) {")
if ipv == 4 {
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 {")
g.P()
}
-func generateAddressConversion(g *GenFile, structName string) {
+func genAddressConversion(g *GenFile, structName string) {
// ParseAddress method
g.P("func Parse", structName, "(s string) (", structName, ", error) {")
g.P(" ip := ", netPkg.Ident("ParseIP"), "(s)")
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(" 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"), " {")
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 {")
g.P()
}
-func generateIPPrefixConversion(g *GenFile, structName string, ipv int) {
+func genIPPrefixConversion(g *GenFile, structName string, ipv int) {
// ParsePrefix method
g.P("func Parse", structName, "(s string) (prefix ", structName, ", err error) {")
g.P(" hasPrefix := ", stringsPkg.Ident("Contains"), "(s, \"/\")")
g.P(" }")
g.P(" return prefix, nil")
g.P("}")
+ g.P()
// 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()
// 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))")
- /*if ipv == 4 {
- g.P(" mask := ", netPkg.Ident("CIDRMask"), "(int(x.Len), 32)")
- } else {
- g.P(" mask := ", netPkg.Ident("CIDRMask"), "(int(x.Len), 128)")
- }
- g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
- g.P(" return ipnet.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 {")
g.P()
}
-func generatePrefixConversion(g *GenFile, structName string) {
+func genPrefixConversion(g *GenFile, structName string) {
// ParsePrefix method
g.P("func Parse", structName, "(ip string) (prefix ", structName, ", err error) {")
g.P(" hasPrefix := ", stringsPkg.Ident("Contains"), "(ip, \"/\")")
g.P(" }")
g.P(" return prefix, nil")
g.P("}")
+ g.P()
// 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()
// 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 {")
g.P()
}
-func generateAddressWithPrefixConversion(g *GenFile, structName string) {
+func genAddressWithPrefixConversion(g *GenFile, structName string) {
// ParseAddressWithPrefix method
g.P("func Parse", structName, "(s string) (", structName, ", error) {")
g.P(" prefix, err := ParsePrefix(s)")
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 {")
g.P()
}
-func generateMacAddressConversion(g *GenFile, structName string) {
+func genMacAddressConversion(g *GenFile, structName string) {
// ParseMAC method
g.P("func Parse", structName, "(s string) (", structName, ", error) {")
g.P(" var macaddr ", structName)
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 {")
g.P("}")
g.P()
}
+
+func genTimestampConversion(g *GenFile, structName string) {
+ // NewTimestamp method
+ g.P("func New", structName, "(t ", timePkg.Ident("Time"), ") ", structName, " {")
+ g.P(" sec := int64(t.Unix())")
+ g.P(" nsec := int32(t.Nanosecond())")
+ 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"), " {")
+ g.P(" ns := int64(x * 1e9)")
+ g.P(" sec := ns / 1e9")
+ 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 {")
+ g.P(" t, err := ", timePkg.Ident("Parse"), "(", timePkg.Ident("RFC3339Nano"), ", string(text))")
+ g.P(" if err != nil {")
+ g.P(" return err")
+ g.P(" }")
+ g.P(" *x = New", structName, "(t)")
+ g.P(" return nil")
+ g.P("}")
+ g.P()
+}