private static int compareGenericRecordField()

in spanner-data-validator-java/src/main/java/com/google/migration/Helpers.java [310:342]


  private static int compareGenericRecordField(Schema.Field field,
      GenericRecord lhs,
      GenericRecord rhs) {
    Schema.Type type = field.schema().getType();
    String fieldName = field.name();

    if(type == Schema.Type.UNION) {
      type = field.schema().getTypes().get(0).getType();
    }

    switch (type) {
      case STRING:
        return lhs.get(fieldName).toString().compareTo(rhs.get(fieldName).toString());
      case INT:
        int lhsInt = (int)lhs.get(fieldName);
        int rhsInt = (int)rhs.get(fieldName);
        return Integer.compare(lhsInt, rhsInt);
      case BOOLEAN:
        boolean lhsBool = (boolean)lhs.get(fieldName);
        boolean rhsBool = (boolean)rhs.get(fieldName);
        return Boolean.compare(lhsBool, rhsBool);
      case LONG:
        long lhsLong = (long)lhs.get(fieldName);
        long rhsLong = (long)rhs.get(fieldName);
        return Long.compare(lhsLong, rhsLong);
      default:
        break;
    } // switch

    String errMsg = String.format("Unrecognized type: %s, field name: %s", type, fieldName);
    LOG.error(errMsg);
    throw new RuntimeException(errMsg);
  }