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


Golang proto.Unmarshal函数代码示例

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


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

示例1: 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

示例2: TestFloatingPointProto

func TestFloatingPointProto(t *testing.T) {
	seed := time.Now().UnixNano()
	popr := math_rand.New(math_rand.NewSource(seed))
	p := NewPopulatedFloatingPoint(popr, false)
	dAtA, err := github_com_gogo_protobuf_proto.Marshal(p)
	if err != nil {
		t.Fatalf("seed = %d, err = %v", seed, err)
	}
	msg := &FloatingPoint{}
	if err := github_com_gogo_protobuf_proto.Unmarshal(dAtA, msg); err != nil {
		t.Fatalf("seed = %d, err = %v", seed, err)
	}
	littlefuzz := make([]byte, len(dAtA))
	copy(littlefuzz, dAtA)
	for i := range dAtA {
		dAtA[i] = byte(popr.Intn(256))
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
	}
	if !p.Equal(msg) {
		t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
	}
	if len(littlefuzz) > 0 {
		fuzzamount := 100
		for i := 0; i < fuzzamount; i++ {
			littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
			littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
		}
		// shouldn't panic
		_ = github_com_gogo_protobuf_proto.Unmarshal(littlefuzz, msg)
	}
}
开发者ID:pascaldekloe,项目名称:colfer,代码行数:33,代码来源:mapsproto2pb_test.go

示例3: Decode

func (c codec) Decode(data []byte) (runtime.Object, error) {
	unknown := &runtime.Unknown{}
	if err := proto.Unmarshal(data, unknown); err != nil {
		return nil, err
	}
	obj, err := c.creater.New(unknown.APIVersion, unknown.Kind)
	if err != nil {
		return nil, err
	}
	pobj, ok := obj.(proto.Message)
	if !ok {
		return nil, fmt.Errorf("runtime object is not a proto.Message: %v", reflect.TypeOf(obj))
	}
	if err := proto.Unmarshal(unknown.RawJSON, pobj); err != nil {
		return nil, err
	}
	if unknown.APIVersion != c.outputVersion {
		out, err := c.convertor.ConvertToVersion(obj, c.outputVersion)
		if err != nil {
			return nil, err
		}
		obj = out
	}
	return obj, nil
}
开发者ID:johndmulhausen,项目名称:kubernetes,代码行数:25,代码来源:protobuf.go

示例4: Lookup

func (o *OortFS) Lookup(ctx context.Context, parent []byte, name string) (string, *pb.Attr, error) {
	// Get the id
	b, err := o.comms.ReadGroupItem(ctx, parent, []byte(name))
	if store.IsNotFound(err) {
		return "", &pb.Attr{}, nil
	} else if err != nil {
		return "", &pb.Attr{}, err
	}
	d := &pb.DirEntry{}
	err = proto.Unmarshal(b, d)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	if d.Tombstone != nil {
		return "", &pb.Attr{}, nil
	}
	// Get the Inode entry
	b, err = o.GetChunk(ctx, d.Id)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	n := &pb.InodeEntry{}
	err = proto.Unmarshal(b, n)
	if err != nil {
		return "", &pb.Attr{}, err
	}
	return d.Name, n.Attr, nil
}
开发者ID:pandemicsyn,项目名称:cfs-binary-release,代码行数:28,代码来源:file.go

示例5: TestMessageProto

func TestMessageProto(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	p := NewPopulatedMessage(popr, false)
	data, err := github_com_gogo_protobuf_proto.Marshal(p)
	if err != nil {
		panic(err)
	}
	msg := &Message{}
	if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil {
		panic(err)
	}
	littlefuzz := make([]byte, len(data))
	copy(littlefuzz, data)
	for i := range data {
		data[i] = byte(popr.Intn(256))
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
	}
	if !p.Equal(msg) {
		t.Fatalf("%#v !Proto %#v", msg, p)
	}
	if len(littlefuzz) > 0 {
		fuzzamount := 100
		for i := 0; i < fuzzamount; i++ {
			littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
			littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
		}
		// shouldn't panic
		_ = github_com_gogo_protobuf_proto.Unmarshal(littlefuzz, msg)
	}
}
开发者ID:velocity-toolkit,项目名称:protobuf,代码行数:32,代码来源:theproto3pb_test.go

示例6: TestIndexQueryProto

