1 // Copyright (c) 2020 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
18 //RegisterPlugin("convert", GenerateConvert)
21 // library dependencies
23 fmtPkg = GoImportPath("fmt")
24 netPkg = GoImportPath("net")
25 timePkg = GoImportPath("time")
26 stringsPkg = GoImportPath("strings")
29 func genIPConversion(g *GenFile, structName string, ipv int) {
30 // ParseIPXAddress method
31 g.P("func Parse", structName, "(s string) (", structName, ", error) {")
33 g.P(" ip := ", netPkg.Ident("ParseIP"), "(s).To4()")
35 g.P(" ip := ", netPkg.Ident("ParseIP"), "(s).To16()")
37 g.P(" if ip == nil {")
38 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP address: %s\", s)")
40 g.P(" var ipaddr ", structName)
42 g.P(" copy(ipaddr[:], ip.To4())")
44 g.P(" copy(ipaddr[:], ip.To16())")
46 g.P(" return ipaddr, nil")
51 g.P("func (x ", structName, ") ToIP() ", netPkg.Ident("IP"), " {")
53 g.P(" return ", netPkg.Ident("IP"), "(x[:]).To4()")
55 g.P(" return ", netPkg.Ident("IP"), "(x[:]).To16()")
60 g.P("func (x ", structName, ") String() string {")
61 g.P(" return x.ToIP().String()")
65 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
66 g.P(" return []byte(x.String()), nil")
69 // UnmarshalText method
70 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
71 g.P(" ipaddr, err := Parse", structName, "(string(text))")
72 g.P(" if err !=nil {")
81 func genAddressConversion(g *GenFile, structName string) {
82 // ParseAddress method
83 g.P("func Parse", structName, "(s string) (", structName, ", error) {")
84 g.P(" ip := ", netPkg.Ident("ParseIP"), "(s)")
85 g.P(" if ip == nil {")
86 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid address: %s\", s)")
88 g.P(" var addr ", structName)
89 g.P(" if ip.To4() == nil {")
90 g.P(" addr.Af = ADDRESS_IP6")
91 g.P(" var ip6 IP6Address")
92 g.P(" copy(ip6[:], ip.To16())")
93 g.P(" addr.Un.SetIP6(ip6)")
95 g.P(" addr.Af = ADDRESS_IP4")
96 g.P(" var ip4 IP4Address")
97 g.P(" copy(ip4[:], ip.To4())")
98 g.P(" addr.Un.SetIP4(ip4)")
100 g.P(" return addr, nil")
104 g.P("func (x ", structName, ") ToIP() ", netPkg.Ident("IP"), " {")
105 g.P(" if x.Af == ADDRESS_IP6 {")
106 g.P(" ip6 := x.Un.GetIP6()")
107 g.P(" return ", netPkg.Ident("IP"), "(ip6[:]).To16()")
109 g.P(" ip4 := x.Un.GetIP4()")
110 g.P(" return ", netPkg.Ident("IP"), "(ip4[:]).To4()")
115 g.P("func (x ", structName, ") String() string {")
116 g.P(" return x.ToIP().String()")
119 // MarshalText method
120 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
121 g.P(" return []byte(x.String()), nil")
124 // UnmarshalText method
125 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
126 g.P(" addr, err := Parse", structName, "(string(text))")
127 g.P(" if err != nil {")
136 func genIPPrefixConversion(g *GenFile, structName string, ipv int) {
137 // ParsePrefix method
138 g.P("func Parse", structName, "(s string) (prefix ", structName, ", err error) {")
139 g.P(" hasPrefix := ", stringsPkg.Ident("Contains"), "(s, \"/\")")
140 g.P(" if hasPrefix {")
141 g.P(" ip, network, err := ", netPkg.Ident("ParseCIDR"), "(s)")
142 g.P(" if err != nil {")
143 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", s, err)")
145 g.P(" maskSize, _ := network.Mask.Size()")
146 g.P(" prefix.Len = byte(maskSize)")
148 g.P(" prefix.Address, err = ParseIP4Address(ip.String())")
150 g.P(" prefix.Address, err = ParseIP6Address(ip.String())")
152 g.P(" if err != nil {")
153 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", s, err)")
156 g.P(" ip := ", netPkg.Ident("ParseIP"), "(s)")
157 g.P(" defaultMaskSize, _ := ", netPkg.Ident("CIDRMask"), "(32, 32).Size()")
158 g.P(" if ip.To4() == nil {")
159 g.P(" defaultMaskSize, _ =", netPkg.Ident("CIDRMask"), "(128, 128).Size()")
161 g.P(" prefix.Len = byte(defaultMaskSize)")
163 g.P(" prefix.Address, err = ParseIP4Address(ip.String())")
165 g.P(" prefix.Address, err = ParseIP6Address(ip.String())")
167 g.P(" if err != nil {")
168 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", s, err)")
171 g.P(" return prefix, nil")
175 g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
177 g.P(" mask := ", netPkg.Ident("CIDRMask"), "(int(x.Len), 32)")
179 g.P(" mask := ", netPkg.Ident("CIDRMask"), "(int(x.Len), 128)")
181 g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
186 g.P("func (x ", structName, ") String() string {")
187 g.P(" ip := x.Address.String()")
188 g.P(" return ip + \"/\" + ", strconvPkg.Ident("Itoa"), "(int(x.Len))")
191 // MarshalText method
192 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
193 g.P(" return []byte(x.String()), nil")
196 // UnmarshalText method
197 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
198 g.P(" prefix, err := Parse", structName, "(string(text))")
199 g.P(" if err != nil {")
208 func genPrefixConversion(g *GenFile, structName string) {
209 // ParsePrefix method
210 g.P("func Parse", structName, "(ip string) (prefix ", structName, ", err error) {")
211 g.P(" hasPrefix := ", stringsPkg.Ident("Contains"), "(ip, \"/\")")
212 g.P(" if hasPrefix {")
213 g.P(" netIP, network, err := ", netPkg.Ident("ParseCIDR"), "(ip)")
214 g.P(" if err != nil {")
215 g.P(" return Prefix{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", ip, err)")
217 g.P(" maskSize, _ := network.Mask.Size()")
218 g.P(" prefix.Len = byte(maskSize)")
219 g.P(" prefix.Address, err = ParseAddress(netIP.String())")
220 g.P(" if err != nil {")
221 g.P(" return Prefix{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", ip, err)")
224 g.P(" netIP := ", netPkg.Ident("ParseIP"), "(ip)")
225 g.P(" defaultMaskSize, _ := ", netPkg.Ident("CIDRMask"), "(32, 32).Size()")
226 g.P(" if netIP.To4() == nil {")
227 g.P(" defaultMaskSize, _ =", netPkg.Ident("CIDRMask"), "(128, 128).Size()")
229 g.P(" prefix.Len = byte(defaultMaskSize)")
230 g.P(" prefix.Address, err = ParseAddress(netIP.String())")
231 g.P(" if err != nil {")
232 g.P(" return Prefix{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", ip, err)")
235 g.P(" return prefix, nil")
239 g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
240 g.P(" var mask ", netPkg.Ident("IPMask"))
241 g.P(" if x.Address.Af == ADDRESS_IP4 {")
242 g.P(" mask = ", netPkg.Ident("CIDRMask"), "(int(x.Len), 32)")
244 g.P(" mask = ", netPkg.Ident("CIDRMask"), "(int(x.Len), 128)")
246 g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
251 g.P("func (x ", structName, ") String() string {")
252 g.P(" ip := x.Address.String()")
253 g.P(" return ip + \"/\" + ", strconvPkg.Ident("Itoa"), "(int(x.Len))")
256 // MarshalText method
257 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
258 g.P(" return []byte(x.String()), nil")
261 // UnmarshalText method
262 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
263 g.P(" prefix, err := Parse", structName, "(string(text))")
264 g.P(" if err !=nil {")
273 func genAddressWithPrefixConversion(g *GenFile, structName string) {
274 // ParseAddressWithPrefix method
275 g.P("func Parse", structName, "(s string) (", structName, ", error) {")
276 g.P(" prefix, err := ParsePrefix(s)")
277 g.P(" if err != nil {")
278 g.P(" return ", structName, "{}, err")
280 g.P(" return ", structName, "(prefix), nil")
284 g.P("func (x ", structName, ") String() string {")
285 g.P(" return Prefix(x).String()")
288 // MarshalText method
289 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
290 g.P(" return []byte(x.String()), nil")
293 // UnmarshalText method
294 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
295 g.P(" prefix, err := Parse", structName, "(string(text))")
296 g.P(" if err != nil {")
305 func genMacAddressConversion(g *GenFile, structName string) {
307 g.P("func Parse", structName, "(s string) (", structName, ", error) {")
308 g.P(" var macaddr ", structName)
309 g.P(" mac, err := ", netPkg.Ident("ParseMAC"), "(s)")
310 g.P(" if err != nil {")
311 g.P(" return macaddr, err")
313 g.P(" copy(macaddr[:], mac[:])")
314 g.P(" return macaddr, nil")
318 g.P("func (x ", structName, ") ToMAC() ", netPkg.Ident("HardwareAddr"), " {")
319 g.P(" return ", netPkg.Ident("HardwareAddr"), "(x[:])")
323 g.P("func (x ", structName, ") String() string {")
324 g.P(" return x.ToMAC().String()")
327 // MarshalText method
328 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
329 g.P(" return []byte(x.String()), nil")
332 // UnmarshalText method
333 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
334 g.P(" mac, err := Parse", structName, "(string(text))")
335 g.P(" if err != nil {")
344 func genTimestampConversion(g *GenFile, structName string) {
345 // NewTimestamp method
346 g.P("func New", structName, "(t ", timePkg.Ident("Time"), ") ", structName, " {")
347 g.P(" sec := int64(t.Unix())")
348 g.P(" nsec := int32(t.Nanosecond())")
349 g.P(" ns := float64(sec) + float64(nsec / 1e9)")
350 g.P(" return ", structName, "(ns)")
354 g.P("func (x ", structName, ") ToTime() ", timePkg.Ident("Time"), " {")
355 g.P(" ns := int64(x * 1e9)")
356 g.P(" sec := ns / 1e9")
357 g.P(" nsec := ns % 1e9")
358 g.P(" return ", timePkg.Ident("Unix"), "(sec, nsec)")
362 g.P("func (x ", structName, ") String() string {")
363 g.P(" return x.ToTime().String()")
366 // MarshalText method
367 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
368 g.P(" return []byte(x.ToTime().Format(", timePkg.Ident("RFC3339Nano"), ")), nil")
371 // UnmarshalText method
372 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
373 g.P(" t, err := ", timePkg.Ident("Parse"), "(", timePkg.Ident("RFC3339Nano"), ", string(text))")
374 g.P(" if err != nil {")
377 g.P(" *x = New", structName, "(t)")