Exemple #1
0
 def schema_type_for_attribute(attribute: Any) -> str:
     # Race and ethnicity fields are the only ones that support list form. These
     # are converted to comma-separated lists stored as strings in BigQuery.
     if is_enum(attribute) or is_list(attribute) or is_str(attribute):
         return bigquery.enums.SqlTypeNames.STRING.value
     if is_int(attribute):
         return bigquery.enums.SqlTypeNames.INTEGER.value
     if is_float(attribute):
         return bigquery.enums.SqlTypeNames.FLOAT.value
     if is_date(attribute):
         return bigquery.enums.SqlTypeNames.DATE.value
     if is_bool(attribute):
         return bigquery.enums.SqlTypeNames.BOOLEAN.value
     raise ValueError(f"Unhandled attribute type for attribute: {attribute}")
Exemple #2
0
    def convert_field_value(field: attr.Attribute,
                            field_value: Union[str, EnumParser]) -> Any:
        if field_value is None:
            return None

        if is_forward_ref(field) or is_list(field):
            return field_value

        if isinstance(field_value, str):
            if not field_value or not field_value.strip():
                return None

        if field.name in converter_overrides:
            converter = converter_overrides[field.name]
            if not isinstance(field_value, converter.field_type):
                raise ValueError(
                    f"Found converter for field [{field.name}] in the converter_overrides, but expected "
                    f"field type [{converter.field_type}] does not match actual field type "
                    f"[{type(field_value)}]")
            return converter.convert(field_value)

        if isinstance(field_value, EnumParser):
            if is_enum(field):
                return field_value.parse()
            raise ValueError(
                f"Found field value [{field_value}] for field that is not an enum [{field}]."
            )

        if isinstance(field_value, str):
            if is_str(field):
                return normalize(field_value)
            if is_date(field):
                return parse_date(field_value)
            if is_int(field):
                return parse_int(field_value)
            if field.type in {bool, Union[bool, None]}:
                return parse_bool(field_value)

        raise ValueError(f"Unsupported field {field.name}")