|
16 | 16 |
|
17 | 17 | #include "Firestore/core/src/firebase/firestore/nanopb/writer.h"
|
18 | 18 |
|
19 |
| -#include "Firestore/Protos/nanopb/google/firestore/v1beta1/document.nanopb.h" |
20 | 19 | #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
|
21 | 20 |
|
22 | 21 | namespace firebase {
|
23 | 22 | namespace firestore {
|
24 | 23 | namespace nanopb {
|
25 | 24 |
|
26 |
| -using std::int64_t; |
27 |
| -using std::int8_t; |
28 |
| -using std::uint64_t; |
29 |
| - |
30 | 25 | namespace {
|
31 | 26 |
|
32 | 27 | // TODO(rsgowman): find a better home for this constant.
|
@@ -66,111 +61,21 @@ pb_ostream_t WrapContainer(Container* out_container) {
|
66 | 61 |
|
67 | 62 | } // namespace
|
68 | 63 |
|
69 |
| -Writer Writer::Wrap(std::vector<uint8_t>* out_bytes) { |
| 64 | +Writer Writer::Wrap(std::vector<std::uint8_t>* out_bytes) { |
70 | 65 | return Writer{WrapContainer(out_bytes)};
|
71 | 66 | }
|
72 | 67 |
|
73 | 68 | Writer Writer::Wrap(std::string* out_string) {
|
74 | 69 | return Writer{WrapContainer(out_string)};
|
75 | 70 | }
|
76 | 71 |
|
77 |
| -void Writer::WriteTag(Tag tag) { |
78 |
| - if (!pb_encode_tag(&stream_, tag.wire_type, tag.field_number)) { |
79 |
| - HARD_FAIL(PB_GET_ERROR(&stream_)); |
80 |
| - } |
81 |
| -} |
82 |
| - |
83 | 72 | void Writer::WriteNanopbMessage(const pb_field_t fields[],
|
84 | 73 | const void* src_struct) {
|
85 | 74 | if (!pb_encode(&stream_, fields, src_struct)) {
|
86 | 75 | HARD_FAIL(PB_GET_ERROR(&stream_));
|
87 | 76 | }
|
88 | 77 | }
|
89 | 78 |
|
90 |
| -void Writer::WriteSize(size_t size) { |
91 |
| - return WriteVarint(size); |
92 |
| -} |
93 |
| - |
94 |
| -void Writer::WriteVarint(uint64_t value) { |
95 |
| - if (!pb_encode_varint(&stream_, value)) { |
96 |
| - HARD_FAIL(PB_GET_ERROR(&stream_)); |
97 |
| - } |
98 |
| -} |
99 |
| - |
100 |
| -void Writer::WriteNull() { |
101 |
| - return WriteVarint(google_protobuf_NullValue_NULL_VALUE); |
102 |
| -} |
103 |
| - |
104 |
| -void Writer::WriteBool(bool bool_value) { |
105 |
| - return WriteVarint(bool_value); |
106 |
| -} |
107 |
| - |
108 |
| -void Writer::WriteInteger(int64_t integer_value) { |
109 |
| - return WriteVarint(integer_value); |
110 |
| -} |
111 |
| - |
112 |
| -void Writer::WriteString(const std::string& string_value) { |
113 |
| - if (!pb_encode_string( |
114 |
| - &stream_, reinterpret_cast<const pb_byte_t*>(string_value.c_str()), |
115 |
| - string_value.length())) { |
116 |
| - HARD_FAIL(PB_GET_ERROR(&stream_)); |
117 |
| - } |
118 |
| -} |
119 |
| - |
120 |
| -void Writer::WriteBytes(const std::vector<uint8_t>& bytes) { |
121 |
| - if (!pb_encode_string(&stream_, |
122 |
| - reinterpret_cast<const pb_byte_t*>(bytes.data()), |
123 |
| - bytes.size())) { |
124 |
| - HARD_FAIL(PB_GET_ERROR(&stream_)); |
125 |
| - } |
126 |
| -} |
127 |
| - |
128 |
| -void Writer::WriteNestedMessage( |
129 |
| - const std::function<void(Writer*)>& write_message_fn) { |
130 |
| - // First calculate the message size using a non-writing substream. |
131 |
| - Writer sizer = Writer::Sizing(); |
132 |
| - write_message_fn(&sizer); |
133 |
| - size_t size = sizer.bytes_written(); |
134 |
| - |
135 |
| - // Write out the size to the output writer. |
136 |
| - WriteSize(size); |
137 |
| - |
138 |
| - // If this stream is itself a sizing stream, then we don't need to actually |
139 |
| - // parse field_value a second time; just update the bytes_written via a call |
140 |
| - // to pb_write. (If we try to write the contents into a sizing stream, it'll |
141 |
| - // fail since sizing streams don't actually have any buffer space.) |
142 |
| - if (stream_.callback == nullptr) { |
143 |
| - if (!pb_write(&stream_, nullptr, size)) { |
144 |
| - HARD_FAIL(PB_GET_ERROR(&stream_)); |
145 |
| - } |
146 |
| - return; |
147 |
| - } |
148 |
| - |
149 |
| - // Ensure the output stream has enough space |
150 |
| - if (stream_.bytes_written + size > stream_.max_size) { |
151 |
| - HARD_FAIL( |
152 |
| - "Insufficient space in the output stream to write the given message"); |
153 |
| - } |
154 |
| - |
155 |
| - // Use a substream to verify that a callback doesn't write more than what it |
156 |
| - // did the first time. (Use an initializer rather than setting fields |
157 |
| - // individually like nanopb does. This gives us a *chance* of noticing if |
158 |
| - // nanopb adds new fields.) |
159 |
| - Writer writer({stream_.callback, stream_.state, |
160 |
| - /*max_size=*/size, /*bytes_written=*/0, |
161 |
| - /*errmsg=*/nullptr}); |
162 |
| - write_message_fn(&writer); |
163 |
| - |
164 |
| - stream_.bytes_written += writer.stream_.bytes_written; |
165 |
| - stream_.state = writer.stream_.state; |
166 |
| - stream_.errmsg = writer.stream_.errmsg; |
167 |
| - |
168 |
| - if (writer.bytes_written() != size) { |
169 |
| - // submsg size changed |
170 |
| - HARD_FAIL("Parsing the nested message twice yielded different sizes"); |
171 |
| - } |
172 |
| -} |
173 |
| - |
174 | 79 | } // namespace nanopb
|
175 | 80 | } // namespace firestore
|
176 | 81 | } // namespace firebase
|
0 commit comments