本文整理汇总了Golang中github.com/maditya/protobuf/jsonpb.UnmarshalString函数的典型用法代码示例。如果您正苦于以下问题:Golang UnmarshalString函数的具体用法?Golang UnmarshalString怎么用?Golang UnmarshalString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了UnmarshalString函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestJsonFullCircleProtoToStd
func TestJsonFullCircleProtoToStd(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
protoMsg := NewPopulatedProtoTypes(popr, true)
j := &jsonpb.Marshaler{}
protoData, err := j.MarshalToString(protoMsg)
if err != nil {
t.Fatal(err)
}
stdMsg := &StdTypes{}
if err2 := jsonpb.UnmarshalString(protoData, stdMsg); err2 != nil {
t.Fatal(err)
}
stdData, err := j.MarshalToString(stdMsg)
if err != nil {
t.Fatal(err)
}
protoMsgOut := &ProtoTypes{}
if err3 := jsonpb.UnmarshalString(stdData, protoMsgOut); err3 != nil {
t.Fatal(err)
}
if !protoMsg.Equal(protoMsgOut) {
t.Fatalf("want %#v got %#v", protoMsg, protoMsgOut)
}
}
开发者ID:maditya,项目名称:protobuf,代码行数:25,代码来源:types_test.go
示例2: TestMoreDefaultsBJSON
func TestMoreDefaultsBJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedMoreDefaultsB(popr, true)
marshaler := github_com_maditya_protobuf_jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &MoreDefaultsB{}
err = github_com_maditya_protobuf_jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
开发者ID:maditya,项目名称:protobuf,代码行数:18,代码来源:mdpb_test.go
示例3: TestEncodedSignedEntryUpdateJSON
func TestEncodedSignedEntryUpdateJSON(t *testing.T) {
popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedEncodedSignedEntryUpdate(popr, true)
marshaler := github_com_gogo_protobuf_jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatal(err)
}
msg := &EncodedSignedEntryUpdate{}
err = github_com_gogo_protobuf_jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatal(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("%#v !Json Equal %#v", msg, p)
}
}
开发者ID:yahoo,项目名称:coname,代码行数:20,代码来源:SignedEntryUpdatepr_test.go
示例4: TestWilsonJSON
func TestWilsonJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedWilson(popr, true)
marshaler := github_com_maditya_protobuf_jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &Wilson{}
err = github_com_maditya_protobuf_jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
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 !Json Equal %#v", seed, msg, p)
}
}
开发者ID:maditya,项目名称:protobuf,代码行数:21,代码来源:castvaluepb_test.go
示例5: handle
func handle(req *pb.ConformanceRequest) *pb.ConformanceResponse {
var err error
var msg pb.TestAllTypes
switch p := req.Payload.(type) {
case *pb.ConformanceRequest_ProtobufPayload:
err = proto.Unmarshal(p.ProtobufPayload, &msg)
case *pb.ConformanceRequest_JsonPayload:
err = jsonpb.UnmarshalString(p.JsonPayload, &msg)
if err != nil && err.Error() == "unmarshaling Any not supported yet" {
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_Skipped{
Skipped: err.Error(),
},
}
}
default:
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_RuntimeError{
RuntimeError: "unknown request payload type",
},
}
}
if err != nil {
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_ParseError{
ParseError: err.Error(),
},
}
}
switch req.RequestedOutputFormat {
case pb.WireFormat_PROTOBUF:
p, err := proto.Marshal(&msg)
if err != nil {
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_SerializeError{
SerializeError: err.Error(),
},
}
}
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_ProtobufPayload{
ProtobufPayload: p,
},
}
case pb.WireFormat_JSON:
p, err := jsonMarshaler.MarshalToString(&msg)
if err != nil {
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_SerializeError{
SerializeError: err.Error(),
},
}
}
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_JsonPayload{
JsonPayload: p,
},
}
default:
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_RuntimeError{
RuntimeError: "unknown output format",
},
}
}
}
开发者ID:maditya,项目名称:protobuf,代码行数:66,代码来源:conformance.go
示例6: TestKeyserverHTTPFrontLookup
func TestKeyserverHTTPFrontLookup(t *testing.T) {
kss, caPool, clks, verifiers, ck, clientConfig, teardown := setupRealm(t, 3, 3)
ks := kss[0]
defer teardown()
stop := stoppableSyncedClocks(clks)
defer close(stop)
waitForFirstEpoch(kss[0], clientConfig.Realms[0].VerificationPolicy.GetQuorum())
clientTLS, err := clientConfig.Realms[0].ClientTLS.Config(ck)
if err != nil {
t.Fatal(err)
}
pgpKeyRef := []byte("this-is-alices-pgp-key")
_, _, _, profile := doRegister(t, ks, clientConfig, clientTLS, caPool, clks[0].Now(), alice, 0, proto.Profile{
Nonce: []byte("definitely used only once"),
Keys: map[string][]byte{"pgp": pgpKeyRef},
})
url := "https://" + ks.httpFrontListen.Addr().String() + "/lookup"
lr := &proto.LookupRequest{UserId: alice,
QuorumRequirement: clientConfig.Realms[0].VerificationPolicy.GetQuorum()}
var b bytes.Buffer
mr := &jsonpb.Marshaler{OrigName: true}
err = mr.Marshal(&b, lr)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", url, &b)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
c := &http.Client{Transport: tr}
resp, err := c.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, e := ioutil.ReadAll(resp.Body)
if resp.Status != "200 OK" {
t.Fatalf("%s (%s)", body, e)
}
proof := &proto.LookupProof{}
err = jsonpb.UnmarshalString(string(body), proof)
if err != nil {
t.Fatal(err)
}
if got, want := proof.Profile.Encoding, profile.Encoding; !bytes.Equal(got, want) {
t.Errorf("profile didn't roundtrip: %x != %x", got, want)
}
if got, want := len(proof.Ratifications), majority(len(kss))+len(verifiers); got < want {
t.Errorf("expected at least %d sehs, got %d", got, want)
}
_, err = coname.VerifyLookup(clientConfig, alice, proof, clks[0].Now())
if err != nil {
t.Fatal(err)
}
// To verify json response preserves the original field names
// as specified in https://github.com/yahoo/coname/blob/master/proto/client.proto#L63-L89
type lp struct {
Entry string `json:"entry"`
Index string `json:"index"`
IndexProof string `json:"index_proof"`
Profile string `json:"profile"`
UserId string `json:"user_id"`
}
l := &lp{}
err = json.Unmarshal(body, &l)
if err != nil {
t.Fatal(err)
}
if l.Entry == "" {
t.Errorf("entry not found in the response")
}
if l.Index == "" {
t.Errorf("index not found in the response")
}
if l.IndexProof == "" {
t.Errorf("index_proof not found in the response")
}
if l.Profile == "" {
t.Errorf("profile not found in the response")
}
if l.UserId == "" {
t.Errorf("user_id not found in the response")
}
}
开发者ID:maditya,项目名称:coname,代码行数:92,代码来源:server_test.go
注:本文中的github.com/maditya/protobuf/jsonpb.UnmarshalString函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论