Skip to content

Commit e25f49a

Browse files
authored
Refactor [En|De]codeVarint to be symetric wrt tags (firebase#837)
Since we can't decode a value before knowing it's type, I've pulled the tag handling out of these methods. More context over here: firebase#829
1 parent b4f2271 commit e25f49a

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

Firestore/core/src/firebase/firestore/remote/serializer.cc

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@ namespace {
3434
*
3535
* @param value The value to encode, represented as a uint64_t.
3636
*/
37-
void EncodeVarint(pb_ostream_t* stream, uint32_t field_number, uint64_t value) {
38-
bool status = pb_encode_tag(stream, PB_WT_VARINT, field_number);
39-
if (!status) {
40-
// TODO(rsgowman): figure out error handling
41-
abort();
42-
}
43-
44-
status = pb_encode_varint(stream, value);
37+
void EncodeVarint(pb_ostream_t* stream, uint64_t value) {
38+
bool status = pb_encode_varint(stream, value);
4539
if (!status) {
4640
// TODO(rsgowman): figure out error handling
4741
abort();
@@ -68,8 +62,7 @@ uint64_t DecodeVarint(pb_istream_t* stream) {
6862
}
6963

7064
void EncodeNull(pb_ostream_t* stream) {
71-
return EncodeVarint(stream, google_firestore_v1beta1_Value_null_value_tag,
72-
google_protobuf_NullValue_NULL_VALUE);
65+
return EncodeVarint(stream, google_protobuf_NullValue_NULL_VALUE);
7366
}
7467

7568
void DecodeNull(pb_istream_t* stream) {
@@ -81,8 +74,7 @@ void DecodeNull(pb_istream_t* stream) {
8174
}
8275

8376
void EncodeBool(pb_ostream_t* stream, bool bool_value) {
84-
return EncodeVarint(stream, google_firestore_v1beta1_Value_boolean_value_tag,
85-
bool_value);
77+
return EncodeVarint(stream, bool_value);
8678
}
8779

8880
bool DecodeBool(pb_istream_t* stream) {
@@ -99,8 +91,7 @@ bool DecodeBool(pb_istream_t* stream) {
9991
}
10092

10193
void EncodeInteger(pb_ostream_t* stream, int64_t integer_value) {
102-
return EncodeVarint(stream, google_firestore_v1beta1_Value_integer_value_tag,
103-
integer_value);
94+
return EncodeVarint(stream, integer_value);
10495
}
10596

10697
int64_t DecodeInteger(pb_istream_t* stream) {
@@ -144,16 +135,38 @@ void Serializer::EncodeTypedValue(const TypedValue& value,
144135
// going to need.
145136
uint8_t buf[1024];
146137
pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
138+
139+
// TODO(rsgowman): some refactoring is in order... but will wait until after a
140+
// non-varint, non-fixed-size (i.e. string) type is present before doing so.
141+
bool status = false;
147142
switch (value.type) {
148143
case FieldValue::Type::Null:
144+
status = pb_encode_tag(&stream, PB_WT_VARINT,
145+
google_firestore_v1beta1_Value_null_value_tag);
146+
if (!status) {
147+
// TODO(rsgowman): figure out error handling
148+
abort();
149+
}
149150
EncodeNull(&stream);
150151
break;
151152

152153
case FieldValue::Type::Boolean:
154+
status = pb_encode_tag(&stream, PB_WT_VARINT,
155+
google_firestore_v1beta1_Value_boolean_value_tag);
156+
if (!status) {
157+
// TODO(rsgowman): figure out error handling
158+
abort();
159+
}
153160
EncodeBool(&stream, value.value.boolean_value);
154161
break;
155162

156163
case FieldValue::Type::Integer:
164+
status = pb_encode_tag(&stream, PB_WT_VARINT,
165+
google_firestore_v1beta1_Value_integer_value_tag);
166+
if (!status) {
167+
// TODO(rsgowman): figure out error handling
168+
abort();
169+
}
157170
EncodeInteger(&stream, value.value.integer_value);
158171
break;
159172

0 commit comments

Comments
 (0)