當前位置: 首頁>>代碼示例>>Golang>>正文


Golang proto.Marshal函數代碼示例

本文整理匯總了Golang中github.com/gogo/protobuf/proto.Marshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Marshal函數的具體用法?Golang Marshal怎麽用?Golang Marshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Marshal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestRepeatedExtensionsFieldsIssue161

func TestRepeatedExtensionsFieldsIssue161(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	rep := 10
	ints := make([]int64, rep)
	for i := range ints {
		ints[i] = r.Int63()
	}
	input := &MyExtendable{}
	if err := proto.SetExtension(input, E_FieldD, ints); err != nil {
		t.Fatal(err)
	}
	data, err := proto.Marshal(input)
	if err != nil {
		t.Fatal(err)
	}
	output := &MyExtendable{}
	if err := proto.Unmarshal(data, output); err != nil {
		t.Fatal(err)
	}
	if !input.Equal(output) {
		t.Fatal("expected equal")
	}
	data2, err2 := proto.Marshal(output)
	if err2 != nil {
		t.Fatal(err2)
	}
	if len(data) != len(data2) {
		t.Fatal("expected equal length buffers")
	}
}
開發者ID:fd,項目名稱:protobuf,代碼行數:30,代碼來源:bug_test.go

示例2: TestNoMergeExtensionMerge

func TestNoMergeExtensionMerge(t *testing.T) {
	bigm := prototests.AContainer
	m := &prototests.Small{SmallField: proto.Int64(1)}
	data, err := proto.Marshal(bigm)
	if err != nil {
		panic(err)
	}
	mdata, err := proto.Marshal(m)
	if err != nil {
		panic(err)
	}
	key := uint32(101)<<3 | uint32(2)
	datakey := make([]byte, 10)
	n := binary.PutUvarint(datakey, uint64(key))
	datakey = datakey[:n]
	datalen := make([]byte, 10)
	n = binary.PutUvarint(datalen, uint64(len(mdata)))
	datalen = datalen[:n]
	data = append(data, append(datakey, append(datalen, mdata...)...)...)
	err = noMerge(data, bigm.Description(), "prototests", "Container")
	if err == nil || !strings.Contains(err.Error(), "FieldB requires merging") {
		t.Fatalf("FieldB should require merging, but error is %v", err)
	}
	t.Log(err)
}
開發者ID:katydid,項目名稱:katydid,代碼行數:25,代碼來源:nomerge_test.go

示例3: TestNoMergeExtensionMerge

func TestNoMergeExtensionMerge(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	bigm := test.NewPopulatedMyExtendable(r, true)
	m := test.NewPopulatedNinOptNative(r, true)
	err := proto.SetExtension(bigm, test.E_FieldB, m)
	if err != nil {
		panic(err)
	}
	data, err := proto.Marshal(bigm)
	if err != nil {
		panic(err)
	}
	key := uint32(101)<<3 | uint32(2)
	data2 := make([]byte, 10)
	n := binary.PutUvarint(data2, uint64(key))
	data2 = data2[:n]
	data = append(data, data2...)
	data4, err := proto.Marshal(test.NewPopulatedNinOptNative(r, true))
	if err != nil {
		panic(err)
	}
	data3 := make([]byte, 10)
	n = binary.PutUvarint(data3, uint64(len(data4)))
	data3 = data3[:n]
	data = append(data, data3...)
	data = append(data, data4...)
	err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "MyExtendable")
	if err == nil || !strings.Contains(err.Error(), "requires merging") {
		t.Fatalf("should require merging")
	}
}
開發者ID:nolenroyalty,項目名稱:bangarang,代碼行數:31,代碼來源:merge_test.go

示例4: WriteMsg

func (this *uint32Writer) WriteMsg(msg proto.Message) (err error) {
	var data []byte
	if m, ok := msg.(marshaler); ok {
		n, ok := getSize(m)
		if !ok {
			data, err = proto.Marshal(msg)
			if err != nil {
				return err
			}
		}
		if n >= len(this.buffer) {
			this.buffer = make([]byte, n)
		}
		_, err = m.MarshalTo(this.buffer)
		if err != nil {
			return err
		}
		data = this.buffer[:n]
	} else {
		data, err = proto.Marshal(msg)
		if err != nil {
			return err
		}
	}
	length := uint32(len(data))
	if err := binary.Write(this.w, this.byteOrder, &length); err != nil {
		return err
	}
	_, err = this.w.Write(data)
	return err
}
開發者ID:rhuss,項目名稱:gofabric8,代碼行數:31,代碼來源:uint32.go