func TestIndexQueryProto(t *testing.T) {
	seed := time.Now().UnixNano()
	popr := math_rand.New(math_rand.NewSource(seed))
	p := NewPopulatedIndexQuery(popr, false)
	data, err := github_com_gogo_protobuf_proto.Marshal(p)
	if err != nil {
		t.Fatalf("seed = %d, err = %v", seed, err)
	}
	msg := &IndexQuery{}
	if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil {
		t.Fatalf("seed = %d, err = %v", seed, err)
	}
	littlefuzz := make([]byte, len(data))
	copy(littlefuzz, data)
	for i := range data {
		data[i] = byte(popr.Intn(256))
	}
	if !p.Equal(msg) {
		t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
	}
	if len(littlefuzz) > 0 {
		fuzzamount := 100
		for i := 0; i < fuzzamount; i++ {
			littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
			littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
		}
		// shouldn't panic
		_ = github_com_gogo_protobuf_proto.Unmarshal(littlefuzz, msg)
	}
}
开发者ID:dropbox,项目名称:gogoprotobuf,代码行数:30,代码来源:indexpb_test.go

示例7: executeMessage

func (r *rpc) executeMessage(conn net.Conn, rpcType internal.RPCType, buf []byte) (internal.RPCType, proto.Message, error) {
	switch rpcType {
	case internal.RPCType_FetchData:
		var req internal.FetchDataRequest
		if err := proto.Unmarshal(buf, &req); err != nil {
			return internal.RPCType_Error, nil, fmt.Errorf("fetch request unmarshal: %v", err)
		}
		resp, err := r.handleFetchData(&req)
		return rpcType, resp, err
	case internal.RPCType_Join:
		var req internal.JoinRequest
		if err := proto.Unmarshal(buf, &req); err != nil {
			return internal.RPCType_Error, nil, fmt.Errorf("join request unmarshal: %v", err)
		}
		resp, err := r.handleJoinRequest(&req)
		return rpcType, resp, err
	case internal.RPCType_PromoteRaft:
		var req internal.PromoteRaftRequest
		if err := proto.Unmarshal(buf, &req); err != nil {
			return internal.RPCType_Error, nil, fmt.Errorf("promote to raft request unmarshal: %v", err)
		}
		resp, err := r.handlePromoteRaftRequest(&req)
		return rpcType, resp, err
	default:
		return internal.RPCType_Error, nil, fmt.Errorf("unknown rpc type:%v", rpcType)
	}
}
开发者ID:rhyolight,项目名称:influxdb,代码行数:27,代码来源:rpc.go

示例8: DecodeInto

func (c codec) DecodeInto(data []byte, obj runtime.Object) error {
	version, kind, err := c.typer.ObjectVersionAndKind(obj)
	if err != nil {
		return err
	}
	unknown := &runtime.Unknown{}
	if err := proto.Unmarshal(data, unknown); err != nil {
		return err
	}
	if unknown.APIVersion == version && unknown.Kind == kind {
		pobj, ok := obj.(proto.Message)
		if !ok {
			return fmt.Errorf("runtime object is not a proto.Message: %v", reflect.TypeOf(obj))
		}

		return proto.Unmarshal(unknown.RawJSON, pobj)
	}

	versioned, err := c.creater.New(unknown.APIVersion, unknown.Kind)
	if err != nil {
		return err
	}

	pobj, ok := versioned.(proto.Message)
	if !ok {
		return fmt.Errorf("runtime object is not a proto.Message: %v", reflect.TypeOf(obj))
	}

	if err := proto.Unmarshal(unknown.RawJSON, pobj); err != nil {
		return err
	}
	return c.convertor.Convert(versioned, obj)
}
开发者ID:johndmulhausen,项目名称:kubernetes,代码行数:33,代码来源:protobuf.go

示例9: Match

func (e *envelopeReceiver) Match(actual interface{}) (success bool, err error) {
	envelope := &events.Envelope{}
	input, ok := actual.(chan []byte)
	if !ok {
		return false, errors.New("Envelope receiver: expected a channel of byte slices")
	}
	var msgBytes []byte
	select {
	case msgBytes = <-input:
	default:
		return false, nil
	}

	if err := proto.Unmarshal(msgBytes, envelope); err != nil {
		// Try again, stripping out the length prefix
		if err := proto.Unmarshal(msgBytes[4:], envelope); err != nil {
			return false, nil
		}
		e.hadPrefix = true
	}
	if e.mustHavePrefix && !e.hadPrefix {
		return false, nil
	}
	e.foundEnvelope = envelope
	if e.matcher != nil {
		return e.matcher.Match(envelope)
	}
	return true, nil
}
开发者ID:yingkitw,项目名称:loggregator,代码行数:29,代码来源:envelope_receiver.go

