当前位置: 首页>>代码示例>>Golang>>正文


Golang proto.Merge函数代码示例

本文整理汇总了Golang中github.com/golang/protobuf/proto.Merge函数的典型用法代码示例。如果您正苦于以下问题:Golang Merge函数的具体用法?Golang Merge怎么用?Golang Merge使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Merge函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: PutMulti

// PutMulti is a batch version of Put. One PendingKey is returned for each
// element of src in the same order.
func (t *Transaction) PutMulti(keys []*Key, src interface{}) ([]*PendingKey, error) {
	if t.id == nil {
		return nil, errExpiredTransaction
	}
	mutation, err := putMutation(keys, src)
	if err != nil {
		return nil, err
	}
	proto.Merge(t.mutation, mutation)

	// Prepare the returned handles, pre-populating where possible.
	ret := make([]*PendingKey, len(keys))
	for i, key := range keys {
		h := &PendingKey{}
		if key.Incomplete() {
			// This key will be in the final commit result.
			t.pending = append(t.pending, h)
		} else {
			h.key = key
		}

		ret[i] = h
	}

	return ret, nil
}
开发者ID:angelabier1,项目名称:pachyderm,代码行数:28,代码来源:transaction.go

示例2: EnumerateTrashedMembers

// Get a list of all members which are currently in the trash.
func (m *MembershipDB) EnumerateTrashedMembers(prev string, num int32) ([]*MemberWithKey, error) {
	var cp *cassandra.ColumnParent = cassandra.NewColumnParent()
	var pred *cassandra.SlicePredicate = cassandra.NewSlicePredicate()
	var r *cassandra.KeyRange = cassandra.NewKeyRange()
	var kss []*cassandra.KeySlice
	var ks *cassandra.KeySlice
	var rv []*MemberWithKey
	var err error

	// Fetch the protobuf column of the application column family.
	cp.ColumnFamily = "membership_archive"
	pred.ColumnNames = [][]byte{
		[]byte("pb_data"),
	}
	if len(prev) > 0 {
		var uuid cassandra.UUID
		if uuid, err = cassandra.ParseUUID(prev); err != nil {
			return rv, err
		}
		r.StartKey = append([]byte(archivePrefix), []byte(uuid)...)
	} else {
		r.StartKey = []byte(archivePrefix)
	}
	r.EndKey = []byte(archiveEnd)
	r.Count = num

	kss, err = m.conn.GetRangeSlices(cp, pred, r, cassandra.ConsistencyLevel_ONE)
	if err != nil {
		return rv, err
	}

	for _, ks = range kss {
		var member *MemberWithKey
		var scol *cassandra.ColumnOrSuperColumn
		var uuid cassandra.UUID = cassandra.UUIDFromBytes(
			ks.Key[len(archivePrefix):])

		if len(ks.Columns) == 0 {
			continue
		}

		for _, scol = range ks.Columns {
			var col *cassandra.Column = scol.Column

			if string(col.Name) == "pb_data" {
				var agreement = new(MembershipAgreement)
				member = new(MemberWithKey)
				err = proto.Unmarshal(col.Value, agreement)
				proto.Merge(&member.Member, agreement.GetMemberData())
				member.Key = uuid.String()
			}
		}

		if member != nil {
			rv = append(rv, member)
		}
	}

	return rv, nil
}
开发者ID:starshipfactory,项目名称:membersys,代码行数:61,代码来源:db.go

示例3: Lookup

func (t testProtoTable) Lookup(_ context.Context, key []byte, msg proto.Message) error {
	m, ok := t[string(key)]
	if !ok {
		return table.ErrNoSuchKey
	}
	proto.Merge(msg, m)
	return nil
}
开发者ID:jwatt,项目名称:kythe,代码行数:8,代码来源:xrefs_test.go

示例4: Lookup

func (t testProtoTable) Lookup(_ context.Context, key []byte, msg proto.Message) error {
	m, ok := t[string(key)]
	if !ok {
		log.Println("Failed to find key", string(key))
		return table.ErrNoSuchKey
	}
	proto.Merge(msg, m)
	return nil
}
开发者ID:pedrokiefer,项目名称:kythe,代码行数:9,代码来源:xrefs_test.go

示例5: TestMerge

func TestMerge(t *testing.T) {
	for _, m := range mergeTests {
		got := proto.Clone(m.dst)
		proto.Merge(got, m.src)
		if !proto.Equal(got, m.want) {
			t.Errorf("Merge(%v, %v)\n got %v\nwant %v\n", m.dst, m.src, got, m.want)
		}
	}
}
开发者ID:zhangcunli,项目名称:go-demo,代码行数:9,代码来源:clone_test.go

