36 lines
794 B
Go
36 lines
794 B
Go
package grpcall
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
"github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
type RequestData func(msg proto.Message) error
|
|
|
|
type RequestParser interface {
|
|
Next(msg proto.Message) error
|
|
}
|
|
|
|
type JsonRequestParser struct {
|
|
dec *json.Decoder
|
|
unmarshaler jsonpb.Unmarshaler
|
|
}
|
|
|
|
func (this JsonRequestParser) Next(msg proto.Message) error {
|
|
if err := this.unmarshaler.UnmarshalNext(this.dec, msg); err != nil {
|
|
return fmt.Errorf("unmarshal request json data error, %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewJsonRequestParser(resolver jsonpb.AnyResolver, data string) RequestParser {
|
|
return &JsonRequestParser{
|
|
dec: json.NewDecoder(strings.NewReader(data)),
|
|
unmarshaler: jsonpb.Unmarshaler{AnyResolver: resolver},
|
|
}
|
|
}
|