Debian Codegraph @

v2/proto_json_stream_go127.go

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