| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [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 | * Support for message sets. |
| 36 | */ |
| 37 | |
| 38 | import ( |
| 39 | "bytes" |
| 40 | "os" |
| 41 | ) |
| 42 | |
| 43 | // ErrNoMessageTypeId occurs when a protocol buffer does not have a message type ID. |
| 44 | // A message type ID is required for storing a protocol buffer in a message set. |
| 45 | var ErrNoMessageTypeId = os.NewError("proto does not have a message type ID") |
| 46 | |
| 47 | // The first two types (_MessageSet_Item and MessageSet) |
| 48 | // model what the protocol compiler produces for the following protocol message: |
| 49 | // message MessageSet { |
| 50 | // repeated group Item = 1 { |
| 51 | // required int32 type_id = 2; |
| 52 | // required string message = 3; |
| 53 | // }; |
| 54 | // } |
| 55 | // That is the MessageSet wire format. We can't use a proto to generate these |
| 56 | // because that would introduce a circular dependency between it and this package. |
| 57 | // |
| 58 | // When a proto1 proto has a field that looks like: |
| 59 | // optional message<MessageSet> info = 3; |
| 60 | // the protocol compiler produces a field in the generated struct that looks like: |
| 61 | // Info *_proto_.MessageSet "PB(bytes,3,opt,name=info)" |
| 62 | // The package is automatically inserted so there is no need for that proto file to |
| 63 | // import this package. |
| 64 | |
| 65 | type _MessageSet_Item struct { |
| 66 | TypeId *int32 "PB(varint,2,req,name=type_id)" |
| 67 | Message []byte "PB(bytes,3,req,name=message)" |
| 68 | } |
| 69 | |
| 70 | type MessageSet struct { |
| 71 | Item []*_MessageSet_Item "PB(group,1,rep)" |
| 72 | XXX_unrecognized *bytes.Buffer |
| 73 | // TODO: caching? |
| 74 | } |
| 75 | |
| 76 | // messageTypeIder is an interface satisfied by a protocol buffer type |
| 77 | // that may be stored in a MessageSet. |
| 78 | type messageTypeIder interface { |
| 79 | MessageTypeId() int32 |
| 80 | } |
| 81 | |
| 82 | func (ms *MessageSet) find(pb interface{}) *_MessageSet_Item { |
| 83 | mti, ok := pb.(messageTypeIder) |
| 84 | if !ok { |
| 85 | return nil |
| 86 | } |
| 87 | id := mti.MessageTypeId() |
| 88 | for _, item := range ms.Item { |
| 89 | if *item.TypeId == id { |
| 90 | return item |
| 91 | } |
| 92 | } |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | func (ms *MessageSet) Has(pb interface{}) bool { |
| 97 | if ms.find(pb) != nil { |
| 98 | return true |
| 99 | } |
| 100 | return false |
| 101 | } |
| 102 | |
| 103 | func (ms *MessageSet) Unmarshal(pb interface{}) os.Error { |
| 104 | if item := ms.find(pb); item != nil { |
| 105 | return Unmarshal(item.Message, pb) |
| 106 | } |
| 107 | if _, ok := pb.(messageTypeIder); !ok { |
| 108 | return ErrNoMessageTypeId |
| 109 | } |
| 110 | return nil // TODO: return error instead? |
| 111 | } |
| 112 | |
| 113 | func (ms *MessageSet) Marshal(pb interface{}) os.Error { |
| 114 | msg, err := Marshal(pb) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | if item := ms.find(pb); item != nil { |
| 119 | // reuse existing item |
| 120 | item.Message = msg |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | mti, ok := pb.(messageTypeIder) |
| 125 | if !ok { |
| 126 | return ErrWrongType // TODO: custom error? |
| 127 | } |
| 128 | |
| 129 | mtid := mti.MessageTypeId() |
| 130 | ms.Item = append(ms.Item, &_MessageSet_Item{ |
| 131 | TypeId: &mtid, |
| 132 | Message: msg, |
| 133 | }) |
| 134 | return nil |
| 135 | } |
| 136 | |
| 137 | // Support for the message_set_wire_format message option. |
| 138 | |
| 139 | func skipVarint(buf []byte) []byte { |
| 140 | i := 0 |
| 141 | for ; buf[i]&0x80 != 0; i++ { |
| 142 | } |
| 143 | return buf[i+1:] |
| 144 | } |
| 145 | |
| 146 | // MarshalMessageSet encodes the extension map represented by m in the message set wire format. |
| 147 | // It is called by generated Marshal methods on protocol buffer messages with the message_set_wire_format option. |
| 148 | func MarshalMessageSet(m map[int32][]byte) ([]byte, os.Error) { |
| 149 | ms := &MessageSet{Item: make([]*_MessageSet_Item, len(m))} |
| 150 | i := 0 |
| 151 | for k, v := range m { |
| 152 | // Remove the wire type and field number varint, as well as the length varint. |
| 153 | v = skipVarint(skipVarint(v)) |
| 154 | |
| 155 | ms.Item[i] = &_MessageSet_Item{ |
| 156 | TypeId: Int32(k), |
| 157 | Message: v, |
| 158 | } |
| 159 | i++ |
| 160 | } |
| 161 | return Marshal(ms) |
| 162 | } |
| 163 | |
| 164 | // UnmarshalMessageSet decodes the extension map encoded in buf in the message set wire format. |
| 165 | // It is called by generated Unmarshal methods on protocol buffer messages with the message_set_wire_format option. |
| 166 | func UnmarshalMessageSet(buf []byte, m map[int32][]byte) os.Error { |
| 167 | ms := new(MessageSet) |
| 168 | if err := Unmarshal(buf, ms); err != nil { |
| 169 | return err |
| 170 | } |
| 171 | for _, item := range ms.Item { |
| 172 | // restore wire type and field number varint, plus length varint. |
| 173 | b := EncodeVarint(uint64(*item.TypeId)<<3 | WireBytes) |
| 174 | b = append(b, EncodeVarint(uint64(len(item.Message)))...) |
| 175 | b = append(b, item.Message...) |
| 176 | |
| 177 | m[*item.TypeId] = b |
| 178 | } |
| 179 | return nil |
| 180 | } |