diff options
author | Petteri Aimonen <jpa@github.mail.kapsi.fi> | 2016-10-10 07:20:49 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-10 07:20:49 +0300 |
commit | 060e6a6cc21eaf555cba6d3ee2558527e6790a5f (patch) | |
tree | 36020dd4606d4c9eba3c88c8827c5ebf19641393 /pb_encode.c | |
parent | 91bb64a47b36b112c9b22391ef76fab29cf2cffc (diff) | |
parent | ee44d0cee9fa87891fdc5371578f6ff3974a8d59 (diff) |
Merge pull request #216 from berni155/proto3_singular_fields_support
Proto3 singular fields support
Diffstat (limited to 'pb_encode.c')
-rw-r--r-- | pb_encode.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/pb_encode.c b/pb_encode.c index 4685614c..4f57fa5f 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -210,6 +210,23 @@ static bool checkreturn encode_basic_field(pb_ostream_t *stream, if (field->size_offset) pSize = (const char*)pData + field->size_offset; + else if (!field->size_offset && PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL) + { + /* In proto3 there are optional fields but no has_ flag, do not encode this fields + * when value is default or empty. */ + if(PB_LTYPE(field->type) == PB_LTYPE_BYTES){ + const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)pData; + if(bytes->size == 0) + implicit_has = false; + else if ((PB_LTYPE(field->type) == PB_LTYPE_STRING && *(const char*)pData == '\0') || + (field->data_size == sizeof(uint_least8_t) && *(const uint_least8_t*)pData == 0) || + (field->data_size == sizeof(uint_least16_t) && *(const uint_least16_t*)pData == 0) || + (field->data_size == sizeof(uint32_t) && *(const uint_least32_t*)pData == 0) || + (field->data_size == sizeof(uint64_t) && *(const uint_least64_t*)pData == 0)) + implicit_has = false; + } + pSize = &implicit_has; + } else pSize = &implicit_has; |