Example #1
0
static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink,
                   int depth) {
  if (map == Qnil) return;
  Map* self = ruby_to_Map(map);

  upb_sink subsink;

  upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);

  assert(upb_fielddef_type(f) == UPB_TYPE_MESSAGE);
  const upb_fielddef* key_field = map_field_key(f);
  const upb_fielddef* value_field = map_field_value(f);

  Map_iter it;
  for (Map_begin(map, &it); !Map_done(&it); Map_next(&it)) {
    VALUE key = Map_iter_key(&it);
    VALUE value = Map_iter_value(&it);

    upb_sink entry_sink;
    upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG),
                         &entry_sink);
    upb_sink_startmsg(&entry_sink);

    put_ruby_value(key, key_field, Qnil, depth + 1, &entry_sink);
    put_ruby_value(value, value_field, self->value_type_class, depth + 1,
                   &entry_sink);

    upb_status status;
    upb_sink_endmsg(&entry_sink, &status);
    upb_sink_endsubmsg(&subsink, getsel(f, UPB_HANDLER_ENDSUBMSG));
  }

  upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
}
Example #2
0
static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink,
                   int depth) {
    upb_sink subsink;
    upb_fieldtype_t type = upb_fielddef_type(f);
    upb_selector_t sel = 0;
    int size;

    if (ary == Qnil) return;

    upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);

    if (upb_fielddef_isprimitive(f)) {
        sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
    }

    size = NUM2INT(RepeatedField_length(ary));
    for (int i = 0; i < size; i++) {
        void* memory = RepeatedField_index_native(ary, i);
        switch (type) {
#define T(upbtypeconst, upbtype, ctype)                         \
  case upbtypeconst:                                            \
    upb_sink_put##upbtype(&subsink, sel, *((ctype *)memory));   \
    break;

            T(UPB_TYPE_FLOAT,  float,  float)
            T(UPB_TYPE_DOUBLE, double, double)
            T(UPB_TYPE_BOOL,   bool,   int8_t)
        case UPB_TYPE_ENUM:
            T(UPB_TYPE_INT32,  int32,  int32_t)
            T(UPB_TYPE_UINT32, uint32, uint32_t)
            T(UPB_TYPE_INT64,  int64,  int64_t)
            T(UPB_TYPE_UINT64, uint64, uint64_t)

        case UPB_TYPE_STRING:
        case UPB_TYPE_BYTES:
            putstr(*((VALUE *)memory), f, &subsink);
            break;
        case UPB_TYPE_MESSAGE:
            putsubmsg(*((VALUE *)memory), f, &subsink, depth);
            break;

#undef T

        }
    }
    upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
}