You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
809 B
Go

package grpcall
import (
"encoding/json"
"fmt"
"io"
"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 {
if err == io.EOF {
return err
}
return fmt.Errorf("unmarshal request json data error, %s", err)
}
return nil
}
func NewJsonRequestParser(resolver jsonpb.AnyResolver, in io.Reader) RequestParser {
return &JsonRequestParser{
dec: json.NewDecoder(in),
unmarshaler: jsonpb.Unmarshaler{AnyResolver: resolver},
}
}