Source File
message_set.go
Belonging Package
github.com/gogo/protobuf/proto
// Go support for Protocol Buffers - Google's data interchange format//// Copyright 2010 The Go Authors. All rights reserved.// https://github.com/golang/protobuf//// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are// met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above// copyright notice, this list of conditions and the following disclaimer// in the documentation and/or other materials provided with the// distribution.// * Neither the name of Google Inc. nor the names of its// contributors may be used to endorse or promote products derived from// this software without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.package proto/** Support for message sets.*/import ()// errNoMessageTypeID occurs when a protocol buffer does not have a message type ID.// A message type ID is required for storing a protocol buffer in a message set.var errNoMessageTypeID = errors.New("proto does not have a message type ID")// The first two types (_MessageSet_Item and messageSet)// model what the protocol compiler produces for the following protocol message:// message MessageSet {// repeated group Item = 1 {// required int32 type_id = 2;// required string message = 3;// };// }// That is the MessageSet wire format. We can't use a proto to generate these// because that would introduce a circular dependency between it and this package.type _MessageSet_Item struct {TypeId *int32 `protobuf:"varint,2,req,name=type_id"`Message []byte `protobuf:"bytes,3,req,name=message"`}type messageSet struct {Item []*_MessageSet_Item `protobuf:"group,1,rep"`XXX_unrecognized []byte// TODO: caching?}// Make sure messageSet is a Message.var _ Message = (*messageSet)(nil)// messageTypeIder is an interface satisfied by a protocol buffer type// that may be stored in a MessageSet.type messageTypeIder interface {MessageTypeId() int32}func ( *messageSet) ( Message) *_MessageSet_Item {, := .(messageTypeIder)if ! {return nil}:= .MessageTypeId()for , := range .Item {if *.TypeId == {return}}return nil}func ( *messageSet) ( Message) bool {return .find() != nil}func ( *messageSet) ( Message) error {if := .find(); != nil {return Unmarshal(.Message, )}if , := .(messageTypeIder); ! {return errNoMessageTypeID}return nil // TODO: return error instead?}func ( *messageSet) ( Message) error {, := Marshal()if != nil {return}if := .find(); != nil {// reuse existing item.Message =return nil}, := .(messageTypeIder)if ! {return errNoMessageTypeID}:= .MessageTypeId().Item = append(.Item, &_MessageSet_Item{TypeId: &,Message: ,})return nil}func ( *messageSet) () { * = messageSet{} }func ( *messageSet) () string { return CompactTextString() }func (*messageSet) () {}// Support for the message_set_wire_format message option.func skipVarint( []byte) []byte {:= 0for ; []&0x80 != 0; ++ {}return [+1:]}// unmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.// It is called by Unmarshal methods on protocol buffer messages with the message_set_wire_format option.func unmarshalMessageSet( []byte, interface{}) error {var map[int32]Extensionswitch exts := .(type) {case *XXX_InternalExtensions:= .extensionsWrite()case map[int32]Extension:=default:return errors.New("proto: not an extension map")}:= new(messageSet)if := Unmarshal(, ); != nil {return}for , := range .Item {:= *.TypeId:= .Message// Restore wire type and field number varint, plus length varint.// Be careful to preserve duplicate items.:= EncodeVarint(uint64()<<3 | WireBytes)if , := []; {// Existing data; rip off the tag and length varint// so we join the new data correctly.// We can assume that ext.enc is set because we are unmarshaling.:= .enc[len():] // skip wire type and field number, := DecodeVarint() // calculate length of length varint= [:] // skip length varint= append(, ...) // join old data and new data}= append(, EncodeVarint(uint64(len()))...)= append(, ...)[] = Extension{enc: }}return nil}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |