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 stringsPkg = GoImportPath("strings")
28 func genIPConversion(g *GenFile, structName string, ipv int) {
29 // ParseIPXAddress method
30 g.P("func Parse", structName, "(s string) (", structName, ", error) {")
32 g.P(" ip := ", netPkg.Ident("ParseIP"), "(s).To4()")
34 g.P(" ip := ", netPkg.Ident("ParseIP"), "(s).To16()")
36 g.P(" if ip == nil {")
37 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP address: %s\", s)")
39 g.P(" var ipaddr ", structName)
41 g.P(" copy(ipaddr[:], ip.To4())")
43 g.P(" copy(ipaddr[:], ip.To16())")
45 g.P(" return ipaddr, nil")
50 g.P("func (x ", structName, ") ToIP() ", netPkg.Ident("IP"), " {")
52 g.P(" return ", netPkg.Ident("IP"), "(x[:]).To4()")
54 g.P(" return ", netPkg.Ident("IP"), "(x[:]).To16()")
59 g.P("func (x ", structName, ") String() string {")
60 g.P(" return x.ToIP().String()")
64 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
65 g.P(" return []byte(x.String()), nil")
68 // UnmarshalText method
69 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
70 g.P(" ipaddr, err := Parse", structName, "(string(text))")
71 g.P(" if err !=nil {")
80 func genAddressConversion(g *GenFile, structName string) {
81 // ParseAddress method
82 g.P("func Parse", structName, "(s string) (", structName, ", error) {")
83 g.P(" ip := ", netPkg.Ident("ParseIP"), "(s)")
84 g.P(" if ip == nil {")
85 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid address: %s\", s)")
87 g.P(" var addr ", structName)
88 g.P(" if ip.To4() == nil {")
89 g.P(" addr.Af = ADDRESS_IP6")
90 g.P(" var ip6 IP6Address")
91 g.P(" copy(ip6[:], ip.To16())")
92 g.P(" addr.Un.SetIP6(ip6)")
94 g.P(" addr.Af = ADDRESS_IP4")
95 g.P(" var ip4 IP4Address")
96 g.P(" copy(ip4[:], ip.To4())")
97 g.P(" addr.Un.SetIP4(ip4)")
99 g.P(" return addr, nil")
103 g.P("func (x ", structName, ") ToIP() ", netPkg.Ident("IP"), " {")
104 g.P(" if x.Af == ADDRESS_IP6 {")
105 g.P(" ip6 := x.Un.GetIP6()")
106 g.P(" return ", netPkg.Ident("IP"), "(ip6[:]).To16()")
108 g.P(" ip4 := x.Un.GetIP4()")
109 g.P(" return ", netPkg.Ident("IP"), "(ip4[:]).To4()")
114 g.P("func (x ", structName, ") String() string {")
115 g.P(" return x.ToIP().String()")
118 // MarshalText method
119 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
120 g.P(" return []byte(x.String()), nil")
123 // UnmarshalText method
124 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
125 g.P(" addr, err := Parse", structName, "(string(text))")
126 g.P(" if err != nil {")
135 func genIPPrefixConversion(g *GenFile, structName string, ipv int) {
136 // ParsePrefix method
137 g.P("func Parse", structName, "(s string) (prefix ", structName, ", err error) {")
138 g.P(" hasPrefix := ", stringsPkg.Ident("Contains"), "(s, \"/\")")
139 g.P(" if hasPrefix {")
140 g.P(" ip, network, err := ", netPkg.Ident("ParseCIDR"), "(s)")
141 g.P(" if err != nil {")
142 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", s, err)")
144 g.P(" maskSize, _ := network.Mask.Size()")
145 g.P(" prefix.Len = byte(maskSize)")
147 g.P(" prefix.Address, err = ParseIP4Address(ip.String())")
149 g.P(" prefix.Address, err = ParseIP6Address(ip.String())")
151 g.P(" if err != nil {")
152 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", s, err)")
155 g.P(" ip := ", netPkg.Ident("ParseIP"), "(s)")
156 g.P(" defaultMaskSize, _ := ", netPkg.Ident("CIDRMask"), "(32, 32).Size()")
157 g.P(" if ip.To4() == nil {")
158 g.P(" defaultMaskSize, _ =", netPkg.Ident("CIDRMask"), "(128, 128).Size()")
160 g.P(" prefix.Len = byte(defaultMaskSize)")
162 g.P(" prefix.Address, err = ParseIP4Address(ip.String())")
164 g.P(" prefix.Address, err = ParseIP6Address(ip.String())")
166 g.P(" if err != nil {")
167 g.P(" return ", structName, "{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", s, err)")
170 g.P(" return prefix, nil")
174 g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
176 g.P(" mask := ", netPkg.Ident("CIDRMask"), "(int(x.Len), 32)")
178 g.P(" mask := ", netPkg.Ident("CIDRMask"), "(int(x.Len), 128)")
180 g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
185 g.P("func (x ", structName, ") String() string {")
186 g.P(" ip := x.Address.String()")
187 g.P(" return ip + \"/\" + ", strconvPkg.Ident("Itoa"), "(int(x.Len))")
189 g.P(" mask := ", netPkg.Ident("CIDRMask"), "(int(x.Len), 32)")
191 g.P(" mask := ", netPkg.Ident("CIDRMask"), "(int(x.Len), 128)")
193 g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
194 g.P(" return ipnet.String()")*/
197 // MarshalText method
198 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
199 g.P(" return []byte(x.String()), nil")
202 // UnmarshalText method
203 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
204 g.P(" prefix, err := Parse", structName, "(string(text))")
205 g.P(" if err != nil {")
214 func genPrefixConversion(g *GenFile, structName string) {
215 // ParsePrefix method
216 g.P("func Parse", structName, "(ip string) (prefix ", structName, ", err error) {")
217 g.P(" hasPrefix := ", stringsPkg.Ident("Contains"), "(ip, \"/\")")
218 g.P(" if hasPrefix {")
219 g.P(" netIP, network, err := ", netPkg.Ident("ParseCIDR"), "(ip)")
220 g.P(" if err != nil {")
221 g.P(" return Prefix{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", ip, err)")
223 g.P(" maskSize, _ := network.Mask.Size()")
224 g.P(" prefix.Len = byte(maskSize)")
225 g.P(" prefix.Address, err = ParseAddress(netIP.String())")
226 g.P(" if err != nil {")
227 g.P(" return Prefix{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", ip, err)")
230 g.P(" netIP := ", netPkg.Ident("ParseIP"), "(ip)")
231 g.P(" defaultMaskSize, _ := ", netPkg.Ident("CIDRMask"), "(32, 32).Size()")
232 g.P(" if netIP.To4() == nil {")
233 g.P(" defaultMaskSize, _ =", netPkg.Ident("CIDRMask"), "(128, 128).Size()")
235 g.P(" prefix.Len = byte(defaultMaskSize)")
236 g.P(" prefix.Address, err = ParseAddress(netIP.String())")
237 g.P(" if err != nil {")
238 g.P(" return Prefix{}, ", fmtPkg.Ident("Errorf"), "(\"invalid IP %s: %s\", ip, err)")
241 g.P(" return prefix, nil")
245 g.P("func (x ", structName, ") ToIPNet() *", netPkg.Ident("IPNet"), " {")
246 g.P(" var mask ", netPkg.Ident("IPMask"))
247 g.P(" if x.Address.Af == ADDRESS_IP4 {")
248 g.P(" mask = ", netPkg.Ident("CIDRMask"), "(int(x.Len), 32)")
250 g.P(" mask = ", netPkg.Ident("CIDRMask"), "(int(x.Len), 128)")
252 g.P(" ipnet := &", netPkg.Ident("IPNet"), "{IP: x.Address.ToIP(), Mask: mask}")
257 g.P("func (x ", structName, ") String() string {")
258 g.P(" ip := x.Address.String()")
259 g.P(" return ip + \"/\" + ", strconvPkg.Ident("Itoa"), "(int(x.Len))")
262 // MarshalText method
263 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
264 g.P(" return []byte(x.String()), nil")
267 // UnmarshalText method
268 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
269 g.P(" prefix, err := Parse", structName, "(string(text))")
270 g.P(" if err !=nil {")
279 func genAddressWithPrefixConversion(g *GenFile, structName string) {
280 // ParseAddressWithPrefix method
281 g.P("func Parse", structName, "(s string) (", structName, ", error) {")
282 g.P(" prefix, err := ParsePrefix(s)")
283 g.P(" if err != nil {")
284 g.P(" return ", structName, "{}, err")
286 g.P(" return ", structName, "(prefix), nil")
290 g.P("func (x ", structName, ") String() string {")
291 g.P(" return Prefix(x).String()")
294 // MarshalText method
295 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
296 g.P(" return []byte(x.String()), nil")
299 // UnmarshalText method
300 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
301 g.P(" prefix, err := Parse", structName, "(string(text))")
302 g.P(" if err != nil {")
311 func genMacAddressConversion(g *GenFile, structName string) {
313 g.P("func Parse", structName, "(s string) (", structName, ", error) {")
314 g.P(" var macaddr ", structName)
315 g.P(" mac, err := ", netPkg.Ident("ParseMAC"), "(s)")
316 g.P(" if err != nil {")
317 g.P(" return macaddr, err")
319 g.P(" copy(macaddr[:], mac[:])")
320 g.P(" return macaddr, nil")
324 g.P("func (x ", structName, ") ToMAC() ", netPkg.Ident("HardwareAddr"), " {")
325 g.P(" return ", netPkg.Ident("HardwareAddr"), "(x[:])")
329 g.P("func (x ", structName, ") String() string {")
330 g.P(" return x.ToMAC().String()")
333 // MarshalText method
334 g.P("func (x *", structName, ") MarshalText() ([]byte, error) {")
335 g.P(" return []byte(x.String()), nil")
338 // UnmarshalText method
339 g.P("func (x *", structName, ") UnmarshalText(text []byte) error {")
340 g.P(" mac, err := Parse", structName, "(string(text))")
341 g.P(" if err != nil {")