Fixed bugs in textoutput.

Text output from descriptor.proto is now
identical to protoc!
pull/13171/head
Joshua Haberman 15 years ago
parent 0fcfeab521
commit 4b6c8b6b23
  1. 6
      core/upb_def.c
  2. 1
      core/upb_def.h
  3. 14
      stream/upb_textprinter.c

@ -331,6 +331,12 @@ upb_enum_iter upb_enum_next(upb_enumdef *e, upb_enum_iter iter) {
return upb_inttable_next(&e->iton, &iter->e); return upb_inttable_next(&e->iton, &iter->e);
} }
upb_string *upb_enumdef_iton(upb_enumdef *def, upb_enumval_t num) {
upb_iton_ent *e =
(upb_iton_ent*)upb_inttable_fastlookup(&def->iton, num, sizeof(*e));
return e ? e->string : NULL;
}
/* upb_fielddef ***************************************************************/ /* upb_fielddef ***************************************************************/

@ -202,6 +202,7 @@ typedef int32_t upb_enumval_t;
// Lookups from name to integer and vice-versa. // Lookups from name to integer and vice-versa.
bool upb_enumdef_ntoi(upb_enumdef *e, upb_string *name, upb_enumval_t *num); bool upb_enumdef_ntoi(upb_enumdef *e, upb_string *name, upb_enumval_t *num);
// Caller does not own a ref on the returned string.
upb_string *upb_enumdef_iton(upb_enumdef *e, upb_enumval_t num); upb_string *upb_enumdef_iton(upb_enumdef *e, upb_enumval_t num);
// Iteration over name/value pairs. The order is undefined. // Iteration over name/value pairs. The order is undefined.

@ -29,6 +29,15 @@ static void upb_textprinter_endfield(upb_textprinter *p)
} }
static bool upb_textprinter_putval(upb_textprinter *p, upb_value val) { static bool upb_textprinter_putval(upb_textprinter *p, upb_value val) {
upb_bytesink_put(p->bytesink, UPB_STRLIT(": "));
upb_enumdef *enum_def;
upb_string *enum_label;
if(p->f->type == UPB_TYPE(ENUM) &&
(enum_def = upb_downcast_enumdef(p->f->def)) != NULL &&
(enum_label = upb_enumdef_iton(enum_def, val.int32)) != NULL) {
// This is an enum value for which we found a corresponding string.
upb_bytesink_put(p->bytesink, enum_label);
} else {
p->str = upb_string_tryrecycle(p->str); p->str = upb_string_tryrecycle(p->str);
#define CASE(fmtstr, member) upb_string_printf(p->str, fmtstr, val.member); break; #define CASE(fmtstr, member) upb_string_printf(p->str, fmtstr, val.member); break;
switch(p->f->type) { switch(p->f->type) {
@ -55,12 +64,13 @@ static bool upb_textprinter_putval(upb_textprinter *p, upb_value val) {
CASE("%hhu", _bool); CASE("%hhu", _bool);
} }
upb_bytesink_put(p->bytesink, p->str); upb_bytesink_put(p->bytesink, p->str);
}
upb_textprinter_endfield(p); upb_textprinter_endfield(p);
return upb_ok(upb_bytesink_status(p->bytesink)); return upb_ok(upb_bytesink_status(p->bytesink));
} }
static bool upb_textprinter_putstr(upb_textprinter *p, upb_string *str) { static bool upb_textprinter_putstr(upb_textprinter *p, upb_string *str) {
upb_bytesink_put(p->bytesink, UPB_STRLIT("\"")); upb_bytesink_put(p->bytesink, UPB_STRLIT(": \""));
// TODO: escaping. // TODO: escaping.
upb_bytesink_put(p->bytesink, str); upb_bytesink_put(p->bytesink, str);
upb_bytesink_put(p->bytesink, UPB_STRLIT("\"")); upb_bytesink_put(p->bytesink, UPB_STRLIT("\""));
@ -79,14 +89,12 @@ static bool upb_textprinter_putdef(upb_textprinter *p, upb_fielddef *f)
{ {
upb_textprinter_indent(p); upb_textprinter_indent(p);
upb_bytesink_put(p->bytesink, f->name); upb_bytesink_put(p->bytesink, f->name);
upb_bytesink_put(p->bytesink, UPB_STRLIT(": "));
p->f = f; p->f = f;
return upb_ok(upb_bytesink_status(p->bytesink)); return upb_ok(upb_bytesink_status(p->bytesink));
} }
static bool upb_textprinter_startmsg(upb_textprinter *p) static bool upb_textprinter_startmsg(upb_textprinter *p)
{ {
upb_bytesink_put(p->bytesink, p->f->def->fqname);
upb_bytesink_put(p->bytesink, UPB_STRLIT(" {")); upb_bytesink_put(p->bytesink, UPB_STRLIT(" {"));
if(!p->single_line) upb_bytesink_put(p->bytesink, UPB_STRLIT("\n")); if(!p->single_line) upb_bytesink_put(p->bytesink, UPB_STRLIT("\n"));
p->indent_depth++; p->indent_depth++;

Loading…
Cancel
Save