def coerce_to_int(val):
    """coerce_to_int coerces a value to an integer."""
    if isinstance(val, int):
        return val
    if val is None:
        return 0
    # Otherwise, val must be a string, since that's the only other kind of data
    # type that we can create. Strip out all non-digit characters.
    new_val = "".join([c for c in val if c in map(str, list(range(10)))])
    if new_val == "":
        return 0
    return int(new_val)