Added option to binary encoder to skip unknown fields.

pull/13171/head
Joshua Haberman 4 years ago
parent 794ce6d061
commit 76764643ac
  1. 6
      tests/bindings/lua/BUILD
  2. 16
      tests/bindings/lua/test_upb.lua
  3. 1
      upb/bindings/lua/msg.c
  4. 7
      upb/encode.c
  5. 3
      upb/encode.h

@ -16,6 +16,7 @@ cc_test(
data = [
"test_upb.lua",
":descriptor_proto_lua",
":empty_proto_lua",
":test_messages_proto2_proto_lua",
":test_messages_proto3_proto_lua",
":test_proto_lua",
@ -49,6 +50,11 @@ lua_proto_library(
deps = ["@com_google_protobuf//:descriptor_proto"],
)
lua_proto_library(
name = "empty_proto_lua",
deps = ["@com_google_protobuf//:empty_proto"],
)
lua_proto_library(
name = "test_messages_proto3_proto_lua",
testonly = 1,

@ -5,6 +5,7 @@ local upb_test = require "tests.bindings.lua.test_pb"
local test_messages_proto3 = require "google.protobuf.test_messages_proto3_pb"
local test_messages_proto2 = require "google.protobuf.test_messages_proto2_pb"
local descriptor = require "google.protobuf.descriptor_pb"
local empty = require "google.protobuf.empty_pb"
if _VERSION >= 'Lua 5.2' then
_ENV = lunit.module("testupb", "seeall")
@ -664,6 +665,21 @@ function test_descriptor_error()
assert_nil(symtab:lookup_msg("ABC"))
end
function test_encode_skipunknown()
-- Test that upb.ENCODE_SKIPUNKNOWN does not encode unknown fields.
local msg = test_messages_proto3.TestAllTypesProto3{
optional_int32 = 10,
optional_uint32 = 20,
optional_int64 = 30,
}
local serialized = upb.encode(msg)
assert_true(#serialized > 0)
local empty_with_unknown = upb.decode(empty.Empty, serialized)
assert_true(#upb.encode(empty_with_unknown) > 0)
print(upb.encode(empty_with_unknown, upb.ENCODE_SKIPUNKNOWN))
assert_true(#upb.encode(empty_with_unknown, {upb.ENCODE_SKIPUNKNOWN}) == 0)
end
function test_gc()
local top = test_messages_proto3.TestAllTypesProto3()
local n = 100

@ -1030,6 +1030,7 @@ void lupb_msg_registertypes(lua_State *L) {
lupb_setfieldi(L, "TXTENC_NOSORT", UPB_TXTENC_NOSORT);
lupb_setfieldi(L, "ENCODE_DETERMINISTIC", UPB_ENCODE_DETERMINISTIC);
lupb_setfieldi(L, "ENCODE_SKIPUNKNOWN", UPB_ENCODE_SKIPUNKNOWN);
lupb_cacheinit(L);
}

@ -408,16 +408,17 @@ static void encode_scalarfield(upb_encstate *e, const char *msg,
static void encode_message(upb_encstate *e, const char *msg,
const upb_msglayout *m, size_t *size) {
size_t pre_len = e->limit - e->ptr;
const char *unknown;
size_t unknown_size;
const upb_msglayout_field *f = &m->fields[m->field_count];
const upb_msglayout_field *first = &m->fields[0];
unknown = upb_msg_getunknown(msg, &unknown_size);
if ((e->options & UPB_ENCODE_SKIPUNKNOWN) == 0) {
size_t unknown_size;
const char *unknown = upb_msg_getunknown(msg, &unknown_size);
if (unknown) {
encode_bytes(e, unknown, unknown_size);
}
}
while (f != first) {
f--;

@ -22,6 +22,9 @@ enum {
* If your proto contains maps, the encoder will need to malloc()/free()
* memory during encode. */
UPB_ENCODE_DETERMINISTIC = 1,
/* When set, unknown fields are not printed. */
UPB_ENCODE_SKIPUNKNOWN = 2,
};
char *upb_encode_ex(const void *msg, const upb_msglayout *l, int options,

Loading…
Cancel
Save