github.com/stellar/go-xdr@v0.0.0-20231122183749-b53fb00bcac2/xdr/example_test.go (about) 1 /* 2 * Copyright (c) 2012 Dave Collins <dave@davec.name> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 package xdr_test 18 19 import ( 20 "fmt" 21 22 "github.com/stellar/go-xdr/xdr" 23 ) 24 25 // This example demonstrates how to use Marshal to automatically XDR encode 26 // data using reflection. 27 func ExampleMarshal() { 28 // Hypothetical image header format. 29 type ImageHeader struct { 30 Signature [3]byte 31 Version uint32 32 IsGrayscale bool 33 NumSections uint32 34 } 35 36 // Sample image header data. 37 h := ImageHeader{[3]byte{0xAB, 0xCD, 0xEF}, 2, true, 10} 38 39 // Use marshal to automatically determine the appropriate underlying XDR 40 // types and encode. 41 encodedData, err := xdr.Marshal(&h) 42 if err != nil { 43 fmt.Println(err) 44 return 45 } 46 47 fmt.Println("encodedData:", encodedData) 48 49 // Output: 50 // encodedData: [171 205 239 0 0 0 0 2 0 0 0 1 0 0 0 10] 51 } 52 53 // This example demonstrates how to use Unmarshal to decode XDR encoded data from 54 // a byte slice into a struct. 55 func ExampleUnmarshal() { 56 // Hypothetical image header format. 57 type ImageHeader struct { 58 Signature [3]byte 59 Version uint32 60 IsGrayscale bool 61 NumSections uint32 62 } 63 64 // XDR encoded data described by the above structure. Typically this would 65 // be read from a file or across the network, but use a manual byte array 66 // here as an example. 67 encodedData := []byte{ 68 0xAB, 0xCD, 0xEF, 0x00, // Signature 69 0x00, 0x00, 0x00, 0x02, // Version 70 0x00, 0x00, 0x00, 0x01, // IsGrayscale 71 0x00, 0x00, 0x00, 0x0A} // NumSections 72 73 // Declare a variable to provide Unmarshal with a concrete type and instance 74 // to decode into. 75 var h ImageHeader 76 remainingBytes, err := xdr.Unmarshal(encodedData, &h) 77 if err != nil { 78 fmt.Println(err) 79 return 80 } 81 82 fmt.Println("remainingBytes:", remainingBytes) 83 fmt.Printf("h: %+v", h) 84 85 // Output: 86 // remainingBytes: [] 87 // h: {Signature:[171 205 239] Version:2 IsGrayscale:true NumSections:10} 88 } 89 90 // This example demonstrates how to manually decode XDR encoded data from a 91 // byte slice. Compare this example with the Unmarshal example which performs 92 // the same task automatically by utilizing a struct type definition and 93 // reflection. 94 func ExampleNewDecoder() { 95 96 // XDR encoded data for a hypothetical ImageHeader struct as follows: 97 // type ImageHeader struct { 98 // Signature [3]byte 99 // Version uint32 100 // IsGrayscale bool 101 // NumSections uint32 102 // } 103 encodedData := []byte{ 104 0xAB, 0xCD, 0xEF, 0x00, // Signature 105 0x00, 0x00, 0x00, 0x02, // Version 106 0x00, 0x00, 0x00, 0x01, // IsGrayscale 107 0x00, 0x00, 0x00, 0x0A} // NumSections 108 109 // Get a new decoder for manual decoding. 110 dec := xdr.NewDecoder(encodedData) 111 112 signature, err := dec.DecodeFixedOpaque(3) 113 if err != nil { 114 fmt.Println(err) 115 return 116 } 117 118 version, err := dec.DecodeUint() 119 if err != nil { 120 fmt.Println(err) 121 return 122 } 123 124 isGrayscale, err := dec.DecodeBool() 125 if err != nil { 126 fmt.Println(err) 127 return 128 } 129 130 numSections, err := dec.DecodeUint() 131 if err != nil { 132 fmt.Println(err) 133 return 134 } 135 136 fmt.Println("signature:", signature) 137 fmt.Println("version:", version) 138 fmt.Println("isGrayscale:", isGrayscale) 139 fmt.Println("numSections:", numSections) 140 141 // Output: 142 // signature: [171 205 239] 143 // version: 2 144 // isGrayscale: true 145 // numSections: 10 146 } 147 148 // This example demonstrates how to manually encode XDR data from Go variables. 149 // Compare this example with the Marshal example which performs the same task 150 // automatically by utilizing a struct type definition and reflection. 151 func ExampleNewEncoder() { 152 // Data for a hypothetical ImageHeader struct as follows: 153 // type ImageHeader struct { 154 // Signature [3]byte 155 // Version uint32 156 // IsGrayscale bool 157 // NumSections uint32 158 // } 159 signature := []byte{0xAB, 0xCD, 0xEF} 160 version := uint32(2) 161 isGrayscale := true 162 numSections := uint32(10) 163 164 // Get a new encoder for manual encoding. 165 enc := xdr.NewEncoder() 166 167 err := enc.EncodeFixedOpaque(signature) 168 if err != nil { 169 fmt.Println(err) 170 return 171 } 172 173 err = enc.EncodeUint(version) 174 if err != nil { 175 fmt.Println(err) 176 return 177 } 178 179 err = enc.EncodeBool(isGrayscale) 180 if err != nil { 181 fmt.Println(err) 182 return 183 } 184 185 err = enc.EncodeUint(numSections) 186 if err != nil { 187 fmt.Println(err) 188 return 189 } 190 191 fmt.Println("encodedData:", enc.Data()) 192 193 // Output: 194 // encodedData: [171 205 239 0 0 0 0 2 0 0 0 1 0 0 0 10] 195 }