Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. All rights reserved. |
| 4 | // http://code.google.com/p/goprotobuf/ |
| 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 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | package proto |
| 33 | |
| 34 | |
| 35 | /* |
| 36 | * Types and routines for supporting protocol buffer extensions. |
| 37 | */ |
| 38 | |
| 39 | import ( |
| 40 | "os" |
| 41 | "reflect" |
| 42 | "unsafe" |
| 43 | ) |
| 44 | |
| 45 | // ExtensionRange represents a range of message extensions for a protocol buffer. |
| 46 | // Used in code generated by the protocol compiler. |
| 47 | type ExtensionRange struct { |
| 48 | Start, End int32 // both inclusive |
| 49 | } |
| 50 | |
| 51 | // extendableProto is an interface implemented by any protocol buffer that may be extended. |
| 52 | type extendableProto interface { |
| 53 | ExtensionRangeArray() []ExtensionRange |
| 54 | ExtensionMap() map[int32][]byte |
| 55 | } |
| 56 | |
| 57 | // ExtensionDesc represents an extension specification. |
| 58 | // Used in generated code from the protocol compiler. |
| 59 | type ExtensionDesc struct { |
| 60 | ExtendedType interface{} // nil pointer to the type that is being extended |
| 61 | ExtensionType interface{} // nil pointer to the extension type |
| 62 | Field int32 // field number |
| 63 | Tag string // PB(...) tag style |
| 64 | } |
| 65 | |
| 66 | // Return true iff the given field number is in an extension range. |
| 67 | func isExtensionField(extended extendableProto, field int32) bool { |
| 68 | for _, er := range extended.ExtensionRangeArray() { |
| 69 | if er.Start <= field && field <= er.End { |
| 70 | return true |
| 71 | } |
| 72 | } |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | func checkExtensionTypes(extended extendableProto, extension *ExtensionDesc) os.Error { |
| 77 | // Check the extended type. |
| 78 | if a, b := reflect.Typeof(extended), reflect.Typeof(extension.ExtendedType); a != b { |
| 79 | return os.NewError("bad extended type; " + b.String() + " does not extend " + a.String()) |
| 80 | } |
| 81 | // Check the range. |
| 82 | if !isExtensionField(extended, extension.Field) { |
| 83 | return os.NewError("bad extension number; not in declared ranges") |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func HasExtension(extended extendableProto, extension *ExtensionDesc) bool { |
| 89 | // TODO: Check types, field numbers, etc.? |
| 90 | _, ok := extended.ExtensionMap()[extension.Field] |
| 91 | return ok |
| 92 | } |
| 93 | |
| 94 | func ClearExtension(extended extendableProto, extension *ExtensionDesc) { |
| 95 | // TODO: Check types, field numbers, etc.? |
| 96 | extended.ExtensionMap()[extension.Field] = nil, false |
| 97 | } |
| 98 | |
| 99 | func GetExtension(extended extendableProto, extension *ExtensionDesc) (interface{}, os.Error) { |
| 100 | if err := checkExtensionTypes(extended, extension); err != nil { |
| 101 | return nil, err |
| 102 | } |
| 103 | |
| 104 | b, ok := extended.ExtensionMap()[extension.Field] |
| 105 | if !ok { |
| 106 | return nil, nil // not an error |
| 107 | } |
| 108 | |
| 109 | // Discard wire type and field number varint. It isn't needed. |
| 110 | _, n := DecodeVarint(b) |
| 111 | o := NewBuffer(b[n:]) |
| 112 | |
| 113 | t := reflect.Typeof(extension.ExtensionType).(*reflect.PtrType) |
| 114 | props := &Properties{} |
| 115 | props.Init(t, "irrelevant_name", extension.Tag, 0) |
| 116 | |
| 117 | base := unsafe.New(t) |
| 118 | var sbase uintptr |
| 119 | if _, ok := t.Elem().(*reflect.StructType); ok { |
| 120 | // props.dec will be dec_struct_message, which does not refer to sbase. |
| 121 | *(*unsafe.Pointer)(base) = unsafe.New(t.Elem()) |
| 122 | } else { |
| 123 | sbase = uintptr(unsafe.New(t.Elem())) |
| 124 | } |
| 125 | if err := props.dec(o, props, uintptr(base), sbase); err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | return unsafe.Unreflect(t, base), nil |
| 129 | } |
| 130 | |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 131 | // TODO: (needed for repeated extensions) |
Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 132 | // - ExtensionSize |
| 133 | // - AddExtension |
| 134 | |
| 135 | func SetExtension(extended extendableProto, extension *ExtensionDesc, value interface{}) os.Error { |
| 136 | if err := checkExtensionTypes(extended, extension); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | if reflect.Typeof(extension.ExtensionType) != reflect.Typeof(value) { |
| 140 | return os.NewError("bad extension value type") |
| 141 | } |
| 142 | |
| 143 | props := new(Properties) |
| 144 | props.Init(reflect.Typeof(extension.ExtensionType), "unknown_name", extension.Tag, 0) |
| 145 | |
| 146 | p := NewBuffer(nil) |
| 147 | v := reflect.NewValue(value) |
| 148 | if err := props.enc(p, props, v.Addr()); err != nil { |
| 149 | return err |
| 150 | } |
| 151 | extended.ExtensionMap()[extension.Field] = p.buf |
| 152 | return nil |
| 153 | } |