| 1 |
From c7d7b73e44b0a062cea51e3c4f2ca07f301a4f66 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Seth Hollyman <[email protected]> |
| 3 |
Date: Thu, 3 Sep 2026 17:47:33 +0000 |
| 4 |
Subject: [PATCH 1/2] fix: split implementation of ProtoJSONStream for 1.27 and |
| 5 |
later |
| 6 |
|
| 7 |
This PR uses go build tags to provide an updated implementation of |
| 8 |
ProtoJSONStream compatible with go 1.27 which has includes major |
| 9 |
revisions to the encoding/json packages (there's a new v2). |
| 10 |
|
| 11 |
In the future when we're only supporting 1.27 and later we can remove |
| 12 |
the old implementation entirely. |
| 13 |
--- |
| 14 |
v2/proto_json_stream.go | 2 + |
| 15 |
v2/proto_json_stream_go127.go | 135 ++++++++++++++++++++++++++++++++++ |
| 16 |
2 files changed, 137 insertions(+) |
| 17 |
create mode 100644 v2/proto_json_stream_go127.go |
| 18 |
|
| 19 |
diff --git a/v2/proto_json_stream.go b/v2/proto_json_stream.go |
| 20 |
index 9b690d40..fa8beeff 100644 |
| 21 |
--- a/v2/proto_json_stream.go |
| 22 |
+++ b/v2/proto_json_stream.go |
| 23 |
@@ -27,6 +27,8 @@ |
| 24 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
|
| 27 |
+//go:build !go1.27 |
| 28 |
+ |
| 29 |
package gax |
| 30 |
|
| 31 |
import ( |
| 32 |
diff --git a/v2/proto_json_stream_go127.go b/v2/proto_json_stream_go127.go |
| 33 |
new file mode 100644 |
| 34 |
index 00000000..0b1534b2 |
| 35 |
--- /dev/null |
| 36 |
+++ b/v2/proto_json_stream_go127.go |
| 37 |
@@ -0,0 +1,135 @@ |
| 38 |
+// Copyright 2022, Google Inc. |
| 39 |
+// All rights reserved. |
| 40 |
+// |
| 41 |
+// Redistribution and use in source and binary forms, with or without |
| 42 |
+// modification, are permitted provided that the following conditions are |
| 43 |
+// met: |
| 44 |
+// |
| 45 |
+// * Redistributions of source code must retain the above copyright |
| 46 |
+// notice, this list of conditions and the following disclaimer. |
| 47 |
+// * Redistributions in binary form must reproduce the above |
| 48 |
+// copyright notice, this list of conditions and the following disclaimer |
| 49 |
+// in the documentation and/or other materials provided with the |
| 50 |
+// distribution. |
| 51 |
+// * Neither the name of Google Inc. nor the names of its |
| 52 |
+// contributors may be used to endorse or promote products derived from |
| 53 |
+// this software without specific prior written permission. |
| 54 |
+// |
| 55 |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 56 |
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 57 |
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 58 |
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 59 |
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 60 |
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 61 |
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 62 |
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 63 |
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 64 |
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 65 |
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 66 |
+ |
| 67 |
+//go:build go1.27 |
| 68 |
+ |
| 69 |
+package gax |
| 70 |
+ |
| 71 |
+import ( |
| 72 |
+ "encoding/json/jsontext" |
| 73 |
+ "errors" |
| 74 |
+ "io" |
| 75 |
+ |
| 76 |
+ "google.golang.org/protobuf/encoding/protojson" |
| 77 |
+ "google.golang.org/protobuf/proto" |
| 78 |
+ "google.golang.org/protobuf/reflect/protoreflect" |
| 79 |
+) |
| 80 |
+ |
| 81 |
+var ( |
| 82 |
+ errBadOpening = errors.New("unexpected opening token, expected '['") |
| 83 |
+) |
| 84 |
+ |
| 85 |
+// ProtoJSONStream represents a wrapper for consuming a stream of protobuf |
| 86 |
+// messages encoded using protobuf-JSON format. More information on this format |
| 87 |
+// can be found at https://developers.google.com/protocol-buffers/docs/proto3#json. |
| 88 |
+// The stream must appear as a comma-delimited, JSON array of obbjects with |
| 89 |
+// opening and closing square braces. |
| 90 |
+// |
| 91 |
+// This is for internal use only. |
| 92 |
+type ProtoJSONStream struct { |
| 93 |
+ first, closed bool |
| 94 |
+ reader io.ReadCloser |
| 95 |
+ stream *jsontext.Decoder |
| 96 |
+ typ protoreflect.MessageType |
| 97 |
+} |
| 98 |
+ |
| 99 |
+// NewProtoJSONStreamReader accepts a stream of bytes via an io.ReadCloser that are |
| 100 |
+// protobuf-JSON encoded protobuf messages of the given type. The ProtoJSONStream |
| 101 |
+// must be closed when done. |
| 102 |
+// |
| 103 |
+// This is for internal use only. |
| 104 |
+func NewProtoJSONStreamReader(rc io.ReadCloser, typ protoreflect.MessageType) *ProtoJSONStream { |
| 105 |
+ return &ProtoJSONStream{ |
| 106 |
+ first: true, |
| 107 |
+ reader: rc, |
| 108 |
+ stream: jsontext.NewDecoder(rc), |
| 109 |
+ typ: typ, |
| 110 |
+ } |
| 111 |
+} |
| 112 |
+ |
| 113 |
+// Recv decodes the next protobuf message in the stream or returns io.EOF if |
| 114 |
+// the stream is done. It is not safe to call Recv on the same stream from |
| 115 |
+// different goroutines, just like it is not safe to do so with a single gRPC |
| 116 |
+// stream. Type-cast the protobuf message returned to the type provided at |
| 117 |
+// ProtoJSONStream creation. |
| 118 |
+// Calls to Recv after calling Close will produce io.EOF. |
| 119 |
+func (s *ProtoJSONStream) Recv() (proto.Message, error) { |
| 120 |
+ if s.closed { |
| 121 |
+ return nil, io.EOF |
| 122 |
+ } |
| 123 |
+ // Handle start-of-stream (which should be a JSON array) |
| 124 |
+ if s.first { |
| 125 |
+ s.first = false |
| 126 |
+ kind := s.stream.PeekKind() |
| 127 |
+ |
| 128 |
+ if kind == jsontext.KindBeginArray { |
| 129 |
+ // Read and discard the start of array. |
| 130 |
+ err := s.stream.SkipValue() |
| 131 |
+ if err != nil { |
| 132 |
+ return nil, err |
| 133 |
+ } |
| 134 |
+ } else { |
| 135 |
+ // stream isn't a JSON array. |
| 136 |
+ return nil, errBadOpening |
| 137 |
+ } |
| 138 |
+ } |
| 139 |
+ |
| 140 |
+ // Ensure we're not at the end of the stream by looking for end of array. |
| 141 |
+ kind := s.stream.PeekKind() |
| 142 |
+ if kind == jsontext.KindEndArray { |
| 143 |
+ // We're at the end of the array. |
| 144 |
+ err := s.stream.SkipValue() |
| 145 |
+ if err != nil { |
| 146 |
+ return nil, err |
| 147 |
+ } |
| 148 |
+ return nil, io.EOF |
| 149 |
+ } |
| 150 |
+ |
| 151 |
+ // Get the next value. |
| 152 |
+ v, err := s.stream.ReadValue() |
| 153 |
+ if err != nil { |
| 154 |
+ return nil, err |
| 155 |
+ } |
| 156 |
+ |
| 157 |
+ // Initialize a new instance of the protobuf message to unmarshal the |
| 158 |
+ // raw data into. |
| 159 |
+ m := s.typ.New().Interface() |
| 160 |
+ unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true} |
| 161 |
+ err = unm.Unmarshal(v, m) |
| 162 |
+ return m, err |
| 163 |
+} |
| 164 |
+ |
| 165 |
+// Close closes the stream so that resources are cleaned up. |
| 166 |
+func (s *ProtoJSONStream) Close() error { |
| 167 |
+ // Dereference the *jsontext.Decoder so that the memory is gc'd. |
| 168 |
+ s.stream = nil |
| 169 |
+ s.closed = true |
| 170 |
+ |
| 171 |
+ return s.reader.Close() |
| 172 |
+} |