Example #1
0
void binary_serialize_spec(CObjRef zthis, PHPOutputTransport& transport,
                           CArrRef spec) {
  for (ArrayIter key_ptr = spec.begin(); !key_ptr.end(); ++key_ptr) {
    Variant key = key_ptr.first();
    if (!key.isInteger()) {
      throw_tprotocolexception("Bad keytype in TSPEC (expected 'long')", INVALID_DATA);
      return;
    }
    unsigned long fieldno = key.toInt64();
    Array fieldspec = key_ptr.second().toArray();

    // field name
    String varname = fieldspec.rvalAt(PHPTransport::s_var,
                                      AccessFlags::Error_Key).toString();

    // thrift type
    int8_t ttype = fieldspec.rvalAt(PHPTransport::s_type,
                                    AccessFlags::Error_Key).toByte();

    Variant prop = zthis->o_get(varname, true, zthis->o_getClassName());
    if (!prop.isNull()) {
      transport.writeI8(ttype);
      transport.writeI16(fieldno);
      binary_serialize(ttype, transport, prop, fieldspec);
    }
  }
  transport.writeI8(T_STOP); // struct end
}
Example #2
0
Variant ArrayUtil::Map(CArrRef inputs, PFUNC_MAP map_function,
                       const void *data) {
  Array ret = Array::Create();

  if (inputs.size() == 1) {
    Array arr = inputs.begin().secondRef().toArray();
    if (!arr.empty()) {
      for (ssize_t k = arr->iter_begin(); k != ArrayData::invalid_index;
           k = arr->iter_advance(k)) {
        Array params;
        params.append(arr->getValueRef(k));
        Variant result;
        if (map_function) {
          result = map_function(params, data);
        } else {
          result = params;
        }
        ret.add(arr->getKey(k), result, true);
      }
    }
  } else {
    int maxlen = 0;
    vector<vector<ssize_t> > positions;
    positions.resize(inputs.size());
    int i = 0;
    for (ArrayIter iter(inputs); iter; ++iter, ++i) {
      Array arr = iter.secondRef().toArray();
      int count = arr.size();
      if (count > maxlen) maxlen = count;

      if (count > 0) {
        positions[i].reserve(count);
        for (ssize_t pos = arr->iter_begin(); pos != ArrayData::invalid_index;
             pos = arr->iter_advance(pos)) {
          positions[i].push_back(pos);
        }
      }
    }

    for (int k = 0; k < maxlen; k++) {
      Array params;
      int i = 0;
      for (ArrayIter iter(inputs); iter; ++iter, ++i) {
        Array arr = iter.secondRef().toArray();
        if (k < arr.size()) {
          params.append(arr->getValueRef(positions[i][k]));
        } else {
          params.append(uninit_null());
        }
      }

      Variant result;
      if (map_function) {
        result = map_function(params, data);
      } else {
        result = params;
      }

      ret.append(result);
    }
  }

  return ret;
}