示例6: DeleteMulti

// DeleteMulti is a batch version of Delete.
func (t *Transaction) DeleteMulti(keys []*Key) error {
	if t.id == nil {
		return errExpiredTransaction
	}
	mutation, err := deleteMutation(keys)
	if err != nil {
		return err
	}
	proto.Merge(t.mutation, mutation)
	return nil
}
开发者ID:hanscj1,项目名称:mantle,代码行数:12,代码来源:transaction.go

示例7: TestProtoMerge

func TestProtoMerge(t *testing.T) {
	person := InitData("ronaflx", 195936, "[email protected]")
	merged := InitData("flx", 1990, "[email protected]")
	proto.Merge(merged, person) // The first param is destination
	if merged.GetName() != "ronaflx" {
		t.Error("expect merged.name == ronaflx", merged.GetName())
	}
	if merged.GetId() != 195936 {
		t.Error("expect merged.id == 195936", merged.GetId())
	}
	if merged.GetEmail() != "[email protected]" {
		t.Error("expect merged.email == [email protected]", merged.GetEmail())
	}
}
开发者ID:ronaflx,项目名称:opensource-tutorial,代码行数:14,代码来源:proto_test.go

示例8: updateConfiguration

func (m *MaxReplicationLagModule) updateConfiguration(configuration *throttlerdata.Configuration, copyZeroValues bool) error {
	m.mutableConfigMu.Lock()
	defer m.mutableConfigMu.Unlock()

	newConfig := m.mutableConfig

	if copyZeroValues {
		newConfig.Configuration = *proto.Clone(configuration).(*throttlerdata.Configuration)
	} else {
		proto.Merge(&newConfig.Configuration, configuration)
	}

	if err := newConfig.Verify(); err != nil {
		return err
	}
	m.mutableConfig = newConfig
	m.applyMutableConfig = true
	return nil
}
开发者ID:erzel,项目名称:vitess,代码行数:19,代码来源:max_replication_lag_module.go

示例9: LoadMetadata

func (fs *AppendFS) LoadMetadata() error {
	ret := (error)(nil)
	fs.metadataMutex.Lock()
	nodes := make(map[uint64]*messages.NodeMetadata)
	children := make(map[uint64][]uint64)
	_, err := fs.metadataFile.Seek(0, 0)
	bufferedReader := bufio.NewReader(fs.metadataFile)
	if err != nil {
		ret = err
		goto Finally
	}
	for {
		msgLen, err := binary.ReadUvarint(bufferedReader)
		if err != nil {
			// EOF is ok here
			if err == io.EOF {
				fmt.Println("Reached expected EOF")
				break
			}
			ret = err
			goto Finally
		}
		msgBuf := make([]byte, msgLen)
		//fmt.Printf("MsgLen: %d\n", msgLen)
		_, err = io.ReadAtLeast(bufferedReader, msgBuf, int(msgLen))
		if err != nil {
			ret = err
			goto Finally
		}
		metadata := &messages.NodeMetadata{}
		err = proto.Unmarshal(msgBuf, metadata)
		if err != nil {
			ret = err
			goto Finally
		}
		if currentNode, ok := nodes[metadata.GetNodeId()]; ok {
			if metadata.Contents != nil {
				currentNode.Contents = nil
			}
			proto.Merge(currentNode, metadata)
			//fmt.Printf("Merged %d\n", currentNode.GetNodeId())
		} else {
			nodes[metadata.GetNodeId()] = metadata
			//fmt.Printf("Added %d\n", metadata.GetNodeId())
		}
	}
	for id, node := range nodes {
		fs.seenNodeId(id)
		if !node.GetValid() {
			continue
		}
		if node.ParentNodeId != nil {
			if currentChildren, ok := children[node.GetParentNodeId()]; ok {
				children[node.GetParentNodeId()] = append(currentChildren, id)
			} else {
				children[node.GetParentNodeId()] = append(make([]uint64, 0), id)
			}
		} else {
			ret = errors.New("Corrupt metadata: missing ParentFileId for file " + string(id))
			goto Finally
		}
	}

	fs.addChildrenHelper(nodes, children, fs.root)

Finally:
	fs.metadataMutex.Unlock()
	return ret
}
开发者ID:e-tothe-ipi,项目名称:appendfs,代码行数:69,代码来源:appendfs.go

示例10: TestUnmarshalRepeatingNonRepeatedExtension

