/
jsonpb.go
60 lines (52 loc) · 1.42 KB
/
jsonpb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package binding
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/golang/protobuf/jsonpb"
protov1 "github.com/golang/protobuf/proto"
"google.golang.org/protobuf/encoding/protojson"
protov2 "google.golang.org/protobuf/proto"
)
// JSONPB encode json to proto.Message
var JSONPB = jsonpbBinding{}
type jsonpbBinding struct{}
func (jsonpbBinding) Name() string {
return "jsonpb"
}
func (b jsonpbBinding) Bind(req *http.Request, obj interface{}) error {
buf, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
return b.BindBody(buf, obj)
}
func (jsonpbBinding) BindBody(body []byte, obj interface{}) error {
switch msg := obj.(type) {
case protov1.Message:
mm := jsonpb.Unmarshaler{AllowUnknownFields: true}
if err := mm.Unmarshal(bytes.NewBuffer(body), msg); err != nil {
return err
}
case protov2.Message:
mm := protojson.UnmarshalOptions{
AllowPartial: true,
DiscardUnknown: true,
}
if err := mm.Unmarshal(body, msg); err != nil {
return err
}
default:
if err := json.Unmarshal(body, obj); err != nil {
return err
}
}
// Here it's same to return validate(obj), but util now we can't add
// `binding:""` to the struct which automatically generate by gen-proto
return nil
// return validate(obj)
}