Generator improvements and cleanup
[govpp.git] / examples / union-example / union_example.go
1 // Copyright (c) 2018 Cisco and/or its affiliates.
2 //
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:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 // union-example is an example to show how to use unions in VPP binary API.
16 package main
17
18 import (
19         "bytes"
20         "fmt"
21         "log"
22         "net"
23
24         "git.fd.io/govpp.git/examples/bin_api/ip"
25         "github.com/lunixbochs/struc"
26 )
27
28 func main() {
29         encodingExample()
30         usageExample()
31 }
32
33 func encodingExample() {
34         // create union with IPv4 address
35         var unionIP4 ip.AddressUnion
36         unionIP4.SetIP4(ip.IP4Address{192, 168, 1, 10})
37
38         // use it in the Address type
39         addr := &ip.Address{
40                 Af: ip.ADDRESS_IP4,
41                 Un: ip.AddressUnionIP4(ip.IP4Address{192, 168, 1, 10}),
42         }
43         log.Printf("encoding union IPv4: %v", addr.Un.GetIP4())
44
45         // encode the address with union
46         data := encode(addr)
47         // decode the address with union
48         addr2 := decode(data)
49
50         log.Printf("decoded union IPv4: %v", addr2.Un.GetIP4())
51 }
52
53 func encode(addr *ip.Address) []byte {
54         log.Printf("encoding address: %#v", addr)
55         buf := new(bytes.Buffer)
56         if err := struc.Pack(buf, addr); err != nil {
57                 panic(err)
58         }
59         return buf.Bytes()
60 }
61
62 func decode(data []byte) *ip.Address {
63         addr := new(ip.Address)
64         buf := bytes.NewReader(data)
65         if err := struc.Unpack(buf, addr); err != nil {
66                 panic(err)
67         }
68         log.Printf("decoded address: %#v", addr)
69         return addr
70 }
71
72 func usageExample() {
73         var convAddr = func(ip string) {
74                 addr, err := ipToAddress(ip)
75                 if err != nil {
76                         log.Printf("converting ip %q failed: %v", ip, err)
77                 }
78                 fmt.Printf("% 0X\n", addr)
79         }
80
81         convAddr("10.10.10.10")
82         convAddr("::1")
83         convAddr("")
84 }
85
86 func ipToAddress(ipstr string) (addr ip.Address, err error) {
87         netIP := net.ParseIP(ipstr)
88         if netIP == nil {
89                 return ip.Address{}, fmt.Errorf("invalid IP: %q", ipstr)
90         }
91         if ip4 := netIP.To4(); ip4 == nil {
92                 addr.Af = ip.ADDRESS_IP6
93                 var ip6addr ip.IP6Address
94                 copy(ip6addr[:], netIP.To16())
95                 addr.Un.SetIP6(ip6addr)
96         } else {
97                 addr.Af = ip.ADDRESS_IP4
98                 var ip4addr ip.IP4Address
99                 copy(ip4addr[:], ip4)
100                 addr.Un.SetIP4(ip4addr)
101         }
102         return
103 }