Skip to content

Commit 47743f4

Browse files
authored
[En|De]codeUnsignedVarint -> [En|De]codeVarint (firebase#817)
* [En|De]codeUnsignedVarint -> [En|De]codeVarint The 'unsigned' portion was misleading, as these varints work with both signed and unsigned integers. (The 'signed' varints also work with both signed and unsigned integers, but use zig-zag encoding so that negative numbers are encoded more efficiently. Note that 'signed' varints aren't used in the Value proto, so won't appear in the serializer class for at least the short term.) Added some docstrings to help disambiguate this.
1 parent 115512c commit 47743f4

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ namespace remote {
2525

2626
namespace {
2727

28-
void EncodeUnsignedVarint(pb_ostream_t* stream,
29-
uint32_t field_number,
30-
uint64_t value) {
28+
/**
29+
* Note that (despite the value parameter type) this works for bool, enum,
30+
* int32, int64, uint32 and uint64 proto field types.
31+
*
32+
* Note: This is not expected to be called direclty, but rather only via the
33+
* other Encode* methods (i.e. EncodeBool, EncodeLong, etc)
34+
*
35+
* @param value The value to encode, represented as a uint64_t.
36+
*/
37+
void EncodeVarint(pb_ostream_t* stream, uint32_t field_number, uint64_t value) {
3138
bool status = pb_encode_tag(stream, PB_WT_VARINT, field_number);
3239
if (!status) {
3340
// TODO(rsgowman): figure out error handling
@@ -41,7 +48,16 @@ void EncodeUnsignedVarint(pb_ostream_t* stream,
4148
}
4249
}
4350

44-
uint64_t DecodeUnsignedVarint(pb_istream_t* stream) {
51+
/**
52+
* Note that (despite the return type) this works for bool, enum, int32, int64,
53+
* uint32 and uint64 proto field types.
54+
*
55+
* Note: This is not expected to be called direclty, but rather only via the
56+
* other Decode* methods (i.e. DecodeBool, DecodeLong, etc)
57+
*
58+
* @return The decoded varint as a uint64_t.
59+
*/
60+
uint64_t DecodeVarint(pb_istream_t* stream) {
4561
uint64_t varint_value;
4662
bool status = pb_decode_varint(stream, &varint_value);
4763
if (!status) {
@@ -52,26 +68,25 @@ uint64_t DecodeUnsignedVarint(pb_istream_t* stream) {
5268
}
5369

5470
void EncodeNull(pb_ostream_t* stream) {
55-
return EncodeUnsignedVarint(stream,
56-
google_firestore_v1beta1_Value_null_value_tag,
57-
google_protobuf_NullValue_NULL_VALUE);
71+
return EncodeVarint(stream, google_firestore_v1beta1_Value_null_value_tag,
72+
google_protobuf_NullValue_NULL_VALUE);
5873
}
5974

6075
void DecodeNull(pb_istream_t* stream) {
61-
uint64_t varint = DecodeUnsignedVarint(stream);
76+
uint64_t varint = DecodeVarint(stream);
6277
if (varint != google_protobuf_NullValue_NULL_VALUE) {
6378
// TODO(rsgowman): figure out error handling
6479
abort();
6580
}
6681
}
6782

6883
void EncodeBool(pb_ostream_t* stream, bool bool_value) {
69-
return EncodeUnsignedVarint(
70-
stream, google_firestore_v1beta1_Value_boolean_value_tag, bool_value);
84+
return EncodeVarint(stream, google_firestore_v1beta1_Value_boolean_value_tag,
85+
bool_value);
7186
}
7287

7388
bool DecodeBool(pb_istream_t* stream) {
74-
uint64_t varint = DecodeUnsignedVarint(stream);
89+
uint64_t varint = DecodeVarint(stream);
7590
switch (varint) {
7691
case 0:
7792
return false;

0 commit comments

Comments
 (0)