From c7d7b73e44b0a062cea51e3c4f2ca07f301a4f66 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Thu, 3 Sep 2026 17:47:33 +0000 Subject: [PATCH 1/2] fix: split implementation of ProtoJSONStream for 1.27 and later This PR uses go build tags to provide an updated implementation of ProtoJSONStream compatible with go 1.27 which has includes major revisions to the encoding/json packages (there's a new v2). In the future when we're only supporting 1.27 and later we can remove the old implementation entirely. --- v2/proto_json_stream.go | 2 + v2/proto_json_stream_go127.go | 135 ++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 v2/proto_json_stream_go127.go diff --git a/v2/proto_json_stream.go b/v2/proto_json_stream.go index 9b690d40..fa8beeff 100644 --- a/v2/proto_json_stream.go +++ b/v2/proto_json_stream.go @@ -27,6 +27,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//go:build !go1.27 + package gax import ( diff --git a/v2/proto_json_stream_go127.go b/v2/proto_json_stream_go127.go new file mode 100644 index 00000000..0b1534b2 --- /dev/null +++ b/v2/proto_json_stream_go127.go @@ -0,0 +1,135 @@ +// Copyright 2022, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +//go:build go1.27 + +package gax + +import ( + "encoding/json/jsontext" + "errors" + "io" + + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" +) + +var ( + errBadOpening = errors.New("unexpected opening token, expected '['") +) + +// ProtoJSONStream represents a wrapper for consuming a stream of protobuf +// messages encoded using protobuf-JSON format. More information on this format +// can be found at https://developers.google.com/protocol-buffers/docs/proto3#json. +// The stream must appear as a comma-delimited, JSON array of obbjects with +// opening and closing square braces. +// +// This is for internal use only. +type ProtoJSONStream struct { + first, closed bool + reader io.ReadCloser + stream *jsontext.Decoder + typ protoreflect.MessageType +} + +// NewProtoJSONStreamReader accepts a stream of bytes via an io.ReadCloser that are +// protobuf-JSON encoded protobuf messages of the given type. The ProtoJSONStream +// must be closed when done. +// +// This is for internal use only. +func NewProtoJSONStreamReader(rc io.ReadCloser, typ protoreflect.MessageType) *ProtoJSONStream { + return &ProtoJSONStream{ + first: true, + reader: rc, + stream: jsontext.NewDecoder(rc), + typ: typ, + } +} + +// Recv decodes the next protobuf message in the stream or returns io.EOF if +// the stream is done. It is not safe to call Recv on the same stream from +// different goroutines, just like it is not safe to do so with a single gRPC +// stream. Type-cast the protobuf message returned to the type provided at +// ProtoJSONStream creation. +// Calls to Recv after calling Close will produce io.EOF. +func (s *ProtoJSONStream) Recv() (proto.Message, error) { + if s.closed { + return nil, io.EOF + } + // Handle start-of-stream (which should be a JSON array) + if s.first { + s.first = false + kind := s.stream.PeekKind() + + if kind == jsontext.KindBeginArray { + // Read and discard the start of array. + err := s.stream.SkipValue() + if err != nil { + return nil, err + } + } else { + // stream isn't a JSON array. + return nil, errBadOpening + } + } + + // Ensure we're not at the end of the stream by looking for end of array. + kind := s.stream.PeekKind() + if kind == jsontext.KindEndArray { + // We're at the end of the array. + err := s.stream.SkipValue() + if err != nil { + return nil, err + } + return nil, io.EOF + } + + // Get the next value. + v, err := s.stream.ReadValue() + if err != nil { + return nil, err + } + + // Initialize a new instance of the protobuf message to unmarshal the + // raw data into. + m := s.typ.New().Interface() + unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} + err = unm.Unmarshal(v, m) + return m, err +} + +// Close closes the stream so that resources are cleaned up. +func (s *ProtoJSONStream) Close() error { + // Dereference the *jsontext.Decoder so that the memory is gc'd. + s.stream = nil + s.closed = true + + return s.reader.Close() +}