Naïve computation of a serialized upb message PiperOrigin-RevId: 670990379pull/18006/head
parent
b01eee755e
commit
290f28871f
5 changed files with 138 additions and 0 deletions
@ -0,0 +1,37 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#include "upb/wire/byte_size.h" |
||||
|
||||
#include <stddef.h> |
||||
|
||||
#include "upb/mem/arena.h" |
||||
#include "upb/message/message.h" |
||||
#include "upb/mini_table/message.h" |
||||
#include "upb/wire/encode.h" |
||||
|
||||
// Must be last.
|
||||
#include "upb/port/def.inc" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
size_t upb_ByteSize(const upb_Message* msg, const upb_MiniTable* mt) { |
||||
upb_Arena* arena = upb_Arena_New(); |
||||
char* buf; |
||||
size_t res = 0; |
||||
|
||||
upb_Encode(msg, mt, 0, arena, &buf, &res); |
||||
|
||||
upb_Arena_Free(arena); |
||||
return res; |
||||
} |
||||
|
||||
#ifdef __cplusplus |
||||
} // extern "C"
|
||||
#endif |
@ -0,0 +1,31 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#ifndef UPB_WIRE_BYTE_SIZE_H_ |
||||
#define UPB_WIRE_BYTE_SIZE_H_ |
||||
|
||||
#include <stddef.h> |
||||
|
||||
#include "upb/message/message.h" |
||||
#include "upb/mini_table/message.h" |
||||
|
||||
// Must be last.
|
||||
#include "upb/port/def.inc" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
UPB_API size_t upb_ByteSize(const upb_Message* msg, const upb_MiniTable* mt); |
||||
|
||||
#ifdef __cplusplus |
||||
} // extern "C"
|
||||
#endif |
||||
|
||||
#include "upb/port/undef.inc" |
||||
|
||||
#endif // UPB_WIRE_BYTE_SIZE_H_
|
@ -0,0 +1,40 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#include "upb/wire/byte_size.h" |
||||
|
||||
#include <gtest/gtest.h> |
||||
#include "google/protobuf/test_messages_proto2.upb.h" |
||||
#include "google/protobuf/test_messages_proto2.upb_minitable.h" |
||||
#include "upb/base/upcast.h" |
||||
#include "upb/mem/arena.h" |
||||
#include "upb/mini_table/message.h" |
||||
|
||||
namespace { |
||||
static const upb_MiniTable* kTestMiniTable = |
||||
&protobuf_0test_0messages__proto2__TestAllTypesProto2_msg_init; |
||||
|
||||
TEST(ByteSizeTest, UnpopulatedMsg) { |
||||
upb_Arena* arena = upb_Arena_New(); |
||||
protobuf_test_messages_proto2_TestAllTypesProto2* msg = |
||||
protobuf_test_messages_proto2_TestAllTypesProto2_new(arena); |
||||
auto res = upb_ByteSize(UPB_UPCAST(msg), kTestMiniTable); |
||||
EXPECT_EQ(res, 0); |
||||
upb_Arena_Free(arena); |
||||
} |
||||
|
||||
TEST(ByteSizeTest, PopulatedMsg) { |
||||
upb_Arena* arena = upb_Arena_New(); |
||||
protobuf_test_messages_proto2_TestAllTypesProto2* msg = |
||||
protobuf_test_messages_proto2_TestAllTypesProto2_new(arena); |
||||
protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_int32(msg, 322); |
||||
auto res = upb_ByteSize(UPB_UPCAST(msg), kTestMiniTable); |
||||
EXPECT_EQ(res, 3); |
||||
upb_Arena_Free(arena); |
||||
} |
||||
|
||||
} // namespace
|
Loading…
Reference in new issue