Skip to content

[En|De]codeUnsignedVarint -> [En|De]codeVarint #817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions Firestore/core/src/firebase/firestore/remote/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ namespace remote {

namespace {

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

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

void EncodeNull(pb_ostream_t* stream) {
return EncodeUnsignedVarint(stream,
google_firestore_v1beta1_Value_null_value_tag,
google_protobuf_NullValue_NULL_VALUE);
return EncodeVarint(stream, google_firestore_v1beta1_Value_null_value_tag,
google_protobuf_NullValue_NULL_VALUE);
}

void DecodeNull(pb_istream_t* stream) {
uint64_t varint = DecodeUnsignedVarint(stream);
uint64_t varint = DecodeVarint(stream);
if (varint != google_protobuf_NullValue_NULL_VALUE) {
// TODO(rsgowman): figure out error handling
abort();
}
}

void EncodeBool(pb_ostream_t* stream, bool bool_value) {
return EncodeUnsignedVarint(
stream, google_firestore_v1beta1_Value_boolean_value_tag, bool_value);
return EncodeVarint(stream, google_firestore_v1beta1_Value_boolean_value_tag,
bool_value);
}

bool DecodeBool(pb_istream_t* stream) {
uint64_t varint = DecodeUnsignedVarint(stream);
uint64_t varint = DecodeVarint(stream);
switch (varint) {
case 0:
return false;
Expand Down