Merge pull request #9 from cfallin/map-fields

Added msgdef flag to indicate map_entry protos.
pull/13171/head
Joshua Haberman 10 years ago
commit 0ab8d5e1bb
  1. 15
      tests/test_def.c
  2. 11
      upb/def.c
  3. 20
      upb/def.h

@ -328,6 +328,20 @@ static void test_partial_freeze() {
upb_msgdef_unref(m3, &m3); upb_msgdef_unref(m3, &m3);
} }
static void test_descriptor_flags() {
upb_msgdef *m = upb_msgdef_new(&m);
ASSERT(upb_msgdef_mapentry(m) == false);
upb_status s = UPB_STATUS_INIT;
upb_msgdef_setfullname(m, "TestMessage", &s);
ASSERT(upb_ok(&s));
upb_msgdef_setmapentry(m, true);
ASSERT(upb_msgdef_mapentry(m) == true);
upb_msgdef *m2 = upb_msgdef_dup(m, &m2);
ASSERT(upb_msgdef_mapentry(m2) == true);
upb_msgdef_unref(m, &m);
upb_msgdef_unref(m2, &m2);
}
int run_tests(int argc, char *argv[]) { int run_tests(int argc, char *argv[]) {
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "Usage: test_def <test.proto.pb>\n"); fprintf(stderr, "Usage: test_def <test.proto.pb>\n");
@ -343,5 +357,6 @@ int run_tests(int argc, char *argv[]) {
test_freeze_free(); test_freeze_free();
test_partial_freeze(); test_partial_freeze();
test_noreftracking(); test_noreftracking();
test_descriptor_flags();
return 0; return 0;
} }

@ -1270,6 +1270,7 @@ upb_msgdef *upb_msgdef_new(const void *owner) {
if (!upb_def_init(UPB_UPCAST(m), UPB_DEF_MSG, &vtbl, owner)) goto err2; if (!upb_def_init(UPB_UPCAST(m), UPB_DEF_MSG, &vtbl, owner)) goto err2;
if (!upb_inttable_init(&m->itof, UPB_CTYPE_PTR)) goto err2; if (!upb_inttable_init(&m->itof, UPB_CTYPE_PTR)) goto err2;
if (!upb_strtable_init(&m->ntof, UPB_CTYPE_PTR)) goto err1; if (!upb_strtable_init(&m->ntof, UPB_CTYPE_PTR)) goto err1;
m->map_entry = false;
return m; return m;
err1: err1:
@ -1284,6 +1285,7 @@ upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner) {
if (!newm) return NULL; if (!newm) return NULL;
bool ok = upb_def_setfullname(UPB_UPCAST(newm), bool ok = upb_def_setfullname(UPB_UPCAST(newm),
upb_def_fullname(UPB_UPCAST(m)), NULL); upb_def_fullname(UPB_UPCAST(m)), NULL);
newm->map_entry = m->map_entry;
UPB_ASSERT_VAR(ok, ok); UPB_ASSERT_VAR(ok, ok);
upb_msg_iter i; upb_msg_iter i;
for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) {
@ -1387,6 +1389,15 @@ int upb_msgdef_numfields(const upb_msgdef *m) {
return upb_strtable_count(&m->ntof); return upb_strtable_count(&m->ntof);
} }
void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry) {
assert(!upb_msgdef_isfrozen(m));
m->map_entry = map_entry;
}
bool upb_msgdef_mapentry(const upb_msgdef *m) {
return m->map_entry;
}
void upb_msg_begin(upb_msg_iter *iter, const upb_msgdef *m) { void upb_msg_begin(upb_msg_iter *iter, const upb_msgdef *m) {
upb_inttable_begin(iter, &m->itof); upb_inttable_begin(iter, &m->itof);
} }

@ -712,6 +712,10 @@ UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE(
// just be moved into symtab.c? // just be moved into symtab.c?
MessageDef* Dup(const void* owner) const; MessageDef* Dup(const void* owner) const;
// Is this message a map entry?
void setmapentry(bool map_entry);
bool mapentry() const;
// Iteration over fields. The order is undefined. // Iteration over fields. The order is undefined.
class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> { class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
public: public:
@ -758,6 +762,11 @@ UPB_DEFINE_STRUCT(upb_msgdef, upb_def,
upb_inttable itof; // int to field upb_inttable itof; // int to field
upb_strtable ntof; // name to field upb_strtable ntof; // name to field
// Is this a map-entry message?
// TODO: set this flag properly for static descriptors; regenerate
// descriptor.upb.c.
bool map_entry;
// TODO(haberman): proper extension ranges (there can be multiple). // TODO(haberman): proper extension ranges (there can be multiple).
)); ));
@ -765,7 +774,7 @@ UPB_DEFINE_STRUCT(upb_msgdef, upb_def,
refs, ref2s) \ refs, ref2s) \
{ \ { \
UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \ UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \
submsg_field_count, itof, ntof \ submsg_field_count, itof, ntof, false \
} }
UPB_BEGIN_EXTERN_C // { UPB_BEGIN_EXTERN_C // {
@ -813,6 +822,9 @@ UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m,
return (upb_fielddef *)upb_msgdef_ntof(m, name, len); return (upb_fielddef *)upb_msgdef_ntof(m, name, len);
} }
void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry);
bool upb_msgdef_mapentry(const upb_msgdef *m);
// upb_msg_iter i; // upb_msg_iter i;
// for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { // for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) {
// upb_fielddef *f = upb_msg_iter_field(&i); // upb_fielddef *f = upb_msg_iter_field(&i);
@ -1266,6 +1278,12 @@ inline const FieldDef *MessageDef::FindFieldByName(const char *name,
inline MessageDef* MessageDef::Dup(const void *owner) const { inline MessageDef* MessageDef::Dup(const void *owner) const {
return upb_msgdef_dup(this, owner); return upb_msgdef_dup(this, owner);
} }
inline void MessageDef::setmapentry(bool map_entry) {
upb_msgdef_setmapentry(this, map_entry);
}
inline bool MessageDef::mapentry() const {
return upb_msgdef_mapentry(this);
}
inline MessageDef::iterator MessageDef::begin() { return iterator(this); } inline MessageDef::iterator MessageDef::begin() { return iterator(this); }
inline MessageDef::iterator MessageDef::end() { return iterator::end(this); } inline MessageDef::iterator MessageDef::end() { return iterator::end(this); }
inline MessageDef::const_iterator MessageDef::begin() const { inline MessageDef::const_iterator MessageDef::begin() const {

Loading…
Cancel
Save