static void test_mapentry_check() { upb_status s = UPB_STATUS_INIT; upb_msgdef *m = upb_msgdef_new(&m); upb_fielddef *f = upb_fielddef_new(&f); upb_symtab *symtab = upb_symtab_new(&symtab); upb_msgdef *subm = upb_msgdef_new(&subm); upb_def *defs[2]; upb_msgdef_setfullname(m, "TestMessage", &s); upb_fielddef_setname(f, "field1", &s); upb_fielddef_setnumber(f, 1, &s); upb_fielddef_setlabel(f, UPB_LABEL_OPTIONAL); upb_fielddef_settype(f, UPB_TYPE_MESSAGE); upb_fielddef_setsubdefname(f, ".MapEntry", &s); upb_msgdef_addfield(m, f, &f, &s); ASSERT(upb_ok(&s)); upb_msgdef_setfullname(subm, "MapEntry", &s); upb_msgdef_setmapentry(subm, true); defs[0] = upb_msgdef_upcast_mutable(m); defs[1] = upb_msgdef_upcast_mutable(subm); upb_symtab_add(symtab, defs, 2, NULL, &s); /* Should not have succeeded: non-repeated field pointing to a MapEntry. */ ASSERT(!upb_ok(&s)); upb_fielddef_setlabel(f, UPB_LABEL_REPEATED); upb_symtab_add(symtab, defs, 2, NULL, &s); ASSERT(upb_ok(&s)); upb_symtab_unref(symtab, &symtab); upb_msgdef_unref(subm, &subm); upb_msgdef_unref(m, &m); }
static void test_symbol_resolution() { upb_status s = UPB_STATUS_INIT; upb_def *defs[2]; upb_msgdef *m1; upb_msgdef *m2; upb_msgdef *m3; upb_fielddef *m3_field1; upb_fielddef *m3_field2; upb_symtab *symtab = upb_symtab_new(&symtab); ASSERT(symtab); /* m1 has name "A.B.C" and no fields. We'll add it to the symtab now. */ m1 = upb_msgdef_new(&m1); ASSERT(m1); ASSERT_STATUS(upb_msgdef_setfullname(m1, "A.B.C", &s), &s); ASSERT_STATUS(upb_symtab_add(symtab, (upb_def**)&m1, 1, NULL, &s), &s); /* m2 has name "D.E" and no fields. We'll add it in the same batch as m3 * below. */ m2 = upb_msgdef_new(&m2); ASSERT(m2); ASSERT_STATUS(upb_msgdef_setfullname(m2, "D.E", &s), &s); /* m3 has name "F.G" and two fields, of type A.B.C and D.E respectively. We'll * add it in the same batch as m2 above. */ m3 = upb_msgdef_new(&m3); ASSERT(m3); ASSERT_STATUS(upb_msgdef_setfullname(m3, "F.G", &s), &s); m3_field1 = upb_fielddef_new(&m3_field1); ASSERT_STATUS(upb_fielddef_setname(m3_field1, "field1", &s), &s); ASSERT_STATUS(upb_fielddef_setnumber(m3_field1, 1, &s), &s); upb_fielddef_setlabel(m3_field1, UPB_LABEL_OPTIONAL); upb_fielddef_settype(m3_field1, UPB_TYPE_MESSAGE); ASSERT_STATUS(upb_fielddef_setsubdefname(m3_field1, ".A.B.C", &s), &s); ASSERT_STATUS(upb_msgdef_addfield(m3, m3_field1, NULL, &s), &s); m3_field2 = upb_fielddef_new(&m3_field2); ASSERT_STATUS(upb_fielddef_setname(m3_field2, "field2", &s), &s); ASSERT_STATUS(upb_fielddef_setnumber(m3_field2, 2, &s), &s); upb_fielddef_setlabel(m3_field2, UPB_LABEL_OPTIONAL); upb_fielddef_settype(m3_field2, UPB_TYPE_MESSAGE); ASSERT_STATUS(upb_fielddef_setsubdefname(m3_field2, ".D.E", &s), &s); ASSERT_STATUS(upb_msgdef_addfield(m3, m3_field2, NULL, &s), &s); defs[0] = upb_msgdef_upcast_mutable(m2); defs[1] = upb_msgdef_upcast_mutable(m3); ASSERT_STATUS(upb_symtab_add(symtab, defs, 2, NULL, &s), &s); upb_fielddef_unref(m3_field2, &m3_field2); upb_fielddef_unref(m3_field1, &m3_field1); upb_msgdef_unref(m3, &m3); upb_msgdef_unref(m2, &m2); upb_msgdef_unref(m1, &m1); upb_symtab_unref(symtab, &symtab); }
// Handlers for google.protobuf.DescriptorProto (representing a message). static bool msg_startmsg(void *closure, const void *hd) { UPB_UNUSED(hd); upb_descreader *r = closure; upb_deflist_push(&r->defs, UPB_UPCAST(upb_msgdef_new(&r->defs))); upb_descreader_startcontainer(r); return true; }
upb_msgdef *upb_msgdef_dup(const upb_msgdef *m) { upb_msgdef *newm = upb_msgdef_new(); newm->size = m->size; newm->hasbit_bytes = m->hasbit_bytes; newm->extstart = m->extstart; newm->extend = m->extend; upb_msg_iter i; for(i = upb_msg_begin(m); !upb_msg_done(i); i = upb_msg_next(m, i)) { upb_msgdef_addfield(newm, upb_fielddef_dup(upb_msg_iter_field(i))); } return newm; }
/* * call-seq: * Descriptor.new => descriptor * * Creates a new, empty, message type descriptor. At a minimum, its name must be * set before it is added to a pool. It cannot be used to create messages until * it is added to a pool, after which it becomes immutable (as part of a * finalization process). */ VALUE Descriptor_alloc(VALUE klass) { Descriptor* self = ALLOC(Descriptor); VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self); self->msgdef = upb_msgdef_new(&self->msgdef); self->klass = Qnil; self->layout = NULL; self->fill_handlers = NULL; self->fill_method = NULL; self->pb_serialize_handlers = NULL; self->json_serialize_handlers = NULL; self->typeclass_references = rb_ary_new(); return ret; }
upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner) { upb_msgdef *newm = upb_msgdef_new(owner); if (!newm) return NULL; upb_def_setfullname(upb_upcast(newm), upb_def_fullname(upb_upcast(m))); upb_msg_iter i; for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { upb_fielddef *f = upb_fielddef_dup(upb_msg_iter_field(&i), &f); if (!f || !upb_msgdef_addfield(newm, f, &f)) { upb_msgdef_unref(newm, owner); return NULL; } } return newm; }
static void test_descriptor_flags() { upb_msgdef *m = upb_msgdef_new(&m); upb_msgdef *m2; upb_status s = UPB_STATUS_INIT; ASSERT(upb_msgdef_mapentry(m) == false); upb_msgdef_setfullname(m, "TestMessage", &s); ASSERT(upb_ok(&s)); upb_msgdef_setmapentry(m, true); ASSERT(upb_msgdef_mapentry(m) == true); m2 = upb_msgdef_dup(m, &m2); ASSERT(upb_msgdef_mapentry(m2) == true); upb_msgdef_unref(m, &m); upb_msgdef_unref(m2, &m2); }
static void test_noreftracking() { /* Reftracking is not required; clients can pass UPB_UNTRACKED_REF for owner. */ upb_msgdef *md = upb_msgdef_new(UPB_UNTRACKED_REF); upb_msgdef_ref(md, UPB_UNTRACKED_REF); /* Clients can mix tracked and untracked refs. */ upb_msgdef_ref(md, &md); upb_msgdef_unref(md, UPB_UNTRACKED_REF); upb_msgdef_unref(md, UPB_UNTRACKED_REF); /* Call some random function on the messagedef to test that it is alive. */ ASSERT(!upb_msgdef_isfrozen(md)); upb_msgdef_unref(md, &md); }
upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner) { upb_msgdef *newm = upb_msgdef_new(owner); if (!newm) return NULL; bool ok = upb_def_setfullname(UPB_UPCAST(newm), upb_def_fullname(UPB_UPCAST(m)), NULL); UPB_ASSERT_VAR(ok, ok); upb_msg_iter i; for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { upb_fielddef *f = upb_fielddef_dup(upb_msg_iter_field(&i), &f); if (!f || !upb_msgdef_addfield(newm, f, &f, NULL)) { upb_msgdef_unref(newm, owner); return NULL; } } return newm; }
static PyObject *PyUpb_MessageDef_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) { return PyUpb_ObjCacheGet(upb_msgdef_new(), subtype); }
static upb_msgdef *upb_msgdef_newnamed(const char *name, void *owner) { upb_msgdef *m = upb_msgdef_new(owner); upb_msgdef_setfullname(m, name, NULL); return m; }
// Handlers for google.protobuf.DescriptorProto (representing a message). static bool msg_startmsg(void *_r) { upb_descreader *r = _r; upb_deflist_push(&r->defs, upb_upcast(upb_msgdef_new(&r->defs))); upb_descreader_startcontainer(r); return true; }