示例10: TestEngineMerge

// TestEngineMerge tests that the passing through of engine merge operations
// to the goMerge function works as expected. The semantics are tested more
// exhaustively in the merge tests themselves.
func TestEngineMerge(t *testing.T) {
	runWithAllEngines(func(engine Engine, t *testing.T) {
		testcases := []struct {
			testKey  proto.EncodedKey
			merges   [][]byte
			expected []byte
		}{
			{
				proto.EncodedKey("haste not in life"),
				[][]byte{
					appender("x"),
					appender("y"),
					appender("z"),
				},
				appender("xyz"),
			},
			{
				proto.EncodedKey("timeseriesmerged"),
				[][]byte{
					timeSeriesInt(testtime, 1000, []tsIntSample{
						{1, 1, 5, 5, 5},
					}...),
					timeSeriesInt(testtime, 1000, []tsIntSample{
						{2, 1, 5, 5, 5},
						{1, 2, 10, 7, 3},
					}...),
					timeSeriesInt(testtime, 1000, []tsIntSample{
						{10, 1, 5, 5, 5},
					}...),
					timeSeriesInt(testtime, 1000, []tsIntSample{
						{5, 1, 5, 5, 5},
						{3, 1, 5, 5, 5},
					}...),
				},
				timeSeriesInt(testtime, 1000, []tsIntSample{
					{1, 3, 15, 7, 3},
					{2, 1, 5, 5, 5},
					{3, 1, 5, 5, 5},
					{5, 1, 5, 5, 5},
					{10, 1, 5, 5, 5},
				}...),
			},
		}
		for _, tc := range testcases {
			for i, update := range tc.merges {
				if err := engine.Merge(tc.testKey, update); err != nil {
					t.Fatalf("%d: %v", i, err)
				}
			}
			result, _ := engine.Get(tc.testKey)
			var resultV, expectedV proto.MVCCMetadata
			gogoproto.Unmarshal(result, &resultV)
			gogoproto.Unmarshal(tc.expected, &expectedV)
			if !reflect.DeepEqual(resultV, expectedV) {
				t.Errorf("unexpected append-merge result: %v != %v", resultV, expectedV)
			}
		}
	}, t)
}
开发者ID:josephwinston,项目名称:cockroach,代码行数:62,代码来源:engine_test.go

示例11: compareMergedValues

func compareMergedValues(t *testing.T, result, expected []byte) bool {
	var resultV, expectedV MVCCMetadata
	if err := proto.Unmarshal(result, &resultV); err != nil {
		t.Fatal(err)
	}
	if err := proto.Unmarshal(expected, &expectedV); err != nil {
		t.Fatal(err)
	}
	return reflect.DeepEqual(resultV, expectedV)
}
开发者ID:mbertschler,项目名称:cockroach,代码行数:10,代码来源:batch_test.go

示例12: ReadDirAll

func (o *OortFS) ReadDirAll(ctx context.Context, id []byte) (*pb.ReadDirAllResponse, error) {
	v, err := o.validateIP(ctx)
	if err != nil {
		return nil, err
	}
	if !v {
		return nil, errors.New("Unknown or unauthorized FS use")
	}
	// Get the keys from the group
	items, err := o.comms.LookupGroup(ctx, id)
	log.Println("ITEMS: ", items)
	if err != nil {
		// TODO: Needs beter error handling
		log.Println("Error looking up group: ", err)
		return &pb.ReadDirAllResponse{}, err
	}
	// Iterate over each item, getting the ID then the Inode Entry
	e := &pb.ReadDirAllResponse{}
	dirent := &pb.DirEntry{}
	for _, item := range items {
		// lookup the item in the group to get the id
		b, err := o.comms.ReadGroupItemByKey(ctx, id, item.ChildKeyA, item.ChildKeyB)
		if err != nil {
			// TODO: Needs beter error handling
			log.Println("Error with lookup: ", err)
			continue
		}
		err = proto.Unmarshal(b, dirent)
		if err != nil {
			return &pb.ReadDirAllResponse{}, err
		}
		if dirent.Tombstone != nil {
			// Skip deleted entries
			continue
		}
		// get the inode entry
		b, err = o.GetChunk(ctx, dirent.Id)
		if err != nil {
			continue
		}
		n := &pb.InodeEntry{}
		err = proto.Unmarshal(b, n)
		if err != nil {
			continue
		}
		if n.IsDir {
			e.DirEntries = append(e.DirEntries, &pb.DirEnt{Name: dirent.Name, Attr: n.Attr})
		} else {
			e.FileEntries = append(e.FileEntries, &pb.DirEnt{Name: dirent.Name, Attr: n.Attr})
		}
	}
	sort.Sort(ByDirent(e.DirEntries))
	sort.Sort(ByDirent(e.FileEntries))
	return e, nil
}
开发者ID:wreese,项目名称:cfs-binary-release,代码行数:55,代码来源:file.go

