Ejemplo n.º 1
0
    def convert_decimal_value(raw_value: tp.Any) -> _meta.Value:

        type_desc = _meta.TypeDescriptor(_meta.BasicType.DECIMAL)

        if isinstance(raw_value, decimal.Decimal):
            return _meta.Value(type_desc, decimalValue=_meta.DecimalValue(str(raw_value)))

        if isinstance(raw_value, int) or isinstance(raw_value, float):
            return _meta.Value(type_desc, decimalValue=_meta.DecimalValue(str(raw_value)))

        msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.DECIMAL.name}"
        raise _ex.ETracInternal(msg)
Ejemplo n.º 2
0
    def convert_float_value(raw_value: tp.Any) -> _meta.Value:

        type_desc = _meta.TypeDescriptor(_meta.BasicType.FLOAT)

        if isinstance(raw_value, float):
            return _meta.Value(type_desc, floatValue=raw_value)

        if isinstance(raw_value, int):
            return _meta.Value(type_desc, floatValue=float(raw_value))

        msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.FLOAT.name}"
        raise _ex.ETracInternal(msg)
Ejemplo n.º 3
0
    def convert_integer_value(raw_value: tp.Any) -> _meta.Value:

        type_desc = _meta.TypeDescriptor(_meta.BasicType.INTEGER)

        if isinstance(raw_value, int):
            return _meta.Value(type_desc, integerValue=raw_value)

        if isinstance(raw_value, float) and raw_value.is_integer():
            return _meta.Value(type_desc, integerValue=int(raw_value))

        msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.INTEGER.name}"
        raise _ex.ETracInternal(msg)
Ejemplo n.º 4
0
    def convert_datetime_value(raw_value: tp.Any) -> _meta.Value:

        type_desc = _meta.TypeDescriptor(_meta.BasicType.DATETIME)

        if isinstance(raw_value, dt.datetime):
            return _meta.Value(type_desc, datetimeValue=_meta.DatetimeValue(isoDatetime=raw_value.isoformat()))

        if isinstance(raw_value, str):
            datetime_value = dt.datetime.fromisoformat(raw_value)
            return _meta.Value(type_desc, datetimeValue=_meta.DatetimeValue(isoDatetime=datetime_value.isoformat()))

        msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.DATETIME.name}"
        raise _ex.ETracInternal(msg)
Ejemplo n.º 5
0
    def convert_string_value(raw_value: tp.Any) -> _meta.Value:

        type_desc = _meta.TypeDescriptor(_meta.BasicType.STRING)

        if isinstance(raw_value, str):
            return _meta.Value(type_desc, stringValue=raw_value)

        if isinstance(raw_value, bool) or \
           isinstance(raw_value, int) or \
           isinstance(raw_value, float) or \
           isinstance(raw_value, decimal.Decimal):

            return _meta.Value(type_desc, stringValue=str(raw_value))

        msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.STRING.name}"
        raise _ex.ETracInternal(msg)
Ejemplo n.º 6
0
    def encode_value(cls, value: tp.Any) -> _meta.Value:

        if value is None:
            raise _ex.ETracInternal("Cannot encode a null value")

        if isinstance(value, bool):
            type_desc = _meta.TypeDescriptor(_meta.BasicType.BOOLEAN)
            return _meta.Value(type_desc, booleanValue=value)

        if isinstance(value, int):
            type_desc = _meta.TypeDescriptor(_meta.BasicType.INTEGER)
            return _meta.Value(type_desc, integerValue=value)

        if isinstance(value, float):
            type_desc = _meta.TypeDescriptor(_meta.BasicType.FLOAT)
            return _meta.Value(type_desc, floatValue=value)

        if isinstance(value, decimal.Decimal):
            type_desc = _meta.TypeDescriptor(_meta.BasicType.DECIMAL)
            return _meta.Value(type_desc, decimalValue=_meta.DecimalValue(str(value)))

        if isinstance(value, str):
            type_desc = _meta.TypeDescriptor(_meta.BasicType.STRING)
            return _meta.Value(type_desc, stringValue=value)

        # dt.datetime inherits dt.date, so check datetime first to avoid encoding datetime as a date
        if isinstance(value, dt.datetime):
            type_desc = _meta.TypeDescriptor(_meta.BasicType.DATETIME)
            return _meta.Value(type_desc, datetimeValue=_meta.DatetimeValue(value.isoformat()))

        if isinstance(value, dt.date):
            type_desc = _meta.TypeDescriptor(_meta.BasicType.DATE)
            return _meta.Value(type_desc, dateValue=_meta.DateValue(value.isoformat()))

        raise _ex.ETracInternal(f"Encoding value type [{type(value)}] is not supported yet")
Ejemplo n.º 7
0
    def convert_boolean_value(raw_value: tp.Any) -> _meta.Value:

        type_desc = _meta.TypeDescriptor(_meta.BasicType.BOOLEAN)

        if isinstance(raw_value, bool):
            return _meta.Value(type_desc, booleanValue=raw_value)

        msg = f"Value of type [{type(raw_value)}] cannot be converted to {_meta.BasicType.BOOLEAN.name}"
        raise _ex.ETracInternal(msg)