1 // Protocol Buffers for Go with Gadgets 2 // 3 // Copyright (c) 2013, The GoGo Authors. All rights reserved. 4 // http://github.com/gogo/protobuf 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 /* 30 Package custom contains custom types for test and example purposes. 31 These types are used by the test structures generated by gogoprotobuf. 32 */ 33 package custom 34 35 import ( 36 "bytes" 37 "encoding/json" 38 ) 39 40 type Bytes []byte 41 42 func (b Bytes) Marshal() ([]byte, error) { 43 buffer := make([]byte, len(b)) 44 _, err := b.MarshalTo(buffer) 45 return buffer, err 46 } 47 48 func (b Bytes) MarshalTo(data []byte) (n int, err error) { 49 copy(data, b) 50 return len(b), nil 51 } 52 53 func (b *Bytes) Unmarshal(data []byte) error { 54 if data == nil { 55 b = nil 56 return nil 57 } 58 pb := make([]byte, len(data)) 59 copy(pb, data) 60 *b = pb 61 return nil 62 } 63 64 func (b Bytes) MarshalJSON() ([]byte, error) { 65 data, err := b.Marshal() 66 if err != nil { 67 return nil, err 68 } 69 return json.Marshal(data) 70 } 71 72 func (b *Bytes) Size() int { 73 return len(*b) 74 } 75 76 func (b *Bytes) UnmarshalJSON(data []byte) error { 77 v := new([]byte) 78 err := json.Unmarshal(data, v) 79 if err != nil { 80 return err 81 } 82 return b.Unmarshal(*v) 83 } 84 85 func (this Bytes) Equal(that Bytes) bool { 86 return bytes.Equal(this, that) 87 } 88 89 func (this Bytes) Compare(that Bytes) int { 90 return bytes.Compare(this, that) 91 } 92 93 type randy interface { 94 Intn(n int) int 95 } 96 97 func NewPopulatedBytes(r randy) *Bytes { 98 l := r.Intn(100) 99 data := Bytes(make([]byte, l)) 100 for i := 0; i < l; i++ { 101 data[i] = byte(r.Intn(255)) 102 } 103 return &data 104 } 105