proto_json_stream.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. package gax
  30. import (
  31. "encoding/json"
  32. "errors"
  33. "io"
  34. "google.golang.org/protobuf/encoding/protojson"
  35. "google.golang.org/protobuf/proto"
  36. "google.golang.org/protobuf/reflect/protoreflect"
  37. )
  38. var (
  39. arrayOpen = json.Delim('[')
  40. arrayClose = json.Delim(']')
  41. errBadOpening = errors.New("unexpected opening token, expected '['")
  42. )
  43. // ProtoJSONStream represents a wrapper for consuming a stream of protobuf
  44. // messages encoded using protobuf-JSON format. More information on this format
  45. // can be found at https://developers.google.com/protocol-buffers/docs/proto3#json.
  46. // The stream must appear as a comma-delimited, JSON array of obbjects with
  47. // opening and closing square braces.
  48. //
  49. // This is for internal use only.
  50. type ProtoJSONStream struct {
  51. first, closed bool
  52. reader io.ReadCloser
  53. stream *json.Decoder
  54. typ protoreflect.MessageType
  55. }
  56. // NewProtoJSONStreamReader accepts a stream of bytes via an io.ReadCloser that are
  57. // protobuf-JSON encoded protobuf messages of the given type. The ProtoJSONStream
  58. // must be closed when done.
  59. //
  60. // This is for internal use only.
  61. func NewProtoJSONStreamReader(rc io.ReadCloser, typ protoreflect.MessageType) *ProtoJSONStream {
  62. return &ProtoJSONStream{
  63. first: true,
  64. reader: rc,
  65. stream: json.NewDecoder(rc),
  66. typ: typ,
  67. }
  68. }
  69. // Recv decodes the next protobuf message in the stream or returns io.EOF if
  70. // the stream is done. It is not safe to call Recv on the same stream from
  71. // different goroutines, just like it is not safe to do so with a single gRPC
  72. // stream. Type-cast the protobuf message returned to the type provided at
  73. // ProtoJSONStream creation.
  74. // Calls to Recv after calling Close will produce io.EOF.
  75. func (s *ProtoJSONStream) Recv() (proto.Message, error) {
  76. if s.closed {
  77. return nil, io.EOF
  78. }
  79. if s.first {
  80. s.first = false
  81. // Consume the opening '[' so Decode gets one object at a time.
  82. if t, err := s.stream.Token(); err != nil {
  83. return nil, err
  84. } else if t != arrayOpen {
  85. return nil, errBadOpening
  86. }
  87. }
  88. // Capture the next block of data for the item (a JSON object) in the stream.
  89. var raw json.RawMessage
  90. if err := s.stream.Decode(&raw); err != nil {
  91. e := err
  92. // To avoid checking the first token of each stream, just attempt to
  93. // Decode the next blob and if that fails, double check if it is just
  94. // the closing token ']'. If it is the closing, return io.EOF. If it
  95. // isn't, return the original error.
  96. if t, _ := s.stream.Token(); t == arrayClose {
  97. e = io.EOF
  98. }
  99. return nil, e
  100. }
  101. // Initialize a new instance of the protobuf message to unmarshal the
  102. // raw data into.
  103. m := s.typ.New().Interface()
  104. err := protojson.Unmarshal(raw, m)
  105. return m, err
  106. }
  107. // Close closes the stream so that resources are cleaned up.
  108. func (s *ProtoJSONStream) Close() error {
  109. // Dereference the *json.Decoder so that the memory is gc'd.
  110. s.stream = nil
  111. s.closed = true
  112. return s.reader.Close()
  113. }