func TestUnmarshalRepeatingNonRepeatedExtension(t *testing.T) {
	// We may see multiple instances of the same extension in the wire
	// format. For example, the proto compiler may encode custom options in
	// this way. Here, we verify that we merge the extensions together.
	tests := []struct {
		name string
		ext  []*pb.ComplexExtension
	}{
		{
			"two fields",
			[]*pb.ComplexExtension{
				{First: proto.Int32(7)},
				{Second: proto.Int32(11)},
			},
		},
		{
			"repeated field",
			[]*pb.ComplexExtension{
				{Third: []int32{1000}},
				{Third: []int32{2000}},
			},
		},
		{
			"two fields and repeated field",
			[]*pb.ComplexExtension{
				{Third: []int32{1000}},
				{First: proto.Int32(9)},
				{Second: proto.Int32(21)},
				{Third: []int32{2000}},
			},
		},
	}
	for _, test := range tests {
		var buf bytes.Buffer
		var want pb.ComplexExtension

		// Generate a serialized representation of a repeated extension
		// by catenating bytes together.
		for i, e := range test.ext {
			// Merge to create the wanted proto.
			proto.Merge(&want, e)

			// serialize the message
			msg := new(pb.OtherMessage)
			err := proto.SetExtension(msg, pb.E_Complex, e)
			if err != nil {
				t.Fatalf("[%s] Error setting extension %d: %v", test.name, i, err)
			}
			b, err := proto.Marshal(msg)
			if err != nil {
				t.Fatalf("[%s] Error marshaling message %d: %v", test.name, i, err)
			}
			buf.Write(b)
		}

		// Unmarshal and read the merged proto.
		msg2 := new(pb.OtherMessage)
		err := proto.Unmarshal(buf.Bytes(), msg2)
		if err != nil {
			t.Fatalf("[%s] Error unmarshaling message: %v", test.name, err)
		}
		e, err := proto.GetExtension(msg2, pb.E_Complex)
		if err != nil {
			t.Fatalf("[%s] Error getting extension: %v", test.name, err)
		}
		ext := e.(*pb.ComplexExtension)
		if ext == nil {
			t.Fatalf("[%s] Invalid extension", test.name)
		}
		if !reflect.DeepEqual(*ext, want) {
			t.Errorf("[%s] Wrong value for ComplexExtension: got: %s want: %s\n", test.name, ext, want)
		}
	}
}
开发者ID:ZurgInq,项目名称:protogame,代码行数:74,代码来源:extensions_test.go

示例11: ServeHTTP

// Output a JSON list of all applicants currently waiting to become members.
func (a *ApplicantListHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	var applist applicantListType
	var enc *json.Encoder
	var err error

	if !a.auth.IsAuthenticatedScope(req, a.admingroup) {
		rw.WriteHeader(http.StatusUnauthorized)
		return
	}

	if req.FormValue("single") == "true" && len(req.FormValue("start")) > 0 {
		var memberreq *membersys.MembershipAgreement
		var mwk *membersys.MemberWithKey
		var bigint *big.Int = big.NewInt(0)
		var uuid cassandra.UUID
		var ok bool
		bigint, ok = bigint.SetString(req.FormValue("start"), 10)
		if !ok {
			rw.WriteHeader(http.StatusInternalServerError)
			rw.Write([]byte("Unable to parse " + req.FormValue("start") +
				" as a number"))
			return
		}
		if bigint.BitLen() == 128 {
			uuid = cassandra.UUIDFromBytes(bigint.Bytes())
			memberreq, _, err = a.database.GetMembershipRequest(
				uuid.String(), "application", "applicant:")
			if err != nil {
				rw.WriteHeader(http.StatusInternalServerError)
				rw.Write([]byte("Unable to retrieve the membership request " +
					uuid.String() + ": " + err.Error()))
				return
			}
			mwk = new(membersys.MemberWithKey)
			mwk.Key = uuid.String()
			proto.Merge(&mwk.Member, memberreq.GetMemberData())
			applist.Applicants = []*membersys.MemberWithKey{mwk}
		}
	} else {
		applist.Applicants, err = a.database.EnumerateMembershipRequests(
			req.FormValue("criterion"), req.FormValue("start"), a.pagesize)
		if err != nil {
			log.Print("Error enumerating applications: ", err)
			rw.WriteHeader(http.StatusInternalServerError)
			rw.Write([]byte("Error enumerating applications: " + err.Error()))
			return
		}
	}

	applist.AgreementUploadCsrfToken, err = a.auth.GenCSRFToken(
		req, applicantAgreementUploadURL, 10*time.Minute)
	if err != nil {
		log.Print("Error generating CSRF token: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error generating CSRF token: " + err.Error()))
		return
	}

	applist.ApprovalCsrfToken, err = a.auth.GenCSRFToken(
		req, applicantApprovalURL, 10*time.Minute)
	if err != nil {
		log.Print("Error generating CSRF token: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error generating CSRF token: " + err.Error()))
		return
	}

	applist.RejectionCsrfToken, err = a.auth.GenCSRFToken(
		req, applicantRejectionURL, 10*time.Minute)
	if err != nil {
		log.Print("Error generating CSRF token: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error generating CSRF token: " + err.Error()))
		return
	}

	rw.Header().Set("Content-Type", "application/json; encoding=utf8")
	enc = json.NewEncoder(rw)
	if err = enc.Encode(applist); err != nil {
		log.Print("Error JSON encoding member list: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error encoding result: " + err.Error()))
		return
	}
}
开发者ID:starshipfactory,项目名称:membersys,代码行数:86,代码来源:applicantlist.go

