Merge pull request #360 from haberman/default-msgval

Added API for getting fielddef default as a upb_msgval
pull/13171/head
Joshua Haberman 4 years ago committed by GitHub
commit 5e53b5bb3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      BUILD
  2. 3
      cmake/CMakeLists.txt
  3. 12
      tests/test_cpp.cc
  4. 4
      tests/test_cpp.proto
  5. 20
      upb/def.c
  6. 1
      upb/def.h
  7. 5
      upb/def.hpp
  8. 4
      upb/handlers.h
  9. 10
      upb/reflection.h
  10. 13
      upb/reflection.hpp

@ -149,6 +149,7 @@ cc_library(
"upb/def.h",
"upb/def.hpp",
"upb/reflection.h",
"upb/reflection.hpp",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],

@ -99,7 +99,8 @@ add_library(reflection
../upb/reflection.c
../upb/def.h
../upb/def.hpp
../upb/reflection.h)
../upb/reflection.h
../upb/reflection.hpp)
target_link_libraries(reflection
descriptor_upb_proto
port

@ -977,6 +977,17 @@ void TestInlinedArena() {
ASSERT(n == 0);
}
void TestDefault() {
upb::SymbolTable symtab;
upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
upb::FieldDefPtr i32_f = md.FindFieldByName("i32");
upb::FieldDefPtr str_f = md.FindFieldByName("str");
ASSERT(i32_f && str_f);
ASSERT(i32_f.default_value().int32_val == 5);
ASSERT(strcmp(str_f.default_value().str_val.data, "abc") == 0);
ASSERT(str_f.default_value().str_val.size == 3);
}
extern "C" {
int run_tests() {
@ -1014,6 +1025,7 @@ int run_tests() {
TestHandlerDataDestruction();
TestIteration();
TestArena();
TestDefault();
return 0;
}

@ -3,9 +3,9 @@ syntax = "proto2";
package upb.test;
message TestMessage {
optional int32 i32 = 1;
optional int32 i32 = 1 [default = 5];
repeated int32 r_i32 = 2;
optional string str = 3;
optional string str = 3 [default = "abc"];
repeated string r_str = 4;
optional TestMessage msg = 5;
repeated TestMessage r_msg = 6;

@ -8,6 +8,9 @@
#include <string.h>
#include "google/protobuf/descriptor.upb.h"
#include "upb/reflection.h"
/* Must be last. */
#include "upb/port_def.inc"
typedef struct {
@ -407,6 +410,23 @@ const upb_oneofdef *upb_fielddef_realcontainingoneof(const upb_fielddef *f) {
return f->oneof;
}
upb_msgval upb_fielddef_default(const upb_fielddef *f) {
UPB_ASSERT(!upb_fielddef_issubmsg(f));
upb_msgval ret;
if (upb_fielddef_isstring(f)) {
str_t *str = f->defaultval.str;
if (str) {
ret.str_val.data = str->str;
ret.str_val.size = str->len;
} else {
ret.str_val.size = 0;
}
} else {
memcpy(&ret, &f->defaultval, 8);
}
return ret;
}
static void chkdefaulttype(const upb_fielddef *f, int ctype) {
UPB_UNUSED(f);
UPB_UNUSED(ctype);

@ -18,6 +18,7 @@
#include "upb/table.int.h"
#include "google/protobuf/descriptor.upb.h"
/* Must be last. */
#include "upb/port_def.inc"
#ifdef __cplusplus

@ -8,10 +8,13 @@
#include <vector>
#include "upb/def.h"
#include "upb/reflection.h"
#include "upb/upb.hpp"
namespace upb {
typedef upb_msgval MessageValue;
class EnumDefPtr;
class MessageDefPtr;
class OneofDefPtr;
@ -106,6 +109,8 @@ class FieldDefPtr {
float default_float() const { return upb_fielddef_defaultfloat(ptr_); }
double default_double() const { return upb_fielddef_defaultdouble(ptr_); }
MessageValue default_value() const { return upb_fielddef_default(ptr_); }
// The resulting string is always NULL-terminated. If non-NULL, the length
// will be stored in *len.
const char* default_string(size_t* len) const {

@ -22,8 +22,6 @@
#include "upb/def.h"
#include "upb/table.int.h"
#include "upb/port_def.inc"
#ifdef __cplusplus
#include "upb/def.hpp"
namespace upb {
@ -34,6 +32,8 @@ template <class T> struct CanonicalType;
} /* namespace upb */
#endif
/* Must be last. */
#include "upb/port_def.inc"
/* The maximum depth that the handler graph can have. This is a resource limit
* for the C stack since we sometimes need to recursively traverse the graph.

@ -8,6 +8,10 @@
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
typedef union {
bool bool_val;
float float_val;
@ -28,6 +32,8 @@ typedef union {
upb_array* array;
} upb_mutmsgval;
upb_msgval upb_fielddef_default(const upb_fielddef *f);
/** upb_msg *******************************************************************/
/* Creates a new message of the given type in the given arena. */
@ -163,6 +169,10 @@ upb_msgval upb_mapiter_value(const upb_map *map, size_t iter);
* iterator must not have been initialized const. */
void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#endif /* UPB_REFLECTION_H_ */

@ -0,0 +1,13 @@
#ifndef UPB_REFLECTION_HPP_
#define UPB_REFLECTION_HPP_
#include "upb/reflection.h"
namespace upb {
typedef upb_msgval MessageValue;
} // namespace upb
#endif // UPB_REFLECTION_HPP_
Loading…
Cancel
Save