446 lines
12 KiB
Go
446 lines
12 KiB
Go
package runtime
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"reflect"
|
|
"strconv"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// JSONPb is a Marshaler which marshals/unmarshals into/from JSON
|
|
// with the "google.golang.org/protobuf/encoding/protojson" marshaler.
|
|
// It supports the full functionality of protobuf unlike JSONBuiltin.
|
|
//
|
|
// The NewDecoder method returns a DecoderWrapper, so the underlying
|
|
// *json.Decoder methods can be used.
|
|
type JSONPb struct {
|
|
protojson.MarshalOptions
|
|
protojson.UnmarshalOptions
|
|
}
|
|
|
|
// ContentType always returns "application/json".
|
|
func (*JSONPb) ContentType(_ interface{}) string {
|
|
return "application/json"
|
|
}
|
|
|
|
// Marshal marshals "v" into JSON.
|
|
func (j *JSONPb) Marshal(v interface{}) ([]byte, error) {
|
|
if p, ok := v.(proto.Message); ok {
|
|
// protojson already returns a freshly allocated slice that the caller owns, so
|
|
// routing it through a bytes.Buffer only added an allocation and a copy of the
|
|
// whole message per call. Every unary response goes through here.
|
|
//
|
|
// The error is returned with a nil slice rather than whatever protojson produced
|
|
// alongside it, matching what the bytes.Buffer path did: marshalTo returned before
|
|
// writing anything, so a failed Marshal never handed back partial output.
|
|
b, err := j.MarshalOptions.Marshal(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if err := j.marshalTo(&buf, v); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
func (j *JSONPb) marshalTo(w io.Writer, v interface{}) error {
|
|
p, ok := v.(proto.Message)
|
|
if !ok {
|
|
buf, err := j.marshalNonProtoField(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if j.Indent != "" {
|
|
b := &bytes.Buffer{}
|
|
if err := json.Indent(b, buf, "", j.Indent); err != nil {
|
|
return err
|
|
}
|
|
buf = b.Bytes()
|
|
}
|
|
_, err = w.Write(buf)
|
|
return err
|
|
}
|
|
|
|
b, err := j.MarshalOptions.Marshal(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = w.Write(b)
|
|
return err
|
|
}
|
|
|
|
var (
|
|
// protoMessageType is stored to prevent constant lookup of the same type at runtime.
|
|
protoMessageType = reflect.TypeFor[proto.Message]()
|
|
)
|
|
|
|
// marshalNonProto marshals a non-message field of a protobuf message.
|
|
// This function does not correctly marshal arbitrary data structures into JSON,
|
|
// it is only capable of marshaling non-message field values of protobuf,
|
|
// i.e. primitive types, enums; pointers to primitives or enums; maps from
|
|
// integer/string types to primitives/enums/pointers to messages.
|
|
func (j *JSONPb) marshalNonProtoField(v interface{}) ([]byte, error) {
|
|
if v == nil {
|
|
return []byte("null"), nil
|
|
}
|
|
rv := reflect.ValueOf(v)
|
|
for rv.Kind() == reflect.Ptr {
|
|
if rv.IsNil() {
|
|
return []byte("null"), nil
|
|
}
|
|
rv = rv.Elem()
|
|
}
|
|
|
|
if rv.Kind() == reflect.Slice {
|
|
if rv.IsNil() {
|
|
if j.EmitUnpopulated {
|
|
return []byte("[]"), nil
|
|
}
|
|
return []byte("null"), nil
|
|
}
|
|
|
|
if rv.Type().Elem().Implements(protoMessageType) {
|
|
var buf bytes.Buffer
|
|
if err := buf.WriteByte('['); err != nil {
|
|
return nil, err
|
|
}
|
|
for i := 0; i < rv.Len(); i++ {
|
|
if i != 0 {
|
|
if err := buf.WriteByte(','); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if err := j.marshalTo(&buf, rv.Index(i).Interface().(proto.Message)); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if err := buf.WriteByte(']'); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
if rv.Type().Elem().Implements(typeProtoEnum) {
|
|
var buf bytes.Buffer
|
|
if err := buf.WriteByte('['); err != nil {
|
|
return nil, err
|
|
}
|
|
for i := 0; i < rv.Len(); i++ {
|
|
if i != 0 {
|
|
if err := buf.WriteByte(','); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
var err error
|
|
if j.UseEnumNumbers {
|
|
_, err = buf.WriteString(strconv.FormatInt(rv.Index(i).Int(), 10))
|
|
} else {
|
|
_, err = buf.WriteString("\"" + rv.Index(i).Interface().(protoEnum).String() + "\"")
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if err := buf.WriteByte(']'); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|
|
}
|
|
|
|
if rv.Kind() == reflect.Map {
|
|
m := make(map[string]*json.RawMessage)
|
|
for _, k := range rv.MapKeys() {
|
|
buf, err := j.Marshal(rv.MapIndex(k).Interface())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
m[fmt.Sprintf("%v", k.Interface())] = (*json.RawMessage)(&buf)
|
|
}
|
|
return json.Marshal(m)
|
|
}
|
|
if enum, ok := rv.Interface().(protoEnum); ok && !j.UseEnumNumbers {
|
|
return json.Marshal(enum.String())
|
|
}
|
|
return json.Marshal(rv.Interface())
|
|
}
|
|
|
|
// Unmarshal unmarshals JSON "data" into "v"
|
|
func (j *JSONPb) Unmarshal(data []byte, v interface{}) error {
|
|
return unmarshalJSONPb(data, j.UnmarshalOptions, v)
|
|
}
|
|
|
|
// NewDecoder returns a Decoder which reads JSON stream from "r".
|
|
func (j *JSONPb) NewDecoder(r io.Reader) Decoder {
|
|
d := json.NewDecoder(r)
|
|
return DecoderWrapper{
|
|
Decoder: d,
|
|
UnmarshalOptions: j.UnmarshalOptions,
|
|
}
|
|
}
|
|
|
|
// DecoderWrapper is a wrapper around a *json.Decoder that adds
|
|
// support for protos to the Decode method.
|
|
type DecoderWrapper struct {
|
|
*json.Decoder
|
|
protojson.UnmarshalOptions
|
|
}
|
|
|
|
// Decode wraps the embedded decoder's Decode method to support
|
|
// protos using a jsonpb.Unmarshaler.
|
|
func (d DecoderWrapper) Decode(v interface{}) error {
|
|
return decodeJSONPb(d.Decoder, d.UnmarshalOptions, v)
|
|
}
|
|
|
|
// NewEncoder returns an Encoder which writes JSON stream into "w".
|
|
func (j *JSONPb) NewEncoder(w io.Writer) Encoder {
|
|
return EncoderFunc(func(v interface{}) error {
|
|
if err := j.marshalTo(w, v); err != nil {
|
|
return err
|
|
}
|
|
// mimic json.Encoder by adding a newline (makes output
|
|
// easier to read when it contains multiple encoded items)
|
|
_, err := w.Write(j.Delimiter())
|
|
return err
|
|
})
|
|
}
|
|
|
|
func unmarshalJSONPb(data []byte, unmarshaler protojson.UnmarshalOptions, v interface{}) error {
|
|
d := json.NewDecoder(bytes.NewReader(data))
|
|
return decodeJSONPb(d, unmarshaler, v)
|
|
}
|
|
|
|
func decodeJSONPb(d *json.Decoder, unmarshaler protojson.UnmarshalOptions, v interface{}) error {
|
|
p, ok := v.(proto.Message)
|
|
if !ok {
|
|
return decodeNonProtoField(d, unmarshaler, v)
|
|
}
|
|
|
|
// Decode into bytes for marshalling
|
|
var b json.RawMessage
|
|
if err := d.Decode(&b); err != nil {
|
|
return err
|
|
}
|
|
|
|
return unmarshaler.Unmarshal([]byte(b), p)
|
|
}
|
|
|
|
func decodeNonProtoField(d *json.Decoder, unmarshaler protojson.UnmarshalOptions, v interface{}) error {
|
|
rv := reflect.ValueOf(v)
|
|
if rv.Kind() != reflect.Ptr {
|
|
return fmt.Errorf("%T is not a pointer", v)
|
|
}
|
|
for rv.Kind() == reflect.Ptr {
|
|
if rv.IsNil() {
|
|
rv.Set(reflect.New(rv.Type().Elem()))
|
|
}
|
|
if rv.Type().ConvertibleTo(typeProtoMessage) {
|
|
// Decode into bytes for marshalling
|
|
var b json.RawMessage
|
|
if err := d.Decode(&b); err != nil {
|
|
return err
|
|
}
|
|
|
|
return unmarshaler.Unmarshal([]byte(b), rv.Interface().(proto.Message))
|
|
}
|
|
rv = rv.Elem()
|
|
}
|
|
if rv.Kind() == reflect.Map {
|
|
if rv.IsNil() {
|
|
rv.Set(reflect.MakeMap(rv.Type()))
|
|
}
|
|
conv, ok := convFromType[rv.Type().Key().Kind()]
|
|
if !ok {
|
|
return fmt.Errorf("unsupported type of map field key: %v", rv.Type().Key())
|
|
}
|
|
|
|
var raw json.RawMessage
|
|
if err := d.Decode(&raw); err != nil {
|
|
return err
|
|
}
|
|
m := make(map[string]*json.RawMessage)
|
|
if err := json.Unmarshal(raw, &m); err != nil {
|
|
return err
|
|
}
|
|
// encoding/json keeps the last value of a repeated key, while protojson
|
|
// rejects a duplicate map key, also when two spellings convert to the
|
|
// same key (e.g. "1" and "01"). Reject both here too.
|
|
if k, ok := duplicateObjectKey(raw); ok {
|
|
return fmt.Errorf("duplicate map key %q", k)
|
|
}
|
|
seen := make(map[any]struct{}, len(m))
|
|
for k, v := range m {
|
|
result := conv.Call([]reflect.Value{reflect.ValueOf(k)})
|
|
if err := result[1].Interface(); err != nil {
|
|
return err.(error)
|
|
}
|
|
bk := result[0]
|
|
if _, ok := seen[bk.Interface()]; ok {
|
|
return fmt.Errorf("duplicate map key %q", k)
|
|
}
|
|
seen[bk.Interface()] = struct{}{}
|
|
bv := reflect.New(rv.Type().Elem())
|
|
if v == nil {
|
|
if err := rejectNullEnumItem(rv.Type().Elem()); err != nil {
|
|
return err
|
|
}
|
|
null := json.RawMessage("null")
|
|
v = &null
|
|
}
|
|
if err := unmarshalJSONPb([]byte(*v), unmarshaler, bv.Interface()); err != nil {
|
|
return err
|
|
}
|
|
rv.SetMapIndex(bk, bv.Elem())
|
|
}
|
|
return nil
|
|
}
|
|
if rv.Kind() == reflect.Slice {
|
|
if rv.Type().Elem().Kind() == reflect.Uint8 {
|
|
var sl []byte
|
|
if err := d.Decode(&sl); err != nil {
|
|
return err
|
|
}
|
|
if sl != nil {
|
|
rv.SetBytes(sl)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var sl []json.RawMessage
|
|
if err := d.Decode(&sl); err != nil {
|
|
return err
|
|
}
|
|
if sl != nil {
|
|
rv.Set(reflect.MakeSlice(rv.Type(), 0, 0))
|
|
}
|
|
for _, item := range sl {
|
|
if string(item) == "null" {
|
|
if err := rejectNullEnumItem(rv.Type().Elem()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
bv := reflect.New(rv.Type().Elem())
|
|
if err := unmarshalJSONPb([]byte(item), unmarshaler, bv.Interface()); err != nil {
|
|
return err
|
|
}
|
|
rv.Set(reflect.Append(rv, bv.Elem()))
|
|
}
|
|
return nil
|
|
}
|
|
if _, ok := rv.Interface().(protoEnum); ok {
|
|
var repr interface{}
|
|
if err := d.Decode(&repr); err != nil {
|
|
return err
|
|
}
|
|
switch v := repr.(type) {
|
|
case string:
|
|
// protojson writes enums by name, so accept the name here too.
|
|
if e, ok := rv.Interface().(protoreflect.Enum); ok {
|
|
if ev := e.Descriptor().Values().ByName(protoreflect.Name(v)); ev != nil {
|
|
rv.SetInt(int64(ev.Number()))
|
|
return nil
|
|
}
|
|
if unmarshaler.DiscardUnknown {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("invalid value %q for enum %s", v, e.Descriptor().FullName())
|
|
}
|
|
return fmt.Errorf("unmarshaling of symbolic enum %q not supported: %T", repr, rv.Interface())
|
|
case float64:
|
|
// Enum values are int32 on the wire. A fractional or out-of-range
|
|
// number would otherwise be silently truncated/wrapped into a bogus
|
|
// enum here, unlike protojson which rejects it, so reject it too.
|
|
if v != math.Trunc(v) || v < math.MinInt32 || v > math.MaxInt32 {
|
|
return fmt.Errorf("%v is not a valid value for enum %T", repr, rv.Interface())
|
|
}
|
|
rv.Set(reflect.ValueOf(int32(v)).Convert(rv.Type()))
|
|
return nil
|
|
case nil:
|
|
// protojson leaves a null enum field unset.
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("cannot assign %#v into Go type %T", repr, rv.Interface())
|
|
}
|
|
}
|
|
return d.Decode(v)
|
|
}
|
|
|
|
// rejectNullEnumItem returns an error when t is an enum other than
|
|
// google.protobuf.NullValue. protojson leaves a null enum field unset, but
|
|
// rejects null as a list item or map value of such an enum.
|
|
func rejectNullEnumItem(t reflect.Type) error {
|
|
e, ok := reflect.Zero(t).Interface().(protoreflect.Enum)
|
|
if !ok || e.Descriptor().FullName() == "google.protobuf.NullValue" {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("invalid value null for enum %s", e.Descriptor().FullName())
|
|
}
|
|
|
|
// duplicateObjectKey returns the first key that appears more than once in the
|
|
// JSON object in data.
|
|
func duplicateObjectKey(data []byte) (string, bool) {
|
|
d := json.NewDecoder(bytes.NewReader(data))
|
|
if t, err := d.Token(); err != nil || t != json.Delim('{') {
|
|
return "", false
|
|
}
|
|
seen := make(map[string]struct{})
|
|
for d.More() {
|
|
t, err := d.Token()
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
k, _ := t.(string)
|
|
if _, ok := seen[k]; ok {
|
|
return k, true
|
|
}
|
|
seen[k] = struct{}{}
|
|
var v json.RawMessage
|
|
if err := d.Decode(&v); err != nil {
|
|
return "", false
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
type protoEnum interface {
|
|
fmt.Stringer
|
|
EnumDescriptor() ([]byte, []int)
|
|
}
|
|
|
|
var typeProtoEnum = reflect.TypeFor[protoEnum]()
|
|
|
|
var typeProtoMessage = reflect.TypeFor[proto.Message]()
|
|
|
|
// Delimiter for newline encoded JSON streams.
|
|
func (j *JSONPb) Delimiter() []byte {
|
|
return []byte("\n")
|
|
}
|
|
|
|
var (
|
|
convFromType = map[reflect.Kind]reflect.Value{
|
|
reflect.String: reflect.ValueOf(String),
|
|
reflect.Bool: reflect.ValueOf(Bool),
|
|
reflect.Float64: reflect.ValueOf(Float64),
|
|
reflect.Float32: reflect.ValueOf(Float32),
|
|
reflect.Int64: reflect.ValueOf(Int64),
|
|
reflect.Int32: reflect.ValueOf(Int32),
|
|
reflect.Uint64: reflect.ValueOf(Uint64),
|
|
reflect.Uint32: reflect.ValueOf(Uint32),
|
|
reflect.Slice: reflect.ValueOf(Bytes),
|
|
}
|
|
)
|