示例5: TestOldUnoM

func TestOldUnoM(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	older := NewPopulatedOldUnoM(popr, true)
	// need optional field to be always initialized, to check it's lost in this test
	older.Field1 = proto.String(randStringUnrecognized(popr))
	data1, err := proto.Marshal(older)
	if err != nil {
		panic(err)
	}

	newer := &UnoM{}
	if err = proto.Unmarshal(data1, newer); err != nil {
		panic(err)
	}
	data2, err := proto.Marshal(newer)
	if err != nil {
		panic(err)
	}

	older2 := &OldUnoM{}
	if err := proto.Unmarshal(data2, older2); err != nil {
		panic(err)
	}

	// check that Field1 is lost
	if older2.Field1 != nil {
		t.Fatalf("field must be lost, but it's not, older: %#v, older2: %#v", older, older2)
	}

	// now restore Field1 and messages should be equal now
	older2.Field1 = older.Field1
	if err := older.VerboseEqual(older2); err != nil {
		t.Fatalf("%#v !VerboseProto %#v, since %v", older, older2, err)
	}
}
開發者ID:fd,項目名稱:protobuf,代碼行數:35,代碼來源:oldnew_test.go

示例6: makeCompoundMessage

// makeCompoundMessage takes a list of messages and generates
// a single compound message containing all of them
func makeCompoundMessage(msgs [][]byte) []byte {
	cMsg := CompoundMessage{}

	cMsg.Messages = make([]*CompoundMessage_SimpleMessage, 0, len(msgs))
	for _, m := range msgs {
		cMsg.Messages = append(cMsg.Messages, &CompoundMessage_SimpleMessage{
			Payload: m,
		})
	}

	buf, err := proto.Marshal(&cMsg)
	if err != nil {
		return nil
	}

	gMsg := GossipMessage{
		Type: MessageTypeCompound,
		Data: buf,
	}

	buf, err = proto.Marshal(&gMsg)
	if err != nil {
		return nil
	}

	return buf
}
開發者ID:CadeLaRen,項目名稱:docker-3,代碼行數:29,代碼來源:message.go

示例7: EmitLogMessage

func (e *LoggregatorEmitter) EmitLogMessage(logMessage *logmessage.LogMessage) {
	messages := splitMessage(string(logMessage.GetMessage()))

	for _, message := range messages {
		if isEmpty(message) {
			continue
		}

		if len(message) > MAX_MESSAGE_BYTE_SIZE {
			logMessage.Message = append([]byte(message)[0:TRUNCATED_OFFSET], TRUNCATED_BYTES...)
		} else {
			logMessage.Message = []byte(message)
		}
		if e.sharedSecret == "" {
			marshalledLogMessage, err := proto.Marshal(logMessage)
			if err != nil {
				e.logger.Errorf("Error marshalling message: %s", err)
				return
			}
			e.LoggregatorClient.Send(marshalledLogMessage)
		} else {
			logEnvelope, err := e.newLogEnvelope(*logMessage.AppId, logMessage)
			if err != nil {
				e.logger.Errorf("Error creating envelope: %s", err)
				return
			}
			marshalledLogEnvelope, err := proto.Marshal(logEnvelope)
			if err != nil {
				e.logger.Errorf("Error marshalling envelope: %s", err)
				return
			}
			e.LoggregatorClient.Send(marshalledLogEnvelope)
		}
	}
}
開發者ID:pkdevbox,項目名稱:loggregatorlib,代碼行數:35,代碼來源:emit.go

示例8: WriteMsg

func (this *fullWriter) WriteMsg(msg proto.Message) (err error) {
	var data []byte
	if m, ok := msg.(marshaler); ok {
		n, ok := getSize(m)
		if !ok {
			data, err = proto.Marshal(msg)
			if err != nil {
				return err
			}
		}
		if n >= len(this.buffer) {
			this.buffer = make([]byte, n)
		}
		_, err = m.MarshalTo(this.buffer)
		if err != nil {
			return err
		}
		data = this.buffer[:n]
	} else {
		data, err = proto.Marshal(msg)
		if err != nil {
			return err
		}
	}
	_, err = this.w.Write(data)
	return err
}
開發者ID:otsimo,項目名稱:otsimoctl,代碼行數:27,代碼來源:full.go

示例9: WriteMsg