示例12: fuzz


//.........这里部分代码省略.........
		func() proto.Message { return new(pb.M23) },
		func() proto.Message { return new(pb.M24) },
		func() proto.Message { return new(pb.M25) },
	}
	datas := ""
	if text {
		datas = string(data)
	}
	score := 0
	for _, ctor := range ctors {
		v := ctor()
		var err error
		if text {
			err = proto.UnmarshalText(datas, v)
		} else {
			err = proto.Unmarshal(data, v)
		}
		if err != nil {
			continue
		}
		score = 1
		sz := proto.Size(v)
		var data1 []byte
		if text {
			var buf bytes.Buffer
			err = proto.MarshalText(&buf, v)
			data1 = buf.Bytes()
		} else {
			data1, err = proto.Marshal(v)
		}
		if err != nil {
			panic(err)
		}
		v1 := ctor()
		if text {
			err = proto.UnmarshalText(string(data1), v1)
		} else {
			if sz != len(data1) {
				panic(fmt.Sprintf("Size returned %v, while Marshal returned %v", sz, len(data1)))
			}
			err = proto.Unmarshal(data1, v1)
		}
		if err != nil {
			panic(err)
		}
		if !DeepEqual(v, v1) {
			fmt.Printf("v0: %#v\n", v)
			fmt.Printf("v1: %#v\n", v1)
			panic(fmt.Sprintf("non idempotent marshal of %T", v))
		}
		if !proto.Equal(v, v1) {
			fmt.Printf("v0: %#v\n", v)
			fmt.Printf("v1: %#v\n", v1)
			panic(fmt.Sprintf("non idempotent marshal of %T", v))
		}
		if text {
			// TODO: Marshal/Unmarshal to binary.
		} else {
			var buf bytes.Buffer
			err := proto.MarshalText(&buf, v)
			if err != nil {
				fmt.Printf("failed to MarshalText: %#v\n", v)
				panic(err)
			}
			v2 := ctor()
			err = proto.UnmarshalText(buf.String(), v2)
			if err != nil {
				if strings.Contains(err.Error(), "unexpected byte 0x2f") {
					continue // known bug
				}
				fmt.Printf("failed to UnmarshalText: %q\n", buf.Bytes())
				panic(err)
			}
			if !proto.Equal(v, v2) {
				fmt.Printf("v0: %#v\n", v)
				fmt.Printf("v2: %#v\n", v2)
				panic(fmt.Sprintf("non idempotent text marshal of %T", v))
			}
		}
		v3 := proto.Clone(v)
		if !DeepEqual(v, v3) {
			fmt.Printf("v0: %#v\n", v)
			fmt.Printf("v3: %#v\n", v3)
			panic(fmt.Sprintf("bad clone of %T", v))
		}
		proto.SetDefaults(v3)
		if !proto.Equal(v, v3) {
			fmt.Printf("v0: %#v\n", v)
			fmt.Printf("v3: %#v\n", v3)
			panic(fmt.Sprintf("SetDefaults changed data %T", v))
		}
		proto.Merge(v3, v1)
		if idempotentMerge(v) && !proto.Equal(v, v3) {
			fmt.Printf("v0: %#v\n", v)
			fmt.Printf("v3: %#v\n", v3)
			panic(fmt.Sprintf("Merge changed data %T", v))
		}
	}
	return score
}
开发者ID:pcasaretto,项目名称:go-fuzz,代码行数:101,代码来源:main.go


注:本文中的github.com/golang/protobuf/proto.Merge函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。