1 // Copyright (c) 2018 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.
15 // union-example is an example to show how to use unions in VPP binary API.
22 "git.fd.io/govpp.git/examples/bin_api/ip"
23 "github.com/lunixbochs/struc"
27 // create union with IPv4 address
28 var unionIP4 ip.AddressUnion
29 unionIP4.SetIP4(ip.IP4Address{Address: []byte{192, 168, 1, 10}})
31 // use it in the Address type
36 log.Printf("encoding union IPv4: %v", addr.Un.GetIP4())
38 // encode the address with union
40 // decode the address with union
43 log.Printf("decoded union IPv4: %v", addr2.Un.GetIP4())
46 func encode(addr *ip.Address) []byte {
47 log.Printf("encoding address: %#v", addr)
48 buf := new(bytes.Buffer)
49 if err := struc.Pack(buf, addr); err != nil {
55 func decode(data []byte) *ip.Address {
56 addr := new(ip.Address)
57 buf := bytes.NewReader(data)
58 if err := struc.Unpack(buf, addr); err != nil {
61 log.Printf("decoded address: %#v", addr)