CanBeOfType🔗

Type: Transpilable verification function
Check that value can be used as an operand of valueType.

Following the normative JSON schema of the query language, a field is implicitly cast to the type of the other operand, and can hence be used as an operand of any type. A global attribute giving a date and time (LOCALNOW, UTCNOW or CLIENTNOW) can be used both as a string and as a date-time, while all the other attributes are strings.

See: https://industrialdigitaltwin.io/aas-specifications/IDTA-01002/v3.2/query-language.html#_json_schema

value🔗
Operand of a comparison
value_type🔗
One of string, number, hex, boolean, date_time or time
Return🔗
True if value can be used as valueType

Code

if isinstance(value, Field_operand):
    return True

if isinstance(value, Attribute_operand):
    if value_type == "string":
        return True

    attribute = value.attribute
    return (
        value_type == "date_time"
        and isinstance(attribute, Global_attribute)
        and (
            attribute.kind == Global_attribute_kind.Local_now
            or attribute.kind == Global_attribute_kind.UTC_now
            or attribute.kind == Global_attribute_kind.Client_now
        )
    )

if isinstance(value, (String_literal, String_cast)):
    return value_type == "string"

if isinstance(
    value,
    (Number_literal, Number_cast, Day_of_week, Day_of_month, Month, Year),
):
    return value_type == "number"

if isinstance(value, (Hex_literal, Hex_cast)):
    return value_type == "hex"

if isinstance(value, (Boolean_literal, Boolean_cast)):
    return value_type == "boolean"

if isinstance(value, (Date_time_literal, Date_time_cast)):
    return value_type == "date_time"

if isinstance(value, (Time_literal, Time_cast)):
    return value_type == "time"

return False