Ejemplo n.º 1
0
  /**
   * Turn a Tuple into a Message with the given type.
   *
   * @param builder a builder for the Message type the tuple will be converted to
   * @param tuple the tuple
   * @return a message representing the given tuple
   */
  public Message tupleToMessage(Builder builder, Tuple tuple) {
    List<FieldDescriptor> fieldDescriptors = builder.getDescriptorForType().getFields();

    if (tuple == null) {
      return builder.build();
    }

    for (int i = 0; i < fieldDescriptors.size() && i < tuple.size(); i++) {
      Object tupleField = null;
      FieldDescriptor fieldDescriptor = fieldDescriptors.get(i);

      try {
        tupleField = tuple.get(i);
      } catch (ExecException e) {
        LOG.warn(
            "Could not convert tuple field "
                + tupleField
                + " to field with descriptor "
                + fieldDescriptor);
        continue;
      }

      if (tupleField != null) {
        if (fieldDescriptor.isRepeated()) {
          // Repeated fields are set with Lists containing objects of the fields' Java type.
          builder.setField(
              fieldDescriptor,
              dataBagToRepeatedField(builder, fieldDescriptor, (DataBag) tupleField));
        } else {
          if (fieldDescriptor.getType() == FieldDescriptor.Type.MESSAGE) {
            Builder nestedMessageBuilder = builder.newBuilderForField(fieldDescriptor);
            builder.setField(
                fieldDescriptor,
                tupleToMessage((Builder) nestedMessageBuilder, (Tuple) tupleField));
          } else {
            builder.setField(fieldDescriptor, tupleFieldToSingleField(fieldDescriptor, tupleField));
          }
        }
      }
    }

    return builder.build();
  }
Ejemplo n.º 2
0
  private static Object readSingleFieldNoTag(
      CodedInputStream input, FieldDescriptor fd, Builder enclosingBuilder) throws IOException {
    switch (fd.getType()) {
      case DOUBLE:
        return input.readDouble();
      case FLOAT:
        return input.readFloat();
      case INT64:
      case UINT64:
        return input.readInt64();
      case INT32:
        return input.readInt32();
      case FIXED64:
        return input.readFixed64();
      case FIXED32:
        return input.readFixed32();
      case BOOL:
        return input.readBool();
      case STRING:
        return input.readString();
      case GROUP:
      case MESSAGE:
        Builder fieldBuilder = enclosingBuilder.newBuilderForField(fd);
        input.readMessage(fieldBuilder, null);
        return fieldBuilder.build();
      case BYTES:
        return input.readBytes();
      case UINT32:
        return input.readUInt32();
      case ENUM:
        EnumValueDescriptor eVal = fd.getEnumType().findValueByNumber(input.readEnum());
        // ideally if a given enum does not exist, we should search
        // unknown fields. but we don't have access to that here. return default
        return eVal != null ? eVal : fd.getDefaultValue();
      case SFIXED32:
        return input.readSFixed32();
      case SFIXED64:
        return input.readSFixed64();
      case SINT32:
        return input.readSInt32();
      case SINT64:
        return input.readSInt64();

      default:
        throw new IllegalArgumentException(
            "Unknown type " + fd.getType() + " for " + fd.getFullName());
    }
  }