func (this *varintWriter) WriteMsg(msg proto.Message) (err error) {
	var data []byte
	if m, ok := msg.(marshaler); ok {
		n, ok := getSize(m)
		if !ok {
			data, err = proto.Marshal(msg)
			if err != nil {
				return err
			}
		}
		if n >= len(this.buffer) {
			this.buffer = make([]byte, n)
		}
		_, err = m.MarshalTo(this.buffer)
		if err != nil {
			return err
		}
		data = this.buffer[:n]
	} else {
		data, err = proto.Marshal(msg)
		if err != nil {
			return err
		}
	}
	length := uint64(len(data))
	n := binary.PutUvarint(this.lenBuf, length)
	_, err = this.w.Write(this.lenBuf[:n])
	if err != nil {
		return err
	}
	_, err = this.w.Write(data)
	return err
}
開發者ID:otsimo,項目名稱:otsimoctl,代碼行數:33,代碼來源:varint.go

示例10: TestRepeatedExtensionsMsgsIssue161

func TestRepeatedExtensionsMsgsIssue161(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	rep := 10
	nins := make([]*NinOptNative, rep)
	for i := range nins {
		nins[i] = NewPopulatedNinOptNative(r, true)
	}
	input := &MyExtendable{}
	if err := proto.SetExtension(input, E_FieldE, nins); err != nil {
		t.Fatal(err)
	}
	data, err := proto.Marshal(input)
	if err != nil {
		t.Fatal(err)
	}
	output := &MyExtendable{}
	if err := proto.Unmarshal(data, output); err != nil {
		t.Fatal(err)
	}
	if !input.Equal(output) {
		t.Fatal("expected equal")
	}
	data2, err2 := proto.Marshal(output)
	if err2 != nil {
		t.Fatal(err2)
	}
	if len(data) != len(data2) {
		t.Fatal("expected equal length buffers")
	}
}
開發者ID:fd,項目名稱:protobuf,代碼行數:30,代碼來源:bug_test.go

示例11: Create

func (o *OortFS) Create(ctx context.Context, parent, id []byte, inode uint64, name string, attr *pb.Attr, isdir bool) (string, *pb.Attr, error) {
	// Check to see if the name already exists
	b, err := o.comms.ReadGroupItem(ctx, parent, []byte(name))
	if err != nil && !store.IsNotFound(err) {
		// TODO: Needs beter error handling
		return "", &pb.Attr{}, err
	}
	p := &pb.DirEntry{}
	err = proto.Unmarshal(b, p)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	// Return an error if entry already exists and is not a tombstone
	if len(b) > 0 && p.Tombstone == nil {
		return "", &pb.Attr{}, nil
	}
	var direntType fuse.DirentType
	if isdir {
		direntType = fuse.DT_Dir
	} else {
		direntType = fuse.DT_File
	}
	// Add the name to the group
	d := &pb.DirEntry{
		Version: DirEntryVersion,
		Name:    name,
		Id:      id,
		Type:    uint32(direntType),
	}
	b, err = proto.Marshal(d)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	err = o.comms.WriteGroup(ctx, parent, []byte(name), b)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	// Add the inode entry
	n := &pb.InodeEntry{
		Version: InodeEntryVersion,
		Inode:   inode,
		IsDir:   isdir,
		Attr:    attr,
		Blocks:  0,
	}
	b, err = proto.Marshal(n)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	err = o.WriteChunk(ctx, id, b)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	return name, attr, nil
}
開發者ID:getcfs,項目名稱:cfs-binary-release,代碼行數:55,代碼來源:file.go

示例12: Create

func (o *OortFS) Create(ctx context.Context, parent, id []byte, inode uint64, name string, attr *pb.Attr, isdir bool) (string, *pb.Attr, error) {
	v, err := o.validateIP(ctx)
	if err != nil {
		return "", nil, err
	}
	if !v {
		return "", nil, errors.New("Unknown or unauthorized FS use")
	}
	// Check to see if the name already exists
	b, err := o.comms.ReadGroupItem(ctx, parent, []byte(name))
	if err != nil && !store.IsNotFound(err) {
		// TODO: Needs beter error handling
		return "", &pb.Attr{}, err
	}
	if len(b) > 0 {
		return "", &pb.Attr{}, nil
	}
	p := &pb.DirEntry{}
	err = proto.Unmarshal(b, p)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	// Add the name to the group
	d := &pb.DirEntry{
		Version: DirEntryVersion,
		Name:    name,
		Id:      id,
	}
	b, err = proto.Marshal(d)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	err = o.comms.WriteGroup(ctx, parent, []byte(name), b)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	// Add the inode entry
	n := &pb.InodeEntry{
		Version: InodeEntryVersion,
		Inode:   inode,
		IsDir:   isdir,
		Attr:    attr,
		Blocks:  0,
	}
	b, err = proto.Marshal(n)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	err = o.WriteChunk(ctx, id, b)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	return name, attr, nil
}
開發者ID:wreese,項目名稱:cfs-binary-release,代碼行數:54,代碼來源:file.go

