Java Code Examples for org.apache.avro.reflect.AvroSchema

The following examples show how to use org.apache.avro.reflect.AvroSchema. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: beam   Source File: AvroCoder.java    License: Apache License 2.0 6 votes vote down vote up
private void recurse(String context, TypeDescriptor<?> type, Schema schema) {
  if (type.getRawType().isAnnotationPresent(AvroSchema.class)) {
    reportError(context, "Custom schemas are not supported -- remove @AvroSchema.");
    return;
  }

  if (!activeTypes.add(type)) {
    reportError(context, "%s appears recursively", type);
    return;
  }

  // If the the record isn't a true class, but rather a GenericRecord, SpecificRecord, etc.
  // with a specified schema, then we need to make the decision based on the generated
  // implementations.
  if (isSubtypeOf(type, IndexedRecord.class)) {
    checkIndexedRecord(context, schema, null);
  } else {
    doCheck(context, type, schema);
  }

  activeTypes.remove(type);
}
 
Example 2
Source Project: beam   Source File: AvroCoder.java    License: Apache License 2.0 6 votes vote down vote up
private void checkRecord(TypeDescriptor<?> type, Schema schema) {
  // For a record, we want to make sure that all the fields are deterministic.
  Class<?> clazz = type.getRawType();
  for (Schema.Field fieldSchema : schema.getFields()) {
    Field field = getField(clazz, fieldSchema.name());
    String fieldContext = field.getDeclaringClass().getName() + "#" + field.getName();

    if (field.isAnnotationPresent(AvroEncode.class)) {
      reportError(
          fieldContext, "Custom encoders may be non-deterministic -- remove @AvroEncode");
      continue;
    }

    if (!IndexedRecord.class.isAssignableFrom(field.getType())
        && field.isAnnotationPresent(AvroSchema.class)) {
      // TODO: We should be able to support custom schemas on POJO fields, but we shouldn't
      // need to, so we just allow it in the case of IndexedRecords.
      reportError(
          fieldContext, "Custom schemas are only supported for subtypes of IndexedRecord.");
      continue;
    }

    TypeDescriptor<?> fieldType = type.resolveType(field.getGenericType());
    recurse(fieldContext, fieldType, fieldSchema.schema());
  }
}