本文整理匯總了Golang中github.com/gogo/protobuf/jsonpb.Unmarshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Unmarshal函數的具體用法?Golang Unmarshal怎麽用?Golang Unmarshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Unmarshal函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestPlainHTTPServer
// TestPlainHTTPServer verifies that we can serve plain http and talk to it.
// This is controlled by -cert=""
func TestPlainHTTPServer(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{
// The default context uses embedded certs.
Insecure: true,
})
defer s.Stopper().Stop()
ts := s.(*TestServer)
httpClient, err := s.GetHTTPClient()
if err != nil {
t.Fatal(err)
}
httpURL := "http://" + ts.Ctx.HTTPAddr + healthPath
if resp, err := httpClient.Get(httpURL); err != nil {
t.Fatalf("error requesting health at %s: %s", httpURL, err)
} else {
defer resp.Body.Close()
var data serverpb.HealthResponse
if err := jsonpb.Unmarshal(resp.Body, &data); err != nil {
t.Error(err)
}
}
httpsURL := "https://" + ts.Ctx.HTTPAddr + healthPath
if _, err := httpClient.Get(httpsURL); err == nil {
t.Fatalf("unexpected success fetching %s", httpsURL)
}
}
示例2: TestPlainHTTPServer
// TestPlainHTTPServer verifies that we can serve plain http and talk to it.
// This is controlled by -cert=""
func TestPlainHTTPServer(t *testing.T) {
defer leaktest.AfterTest(t)()
// Create a custom context. The default one uses embedded certs.
ctx := MakeContext()
ctx.Addr = "127.0.0.1:0"
ctx.HTTPAddr = "127.0.0.1:0"
ctx.Insecure = true
s := TestServer{Ctx: &ctx}
if err := s.Start(); err != nil {
t.Fatalf("could not start plain http server: %v", err)
}
defer s.Stop()
httpClient, err := ctx.GetHTTPClient()
if err != nil {
t.Fatal(err)
}
httpURL := "http://" + s.Ctx.HTTPAddr + healthPath
if resp, err := httpClient.Get(httpURL); err != nil {
t.Fatalf("error requesting health at %s: %s", httpURL, err)
} else {
defer resp.Body.Close()
var data serverpb.HealthResponse
if err := jsonpb.Unmarshal(resp.Body, &data); err != nil {
t.Error(err)
}
}
httpsURL := "https://" + s.Ctx.HTTPAddr + healthPath
if _, err := httpClient.Get(httpsURL); err == nil {
t.Fatalf("unexpected success fetching %s", httpsURL)
}
}
示例3: NewDecoder
// NewDecoder implements gwruntime.Marshaler.
func (j *JSONPb) NewDecoder(r io.Reader) gwruntime.Decoder {
return gwruntime.DecoderFunc(func(v interface{}) error {
if pb, ok := v.(proto.Message); ok {
return jsonpb.Unmarshal(r, pb)
}
return errors.Errorf("unexpected type %T does not implement %s", v, typeProtoMessage)
})
}
示例4: GetJSON
// GetJSON uses the supplied client to GET the URL specified by the parameters
// and unmarshals the result into response.
func GetJSON(httpClient http.Client, path string, response proto.Message) error {
resp, err := httpClient.Get(path)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body)
return errors.Errorf("status: %s, body: %s", resp.Status, b)
}
return jsonpb.Unmarshal(resp.Body, response)
}
示例5: TestAcceptEncoding
// TestAcceptEncoding hits the server while explicitly disabling
// decompression on a custom client's Transport and setting it
// conditionally via the request's Accept-Encoding headers.
func TestAcceptEncoding(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
client, err := s.GetHTTPClient()
if err != nil {
t.Fatal(err)
}
testData := []struct {
acceptEncoding string
newReader func(io.Reader) io.Reader
}{
{"",
func(b io.Reader) io.Reader {
return b
},
},
{httputil.GzipEncoding,
func(b io.Reader) io.Reader {
r, err := gzip.NewReader(b)
if err != nil {
t.Fatalf("could not create new gzip reader: %s", err)
}
return r
},
},
}
for _, d := range testData {
req, err := http.NewRequest("GET", s.AdminURL()+statusPrefix+"metrics/local", nil)
if err != nil {
t.Fatalf("could not create request: %s", err)
}
if d.acceptEncoding != "" {
req.Header.Set(httputil.AcceptEncodingHeader, d.acceptEncoding)
}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("could not make request to %s: %s", req.URL, err)
}
defer resp.Body.Close()
if ce := resp.Header.Get(httputil.ContentEncodingHeader); ce != d.acceptEncoding {
t.Fatalf("unexpected content encoding: '%s' != '%s'", ce, d.acceptEncoding)
}
r := d.newReader(resp.Body)
var data serverpb.JSONResponse
if err := jsonpb.Unmarshal(r, &data); err != nil {
t.Error(err)
}
}
}
示例6: PostJSON
// PostJSON uses the supplied client to POST request to the URL specified by
// the parameters and unmarshals the result into response .
func PostJSON(httpClient http.Client, path string, request, response proto.Message) error {
str, err := (&jsonpb.Marshaler{}).MarshalToString(request)
if err != nil {
return err
}
resp, err := httpClient.Post(path, JSONContentType, strings.NewReader(str))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(resp.Body)
return errors.Errorf("status: %s, body: %s, error: %s", resp.Status, b, err)
}
return jsonpb.Unmarshal(resp.Body, response)
}
示例7: doJSONRequest
func doJSONRequest(httpClient http.Client, req *http.Request, response proto.Message) error {
if timeout := httpClient.Timeout; timeout > 0 {
req.Header.Set("Grpc-Timeout", strconv.FormatInt(timeout.Nanoseconds(), 10)+"n")
}
req.Header.Set(AcceptHeader, JSONContentType)
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if contentType := resp.Header.Get(ContentTypeHeader); !(resp.StatusCode == http.StatusOK && contentType == JSONContentType) {
b, err := ioutil.ReadAll(resp.Body)
return errors.Errorf("status: %s, content-type: %s, body: %s, error: %v", resp.Status, contentType, b, err)
}
return jsonpb.Unmarshal(resp.Body, response)
}
示例8: TestHealth
// TestHealth verifies that health endpoint returns an empty JSON response.
func TestHealth(t *testing.T) {
defer leaktest.AfterTest(t)()
s := StartTestServer(t)
defer s.Stop()
u := s.Ctx.AdminURL() + healthPath
httpClient, err := s.Ctx.GetHTTPClient()
if err != nil {
t.Fatal(err)
}
resp, err := httpClient.Get(u)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
var data serverpb.HealthResponse
if err := jsonpb.Unmarshal(resp.Body, &data); err != nil {
t.Error(err)
}
}
示例9: Unmarshal
// Unmarshal implements gwruntime.Marshaler.
func (j *JSONPb) Unmarshal(data []byte, v interface{}) error {
if pb, ok := v.(proto.Message); ok {
return jsonpb.Unmarshal(bytes.NewReader(data), pb)
}
return errors.Errorf("unexpected type %T does not implement %s", v, typeProtoMessage)
}
示例10: getRequestProto
// getRequestProto unmarshals the result of a get request to the test server
// with the given path.
func getRequestProto(t *testing.T, ts serverutils.TestServerInterface, path string, v proto.Message) error {
respBody := getRequestReader(t, ts, path)
defer respBody.Close()
return jsonpb.Unmarshal(respBody, v)
}