示例13: FromProtobufferMessage

func FromProtobufferMessage(data []byte) (appId string, err error) {
	receivedMessage := &logmessage.LogMessage{}
	err = proto.Unmarshal(data, receivedMessage)
	if err == nil {
		return *receivedMessage.AppId, nil
	}

	receivedEnvelope := &logmessage.LogEnvelope{}
	err = proto.Unmarshal(data, receivedEnvelope)
	if err != nil {
		err = errors.New(fmt.Sprintf("Log data could not be unmarshaled to message or envelope. Dropping it... Error: %v. Data: %v", err, data))
		return "", err
	}
	return *receivedEnvelope.RoutingKey, nil
}
开发者ID:pkdevbox,项目名称:loggregatorlib,代码行数:15,代码来源:appid.go

示例14: CopyFrom

// CopyFrom copies all the cached results from the originRangeID
// response cache into this one. Note that the cache will not be
// locked while copying is in progress. Failures decoding individual
// cache entries return an error. The copy is done directly using the
// engine instead of interpreting values through MVCC for efficiency.
func (rc *ResponseCache) CopyFrom(e engine.Engine, originRangeID proto.RangeID) error {
	prefix := keys.ResponseCacheKey(originRangeID, nil) // response cache prefix
	start := engine.MVCCEncodeKey(prefix)
	end := engine.MVCCEncodeKey(prefix.PrefixEnd())

	return e.Iterate(start, end, func(kv proto.RawKeyValue) (bool, error) {
		// Decode the key into a cmd, skipping on error. Otherwise,
		// write it to the corresponding key in the new cache.
		cmdID, err := rc.decodeResponseCacheKey(kv.Key)
		if err != nil {
			return false, util.Errorf("could not decode a response cache key %s: %s",
				proto.Key(kv.Key), err)
		}
		key := keys.ResponseCacheKey(rc.rangeID, &cmdID)
		encKey := engine.MVCCEncodeKey(key)
		// Decode the value, update the checksum and re-encode.
		meta := &engine.MVCCMetadata{}
		if err := gogoproto.Unmarshal(kv.Value, meta); err != nil {
			return false, util.Errorf("could not decode response cache value %s [% x]: %s",
				proto.Key(kv.Key), kv.Value, err)
		}
		meta.Value.Checksum = nil
		meta.Value.InitChecksum(key)
		_, _, err = engine.PutProto(e, encKey, meta)
		return false, err
	})
}
开发者ID:mberhault,项目名称:cockroach,代码行数:32,代码来源:response_cache.go

示例15: Entries

// Entries implements the raft.Storage interface. Note that maxBytes is advisory
// and this method will always return at least one entry even if it exceeds
// maxBytes. Passing maxBytes equal to zero disables size checking.
// TODO(bdarnell): consider caching for recent entries, if rocksdb's builtin caching
// is insufficient.
func (r *Range) Entries(lo, hi, maxBytes uint64) ([]raftpb.Entry, error) {
	// Scan over the log to find the requested entries in the range [lo, hi),
	// stopping once we have enough.
	var ents []raftpb.Entry
	size := uint64(0)
	var ent raftpb.Entry
	scanFunc := func(kv proto.KeyValue) (bool, error) {
		err := gogoproto.Unmarshal(kv.Value.GetBytes(), &ent)
		if err != nil {
			return false, err
		}
		size += uint64(ent.Size())
		ents = append(ents, ent)
		return maxBytes > 0 && size > maxBytes, nil
	}

	_, err := engine.MVCCIterate(r.rm.Engine(),
		keys.RaftLogKey(r.Desc().RaftID, lo),
		keys.RaftLogKey(r.Desc().RaftID, hi),
		proto.ZeroTimestamp, true /* consistent */, nil /* txn */, scanFunc)

	if err != nil {
		return nil, err
	}

	// If neither the number of entries nor the size limitations had an
	// effect, we weren't able to supply everything the client wanted.
	if len(ents) != int(hi-lo) && (maxBytes == 0 || size < maxBytes) {
		return nil, raft.ErrUnavailable
	}

	return ents, nil
}
开发者ID:Hellblazer,项目名称:cockroach,代码行数:38,代码来源:range_raftstorage.go


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