示例13: writeResponse

func writeResponse(w io.Writer, id uint64, serr string, response proto.Message) (err error) {
	// clean response if error
	if serr != "" {
		response = nil
	}

	// marshal response
	pbResponse := []byte{}
	if response != nil {
		pbResponse, err = proto.Marshal(response)
		if err != nil {
			return err
		}
	}

	// compress serialized proto data
	compressedPbResponse, err := snappy.Encode(nil, pbResponse)
	if err != nil {
		return err
	}

	// generate header
	header := &wire.ResponseHeader{
		Id:                          id,
		Error:                       serr,
		RawResponseLen:              uint32(len(pbResponse)),
		SnappyCompressedResponseLen: uint32(len(compressedPbResponse)),
		Checksum:                    crc32.ChecksumIEEE(compressedPbResponse),
	}

	// check header size
	pbHeader, err := proto.Marshal(header)
	if err != err {
		return
	}
	if uint32(len(pbHeader)) > wire.Default_Const_MaxHeaderLen {
		return fmt.Errorf("protorpc.writeResponse: header larger than max_header_len: %d.",
			len(pbHeader),
		)
	}

	// send header (more)
	if err = sendFrame(w, pbHeader); err != nil {
		return
	}

	// send body (end)
	if err = sendFrame(w, compressedPbResponse); err != nil {
		return
	}

	return nil
}
開發者ID:josephwinston,項目名稱:cockroach,代碼行數:53,代碼來源:wire.go

示例14: Symlink

func (o *OortFS) Symlink(ctx context.Context, parent, id []byte, name string, target string, attr *pb.Attr, inode uint64) (*pb.SymlinkResponse, error) {
	v, err := o.validateIP(ctx)
	if err != nil {
		return nil, err
	}
	if !v {
		return nil, errors.New("Unknown or unauthorized FS use")
	}
	// Check to see if the name exists
	val, err := o.comms.ReadGroupItem(ctx, parent, []byte(name))
	if err != nil && !store.IsNotFound(err) {
		// TODO: Needs beter error handling
		return &pb.SymlinkResponse{}, err
	}
	if len(val) > 1 { // Exists already
		return &pb.SymlinkResponse{}, nil
	}
	n := &pb.InodeEntry{
		Version: InodeEntryVersion,
		Inode:   inode,
		IsDir:   false,
		IsLink:  true,
		Target:  target,
		Attr:    attr,
	}
	b, err := proto.Marshal(n)
	if err != nil {
		return &pb.SymlinkResponse{}, err
	}
	err = o.WriteChunk(ctx, id, b)
	if err != nil {
		return &pb.SymlinkResponse{}, err
	}
	// Add the name to the group
	d := &pb.DirEntry{
		Version: DirEntryVersion,
		Name:    name,
		Id:      id,
	}
	b, err = proto.Marshal(d)
	if err != nil {
		return &pb.SymlinkResponse{}, err
	}
	err = o.comms.WriteGroup(ctx, parent, []byte(name), b)
	if err != nil {
		return &pb.SymlinkResponse{}, err
	}
	return &pb.SymlinkResponse{Name: name, Attr: attr}, nil
}
開發者ID:wreese,項目名稱:cfs-binary-release,代碼行數:49,代碼來源:file.go

示例15: updateRangeDescriptor

// updateRangeDescriptor adds a ConditionalPut on the range descriptor. The
// conditional put verifies that changes to the range descriptor are made in a
// well-defined order, preventing a scenario where a wayward replica which is
// no longer part of the original Raft group comes back online to form a
// splinter group with a node which was also a former replica, and hijacks the
// range descriptor. This is a last line of defense; other mechanisms should
// prevent rogue replicas from getting this far (see #768).
func updateRangeDescriptor(b *client.Batch, descKey proto.Key, oldDesc, newDesc *proto.RangeDescriptor) error {
	var oldValue []byte
	if oldDesc != nil {
		var err error
		if oldValue, err = gogoproto.Marshal(oldDesc); err != nil {
			return err
		}
	}
	newValue, err := gogoproto.Marshal(newDesc)
	if err != nil {
		return err
	}
	b.CPut(descKey, newValue, oldValue)
	return nil
}
開發者ID:simonzhangsm,項目名稱:cockroach,代碼行數:22,代碼來源:range_command.go


注:本文中的github.com/gogo/protobuf/proto.Marshal函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。