java/src/main/java/com/google/fhir/stu3/BigQuerySchema.java
/* Generate a schema for a specific FieldDescriptor, with an optional message instance. */
Generate a schema for a specific FieldDescriptor, with an optional message instance.
Generate a schema for a specific FieldDescriptor with an optional message instance .
private static TableFieldSchema fromFieldDescriptor(FieldDescriptor fieldDescriptor) {
    String fieldName = fieldDescriptor.getJsonName();
    TableFieldSchema field =
        new TableFieldSchema()
            .setName(fieldName)
            .setMode(fieldDescriptor.isRepeated() ? "REPEATED" : "NULLABLE");
    if (fieldDescriptor.getType() != FieldDescriptor.Type.MESSAGE) {
      throw new IllegalStateException(
          "Unexpected primitive field: " + fieldDescriptor.getFullName());
    }
    Descriptor fieldType = fieldDescriptor.getMessageType();
    if (fieldName.equals("id")
        && !AnnotationUtils.isResource(fieldDescriptor.getContainingType())) {
      return null;
    }
    if (AnnotationUtils.isPrimitiveType(fieldType)) {
      return field.setType(schemaTypeForPrimitive(fieldType));
    }
    if (fieldType.equals(Extension.getDescriptor())
        || fieldType.equals(ContainedResource.getDescriptor())) {
      return field.setType("STRING");
    }
    if (fieldType.equals(fieldDescriptor.getContainingType())) {
      return null;
    }
    if (fieldDescriptor.getContainingType().equals(Identifier.getDescriptor())
        && fieldName.equals("assigner")) {
      return null;
    }
    field.setType("RECORD");
    return field.setFields(fromDescriptor(fieldType, fieldDescriptor).getFields());
  }
private static TableFieldSchema fromFieldDescriptor ( FieldDescriptor fieldDescriptor ) { String fieldName = fieldDescriptor . getJsonName ( ) ; TableFieldSchema field = new TableFieldSchema ( ) . setName ( fieldName ) . setMode ( fieldDescriptor . isRepeated ( ) ? "REPEATED" : "NULLABLE" ) ; if ( fieldDescriptor . getType ( ) != FieldDescriptor . Type . MESSAGE ) { throw new IllegalStateException ( "Unexpected primitive field: " + fieldDescriptor . getFullName ( ) ) ; } Descriptor fieldType = fieldDescriptor . getMessageType ( ) ; if ( fieldName . equals ( "id" ) && ! AnnotationUtils . isResource ( fieldDescriptor . getContainingType ( ) ) ) { return null ; } if ( AnnotationUtils . isPrimitiveType ( fieldType ) ) { return field . setType ( schemaTypeForPrimitive ( fieldType ) ) ; } if ( fieldType . equals ( Extension . getDescriptor ( ) ) || fieldType . equals ( ContainedResource . getDescriptor ( ) ) ) { return field . setType ( "STRING" ) ; } if ( fieldType . equals ( fieldDescriptor . getContainingType ( ) ) ) { return null ; } if ( fieldDescriptor . getContainingType ( ) . equals ( Identifier . getDescriptor ( ) ) && fieldName . equals ( "assigner" ) ) { return null ; } field . setType ( "RECORD" ) ; return field . setFields ( fromDescriptor ( fieldType , fieldDescriptor ) . getFields ( ) ) ; }
Generate a schema for a specific FieldDescriptor, with an optional message instance.
Generate a schema for a specific FieldDescriptor with an optional message instance .
// We directly use the jsonName of the field when generating the schema. // We don't include the "id" except for resources. // We don't include extensions or contained resources unless they exist in the data. // We don't include nested types. // TODO: consider allowing a certain level of nesting. // Identifier and Reference refer to each other. We stop the recursion by not including // Identifier.assigner unless it exists in the data.