本文整理汇总了Golang中github.com/gogo/protobuf/proto.Clone函数的典型用法代码示例。如果您正苦于以下问题:Golang Clone函数的具体用法?Golang Clone怎么用?Golang Clone使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Clone函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: newTestSender
// TestSender mocks out some of the txn coordinator sender's
// functionality. It responds to PutRequests using testPutResp.
func newTestSender(pre, post func(roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error)) SenderFunc {
txnKey := roachpb.Key("test-txn")
txnID := []byte(uuid.NewUUID4())
return func(_ context.Context, ba roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) {
ba.UserPriority = proto.Int32(-1)
if ba.Txn != nil && len(ba.Txn.ID) == 0 {
ba.Txn.Key = txnKey
ba.Txn.ID = txnID
}
var br *roachpb.BatchResponse
var pErr *roachpb.Error
if pre != nil {
br, pErr = pre(ba)
} else {
br = ba.CreateReply()
}
if pErr != nil {
return nil, pErr
}
var writing bool
status := roachpb.PENDING
for i, req := range ba.Requests {
args := req.GetInner()
if _, ok := args.(*roachpb.PutRequest); ok {
if !br.Responses[i].SetValue(proto.Clone(testPutResp).(roachpb.Response)) {
panic("failed to set put response")
}
}
if roachpb.IsTransactionWrite(args) {
writing = true
}
}
if args, ok := ba.GetArg(roachpb.EndTransaction); ok {
et := args.(*roachpb.EndTransactionRequest)
writing = true
if et.Commit {
status = roachpb.COMMITTED
} else {
status = roachpb.ABORTED
}
}
br.Txn = proto.Clone(ba.Txn).(*roachpb.Transaction)
if br.Txn != nil && pErr == nil {
br.Txn.Writing = writing
br.Txn.Status = status
}
if post != nil {
br, pErr = post(ba)
}
return br, pErr
}
}
示例2: newTestSender
func newTestSender(pre, post func(proto.BatchRequest) (*proto.BatchResponse, *proto.Error)) SenderFunc {
txnKey := proto.Key("test-txn")
txnID := []byte(uuid.NewUUID4())
return func(_ context.Context, ba proto.BatchRequest) (*proto.BatchResponse, *proto.Error) {
ba.UserPriority = gogoproto.Int32(-1)
if ba.Txn != nil && len(ba.Txn.ID) == 0 {
ba.Txn.Key = txnKey
ba.Txn.ID = txnID
}
var br *proto.BatchResponse
var pErr *proto.Error
if pre != nil {
br, pErr = pre(ba)
} else {
br = &proto.BatchResponse{}
}
if pErr != nil {
return nil, pErr
}
var writing bool
status := proto.PENDING
if _, ok := ba.GetArg(proto.Put); ok {
br.Add(gogoproto.Clone(testPutResp).(proto.Response))
writing = true
}
if args, ok := ba.GetArg(proto.EndTransaction); ok {
et := args.(*proto.EndTransactionRequest)
writing = true
if et.Commit {
status = proto.COMMITTED
} else {
status = proto.ABORTED
}
}
br.Txn = gogoproto.Clone(ba.Txn).(*proto.Transaction)
if br.Txn != nil && pErr == nil {
br.Txn.Writing = writing
br.Txn.Status = status
}
if post != nil {
br, pErr = post(ba)
}
return br, pErr
}
}
示例3: applyMutations
// Future home of the aysynchronous schema changer that picks up
// queued schema changes and processes them.
//
// applyMutations applies the queued mutations for a table.
// TODO(vivek): Eliminate the need to pass in tableName.
func (p *planner) applyMutations(tableDesc *TableDescriptor, tableName *parser.QualifiedName) error {
if len(tableDesc.Mutations) == 0 {
return nil
}
newTableDesc := proto.Clone(tableDesc).(*TableDescriptor)
// Make all mutations active.
for _, mutation := range newTableDesc.Mutations {
newTableDesc.makeMutationComplete(mutation)
}
newTableDesc.Mutations = nil
if err := newTableDesc.Validate(); err != nil {
return err
}
b := client.Batch{}
if err := p.backfillBatch(&b, tableName, tableDesc, newTableDesc); err != nil {
return err
}
// TODO(pmattis): This is a hack. Remove when schema change operations work
// properly.
p.hackNoteSchemaChange(newTableDesc)
b.Put(MakeDescMetadataKey(newTableDesc.GetID()), wrapDescriptor(newTableDesc))
if err := p.txn.Run(&b); err != nil {
return convertBatchError(newTableDesc, b, err)
}
return nil
}
示例4: maybeWrap
// MaybeWrap wraps the given argument in a batch, unless it is already one.
func maybeWrap(args proto.Request) (*proto.BatchRequest, func(*proto.BatchResponse) proto.Response) {
if ba, ok := args.(*proto.BatchRequest); ok {
return ba, func(br *proto.BatchResponse) proto.Response { return br }
}
ba := &proto.BatchRequest{}
ba.RequestHeader = *(gogoproto.Clone(args.Header()).(*proto.RequestHeader))
ba.Add(args)
return ba, func(br *proto.BatchResponse) proto.Response {
var unwrappedReply proto.Response
if len(br.Responses) == 0 {
unwrappedReply = args.CreateReply()
} else {
unwrappedReply = br.Responses[0].GetInner()
}
// The ReplyTxn is propagated from one response to the next request,
// and we adopt the mechanism that whenever the Txn changes, it needs
// to be set in the reply, for example to ratched up the transaction
// timestamp on writes when necessary.
// This is internally necessary to sequentially execute the batch,
// so it makes some sense to take the burden of updating the Txn
// from TxnCoordSender - it will only need to act on retries/aborts
// in the future.
unwrappedReply.Header().Txn = br.Txn
if unwrappedReply.Header().Error == nil {
unwrappedReply.Header().Error = br.Error
}
return unwrappedReply
}
}
示例5: Send
// Send implements the client.Sender interface.
func (rls *retryableLocalSender) Send(_ context.Context, call proto.Call) {
// Instant retry to handle the case of a range split, which is
// exposed here as a RangeKeyMismatchError.
retryOpts := retry.Options{}
// In local tests, the RPCs are not actually sent over the wire. We
// need to clone the Txn in order to avoid unexpected sharing
// between TxnCoordSender and client.Txn.
if header := call.Args.Header(); header.Txn != nil {
header.Txn = gogoproto.Clone(header.Txn).(*proto.Transaction)
}
var err error
for r := retry.Start(retryOpts); r.Next(); {
call.Reply.Header().Error = nil
rls.LocalSender.Send(context.TODO(), call)
// Check for range key mismatch error (this could happen if
// range was split between lookup and execution). In this case,
// reset header.Replica and engage retry loop.
if err = call.Reply.Header().GoError(); err != nil {
if _, ok := err.(*proto.RangeKeyMismatchError); ok {
// Clear request replica.
call.Args.Header().Replica = proto.Replica{}
log.Warning(err)
continue
}
}
return
}
panic(fmt.Sprintf("local sender did not succeed: %s", err))
}
示例6: getTableLease
// getTableLease acquires a lease for the specified table. The lease will be
// released when the planner closes.
func (p *planner) getTableLease(qname *parser.QualifiedName) (*TableDescriptor, *roachpb.Error) {
if err := qname.NormalizeTableName(p.session.Database); err != nil {
return nil, roachpb.NewError(err)
}
if qname.Database() == systemDB.Name || testDisableTableLeases {
// We don't go through the normal lease mechanism for system tables. The
// system.lease and system.descriptor table, in particular, are problematic
// because they are used for acquiring leases itself, creating a
// chicken&egg problem.
return p.getTableDesc(qname)
}
tableID, pErr := p.getTableID(qname)
if pErr != nil {
return nil, pErr
}
if p.leases == nil {
p.leases = make(map[ID]*LeaseState)
}
lease, ok := p.leases[tableID]
if !ok {
var pErr *roachpb.Error
lease, pErr = p.leaseMgr.Acquire(p.txn, tableID, 0)
if pErr != nil {
return nil, pErr
}
p.leases[tableID] = lease
}
return proto.Clone(&lease.TableDescriptor).(*TableDescriptor), nil
}
示例7: newTestSender
func newTestSender(handler func(proto.Call)) SenderFunc {
txnKey := proto.Key("test-txn")
txnID := []byte(uuid.NewUUID4())
return func(_ context.Context, call proto.Call) {
header := call.Args.Header()
header.UserPriority = gogoproto.Int32(-1)
if header.Txn != nil && len(header.Txn.ID) == 0 {
header.Txn.Key = txnKey
header.Txn.ID = txnID
}
call.Reply.Reset()
var writing bool
switch call.Args.(type) {
case *proto.PutRequest:
gogoproto.Merge(call.Reply, testPutResp)
writing = true
case *proto.EndTransactionRequest:
writing = true
default:
// Do nothing.
}
call.Reply.Header().Txn = gogoproto.Clone(call.Args.Header().Txn).(*proto.Transaction)
if txn := call.Reply.Header().Txn; txn != nil {
txn.Writing = writing
}
if handler != nil {
handler(call)
}
}
}
示例8: TestClone
func TestClone(t *testing.T) {
m := proto.Clone(cloneTestMessage).(*pb.MyMessage)
if !proto.Equal(m, cloneTestMessage) {
t.Errorf("Clone(%v) = %v", cloneTestMessage, m)
}
// Verify it was a deep copy.
*m.Inner.Port++
if proto.Equal(m, cloneTestMessage) {
t.Error("Mutating clone changed the original")
}
// Byte fields and repeated fields should be copied.
if &m.Pet[0] == &cloneTestMessage.Pet[0] {
t.Error("Pet: repeated field not copied")
}
if &m.Others[0] == &cloneTestMessage.Others[0] {
t.Error("Others: repeated field not copied")
}
if &m.Others[0].Value[0] == &cloneTestMessage.Others[0].Value[0] {
t.Error("Others[0].Value: bytes field not copied")
}
if &m.RepBytes[0] == &cloneTestMessage.RepBytes[0] {
t.Error("RepBytes: repeated field not copied")
}
if &m.RepBytes[0][0] == &cloneTestMessage.RepBytes[0][0] {
t.Error("RepBytes[0]: bytes field not copied")
}
}
示例9: StartWithStopper
// StartWithStopper is the same as Start, but allows passing a stopper
// explicitly.
func (ts *TestServer) StartWithStopper(stopper *stop.Stopper) error {
if ts.Ctx == nil {
ts.Ctx = NewTestContext()
}
if stopper == nil {
stopper = stop.NewStopper()
}
// Change the replication requirements so we don't get log spam
// about ranges not being replicated enough.
// TODO(marc): set this in the zones table when we have an entry
// for the default cluster-wide zone config and remove these
// shenanigans about mutating the global default.
oldDefaultZC := proto.Clone(config.DefaultZoneConfig).(*config.ZoneConfig)
config.DefaultZoneConfig.ReplicaAttrs = []roachpb.Attributes{{}}
stopper.AddCloser(stop.CloserFn(func() {
config.DefaultZoneConfig = oldDefaultZC
}))
var err error
ts.Server, err = NewServer(ts.Ctx, stopper)
if err != nil {
return err
}
// Ensure we have the correct number of engines. Add in in-memory ones where
// needed. There must be at least one store/engine.
if ts.StoresPerNode < 1 {
ts.StoresPerNode = 1
}
for i := len(ts.Ctx.Engines); i < ts.StoresPerNode; i++ {
ts.Ctx.Engines = append(ts.Ctx.Engines, engine.NewInMem(roachpb.Attributes{}, 100<<20, ts.Server.stopper))
}
if !ts.SkipBootstrap {
stopper := stop.NewStopper()
_, err := BootstrapCluster("cluster-1", ts.Ctx.Engines, stopper)
if err != nil {
return util.Errorf("could not bootstrap cluster: %s", err)
}
stopper.Stop()
}
if err := ts.Server.Start(true); err != nil {
return err
}
// If enabled, wait for initial splits to complete before returning control.
// If initial splits do not complete, the server is stopped before
// returning.
if config.TestingTableSplitsDisabled() {
return nil
}
if err := ts.WaitForInitialSplits(); err != nil {
ts.Stop()
return err
}
return nil
}
示例10: Generate
func Generate(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse {
// Begin by allocating a generator. The request and response structures are stored there
// so we can do error handling easily - the response structure contains the field to
// report failure.
g := generator.New()
g.Request = req
g.CommandLineParameters(g.Request.GetParameter())
// Create a wrapped version of the Descriptors and EnumDescriptors that
// point to the file that defines them.
g.WrapTypes()
g.SetPackageNames()
g.BuildTypeNameMap()
g.GenerateAllFiles()
if err := goformat(g.Response); err != nil {
g.Error(err)
}
testReq := proto.Clone(req).(*plugin.CodeGeneratorRequest)
testResp := GeneratePlugin(testReq, testgen.NewPlugin(), "pb_test.go")
for i := 0; i < len(testResp.File); i++ {
if strings.Contains(*testResp.File[i].Content, `//These tests are generated by github.com/gogo/protobuf/plugin/testgen`) {
g.Response.File = append(g.Response.File, testResp.File[i])
}
}
return g.Response
}
示例11: applyMutations
// Future home of the asynchronous schema changer that picks up
// queued schema changes and processes them.
//
// applyMutations applies the queued mutations for a table.
func (p *planner) applyMutations(tableDesc *TableDescriptor) error {
if len(tableDesc.Mutations) == 0 {
return nil
}
newTableDesc := proto.Clone(tableDesc).(*TableDescriptor)
p.applyUpVersion(newTableDesc)
// Make all mutations active.
for _, mutation := range newTableDesc.Mutations {
newTableDesc.makeMutationComplete(mutation)
}
newTableDesc.Mutations = nil
if err := newTableDesc.Validate(); err != nil {
return err
}
b := client.Batch{}
if err := p.backfillBatch(&b, tableDesc, newTableDesc); err != nil {
return err
}
b.Put(MakeDescMetadataKey(newTableDesc.GetID()), wrapDescriptor(newTableDesc))
if err := p.txn.Run(&b); err != nil {
return convertBatchError(newTableDesc, b, err)
}
p.notifyCompletedSchemaChange(newTableDesc.ID)
return nil
}
示例12: TestProto3SetDefaults
func TestProto3SetDefaults(t *testing.T) {
in := &pb.Message{
Terrain: map[string]*pb.Nested{
"meadow": new(pb.Nested),
},
Proto2Field: new(tpb.SubDefaults),
Proto2Value: map[string]*tpb.SubDefaults{
"badlands": new(tpb.SubDefaults),
},
}
got := proto.Clone(in).(*pb.Message)
proto.SetDefaults(got)
// There are no defaults in proto3. Everything should be the zero value, but
// we need to remember to set defaults for nested proto2 messages.
want := &pb.Message{
Terrain: map[string]*pb.Nested{
"meadow": new(pb.Nested),
},
Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
Proto2Value: map[string]*tpb.SubDefaults{
"badlands": {N: proto.Int64(7)},
},
}
if !proto.Equal(got, want) {
t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
}
}
示例13: 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)
}
}
}
示例14: Clone
// Clone uses proto.Clone to return a deep copy of pb. It panics if pb
// recursively contains any instances of types which are known to be
// unsupported by proto.Clone.
//
// This function and its associated lint (see `make check`) exist to ensure we
// do not attempt to proto.Clone types which are not supported by proto.Clone.
// This hackery is necessary because proto.Clone gives no direct indication
// that it has incompletely cloned a type; it merely logs to standard output
// (see https://github.com/golang/protobuf/blob/89238a3/proto/clone.go#L204).
//
// The concrete case against which this is currently guarding may be resolved
// upstream, see https://github.com/gogo/protobuf/issues/147.
func Clone(pb proto.Message) proto.Message {
for _, verbotenKind := range verbotenKinds {
if v := findVerboten(reflect.ValueOf(pb), verbotenKind); v != nil {
panic(fmt.Sprintf("attempt to clone %+v, which contains %+v", pb, v))
}
}
return proto.Clone(pb)
}
示例15: sendRPC
func sendRPC(opts Options, addrs []net.Addr, rpcContext *Context, name string,
args, reply gogoproto.Message) ([]gogoproto.Message, error) {
getArgs := func(addr net.Addr) gogoproto.Message {
return args
}
getReply := func() gogoproto.Message {
return gogoproto.Clone(reply)
}
return Send(opts, name, addrs, getArgs, getReply, rpcContext)
}