private void writeField()

in odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/commons/proto/ProtobufRecordStreamWriter.java [207:385]


  private void writeField(Object v, TypeInfo typeInfo) throws IOException {
    switch (typeInfo.getOdpsType()) {
      case BOOLEAN: {
        boolean value = (Boolean) v;
        crc.update(value);
        out.writeBoolNoTag(value);
        break;
      }
      case DATETIME: {
        long longValue;
        if (v instanceof ZonedDateTime) {
          longValue = ((ZonedDateTime) v).toInstant().toEpochMilli();
        } else {
          longValue = ((Date) v).getTime();
        }

        if (shouldTransform) {
          longValue = DateUtils.date2ms(new Date(longValue), DateUtils.LOCAL_CAL);
        }
        crc.update(longValue);
        out.writeSInt64NoTag(longValue);
        break;
      }
      case DATE: {
        long longValue;
        LocalDate localDate;
        if (v instanceof LocalDate) {
          localDate = (LocalDate) v;
        } else {
          // to date in GMT, for compatible
          localDate = OdpsTypeTransformer.dateToLocalDate((java.sql.Date)v, DEFAULT_CALENDAR);
        }
        longValue = localDate.toEpochDay();
        crc.update(longValue);
        out.writeSInt64NoTag(longValue);
        break;
      }
      case TIMESTAMP_NTZ: {
        LocalDateTime localDateTime = (LocalDateTime) v;
        Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
        int nano = instant.getNano();
        long value = instant.getEpochSecond();
        crc.update(value);
        crc.update(nano);
        out.writeSInt64NoTag(value);
        out.writeSInt32NoTag(nano);
        break;
      }
      case TIMESTAMP: {
        Instant instant;
        if (v instanceof Instant) {
          instant = (Instant) v;
        } else {
          instant = ((Timestamp) v).toInstant();
        }
        int nano = instant.getNano();
        long value = instant.getEpochSecond();
        crc.update(value);
        crc.update(nano);
        out.writeSInt64NoTag(value);
        out.writeSInt32NoTag(nano);
        break;
      }
      case INTERVAL_DAY_TIME: {
        long value = ((IntervalDayTime) v).getTotalSeconds();
        int nano = ((IntervalDayTime) v).getNanos();
        crc.update(value);
        crc.update(nano);
        out.writeSInt64NoTag(value);
        out.writeSInt32NoTag(nano);
        break;
      }
      case VARCHAR:
      case CHAR: {
        byte [] bytes;
        bytes = ((AbstractChar) v).getValue().getBytes("UTF-8");
        crc.update(bytes, 0, bytes.length);
        writeRawBytes(bytes, out);
        break;
      }
      case JSON: {
        String value;
        if (v instanceof String) {
          value = (String) v;
        } else {
          value = ((SimpleJsonValue) v).toString();
        }
        byte[] bytes = value.getBytes("UTF-8");
        crc.update(bytes, 0, bytes.length);
        writeRawBytes(bytes, out);
        break;
      }
      case STRING: {
        byte[] bytes;
        if (v instanceof String) {
          String value = (String) v;
          bytes = value.getBytes("UTF-8");
        } else {
          bytes = (byte[]) v;
        }
        crc.update(bytes, 0, bytes.length);
        writeRawBytes(bytes, out);
        break;
      }
      case BINARY: {
        byte[] bytes = ((Binary) v).data();

        crc.update(bytes, 0, bytes.length);
        writeRawBytes(bytes, out);
        break;
      }
      case DOUBLE: {
        double value = (Double) v;
        crc.update(value);
        out.writeDoubleNoTag(value);
        break;
      }
      case FLOAT: {
        float value = (Float) v;
        crc.update(value);
        out.writeFloatNoTag(value);
        break;
      }
      case BIGINT: {
        long value = (Long) v;
        crc.update(value);
        out.writeSInt64NoTag(value);
        break;
      }
      case INTERVAL_YEAR_MONTH: {
        long value = ((IntervalYearMonth) v).getTotalMonths();
        crc.update(value);
        out.writeSInt64NoTag(value);
        break;
      }
      case INT: {
        long value = ((Integer) v).longValue();
        crc.update(value);
        out.writeSInt64NoTag(value);
        break;
      }
      case SMALLINT: {
        long value = ((Short) v).longValue();
        crc.update(value);
        out.writeSInt64NoTag(value);
        break;
      }
      case TINYINT: {
        long value = ((Byte) v).longValue();
        crc.update(value);
        out.writeSInt64NoTag(value);
        break;
      }
      case DECIMAL: {
        String value = ((BigDecimal) v).toPlainString();
        byte[] bytes = value.getBytes("UTF-8");
        crc.update(bytes, 0, bytes.length);
        writeRawBytes(bytes, out);
        break;
      }
      case ARRAY: {
        writeArray((List) v, ((ArrayTypeInfo) typeInfo).getElementTypeInfo());
        break;
      }
      case MAP: {
        MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;

        writeMap((Map) v, mapTypeInfo.getKeyTypeInfo(),
                 mapTypeInfo.getValueTypeInfo());
        break;
      }
      case STRUCT: {
        writeStruct((Struct) v, (StructTypeInfo) typeInfo);
        break;
      }
      default:
        throw new IOException("Invalid data type: " + typeInfo);
    }
  }