|
1 | 1 | from abc import ABC, abstractmethod |
| 2 | +from collections.abc import Callable, Collection, Sequence |
2 | 3 | from dataclasses import dataclass |
| 4 | +from decimal import Decimal |
3 | 5 | from enum import StrEnum |
4 | | -from typing import Any, Self |
| 6 | +from typing import Any, NoReturn, Self |
5 | 7 | from jsonata import Jsonata # pyright: ignore[reportMissingTypeStubs] |
6 | 8 |
|
7 | 9 |
|
@@ -48,7 +50,132 @@ def register(cls, transform: type[Self]) -> None: |
48 | 50 | cls.registry[transform.key()] = transform |
49 | 51 |
|
50 | 52 |
|
| 53 | +class TransformError(Exception): |
| 54 | + def __init__(self, message: str, params: Sequence[Any] | dict[str, Any]) -> None: |
| 55 | + super().__init__(message) |
| 56 | + match params: |
| 57 | + case dict(): |
| 58 | + for param, value in params.items(): |
| 59 | + self.add_note(f"param({param}) = {value}") |
| 60 | + case _: |
| 61 | + for i, value in enumerate(params): |
| 62 | + self.add_note(f"param({i}) = {value}") |
| 63 | + |
| 64 | + |
| 65 | +def coalesce_numbers[T: int | float | Decimal | None](*args: T | None) -> T: |
| 66 | + for arg in args: |
| 67 | + if arg: |
| 68 | + return arg |
| 69 | + |
| 70 | + raise TransformError("No non-zero value to coalesce into", params=args) |
| 71 | + |
| 72 | + |
| 73 | +def divide[T: int | Decimal](numerator: T, denominator: T) -> float | Decimal: |
| 74 | + "JSONata native x / y operator can only divide int and float, but not Decimal" |
| 75 | + try: |
| 76 | + return numerator / denominator |
| 77 | + except Exception as error: |
| 78 | + raise TransformError("Cannot divide", params={"numerator": numerator, "denominator": denominator}) from error |
| 79 | + |
| 80 | + |
| 81 | +def is_positive[T: int | float | Decimal](value: T) -> T: |
| 82 | + if value <= 0: |
| 83 | + raise TransformError("Value is not positive", params={"value": value}) |
| 84 | + return value |
| 85 | + |
| 86 | + |
| 87 | +def left_pad_zeroes(value: str, width: int) -> str: |
| 88 | + try: |
| 89 | + return value.zfill(width) |
| 90 | + except Exception as error: |
| 91 | + raise TransformError("Cannot left pad zeroes", params={"value": value, "width": width}) from error |
| 92 | + |
| 93 | + |
| 94 | +def length(value: Collection[Any]) -> int: |
| 95 | + try: |
| 96 | + return len(value) |
| 97 | + except Exception as error: |
| 98 | + raise TransformError("Cannot get length", params={"value": value}) from error |
| 99 | + |
| 100 | + |
| 101 | +def map_to[K, V](key: K, mapping: dict[K, V]) -> V: |
| 102 | + try: |
| 103 | + return mapping[key] |
| 104 | + except Exception as error: |
| 105 | + raise TransformError("Cannot map to", params={"key": key, "mapping": mapping}) from error |
| 106 | + |
| 107 | + |
| 108 | +class SkippedRow(Exception): |
| 109 | + pass |
| 110 | + |
| 111 | + |
| 112 | +def skip_row(reason: str | None) -> NoReturn: |
| 113 | + raise SkippedRow(reason or "") |
| 114 | + |
| 115 | + |
| 116 | +def strip_whitespaces(value: str) -> str: |
| 117 | + try: |
| 118 | + return value.strip() |
| 119 | + except Exception as error: |
| 120 | + raise TransformError("Cannot strip whitespaces", params={"value": value}) from error |
| 121 | + |
| 122 | + |
| 123 | +def to_decimal(value: str | float | int) -> Decimal: |
| 124 | + try: |
| 125 | + if isinstance(value, str): |
| 126 | + return Decimal(value.strip().replace(",", ".")) |
| 127 | + else: |
| 128 | + return Decimal(value) |
| 129 | + except Exception as error: |
| 130 | + raise TransformError("Cannot convert to decimal", params={"value": value}) from error |
| 131 | + |
| 132 | + |
| 133 | +def to_int(value: str) -> int: |
| 134 | + try: |
| 135 | + return int(value.strip()) |
| 136 | + except Exception as error: |
| 137 | + raise TransformError("Cannot convert to int", params={"value": value}) from error |
| 138 | + |
| 139 | + |
| 140 | +def type_of(value: Any) -> str: |
| 141 | + match value: |
| 142 | + case int(): |
| 143 | + return "int" |
| 144 | + case float(): |
| 145 | + return "float" |
| 146 | + case Decimal(): |
| 147 | + return "decimal" |
| 148 | + case str(): |
| 149 | + return "string" |
| 150 | + case _: |
| 151 | + return str(type(value)) # pyright: ignore[reportUnknownArgumentType] |
| 152 | + |
| 153 | + |
51 | 154 | class Transform(Jsonata): |
52 | | - @classmethod |
53 | | - def build(cls, expression: str) -> "Transform": |
54 | | - return Transform(expr=expression) |
| 155 | + def __init__(self, expression: str) -> None: |
| 156 | + super().__init__(expression) |
| 157 | + self.validate_input = False |
| 158 | + |
| 159 | + @staticmethod |
| 160 | + def get_builtin_functions() -> dict[str, Callable[..., Any]]: |
| 161 | + return { |
| 162 | + "coalesce_numbers": coalesce_numbers, |
| 163 | + "divide": divide, |
| 164 | + "is_positive": is_positive, |
| 165 | + "left_pad_zeroes": left_pad_zeroes, |
| 166 | + "length": length, |
| 167 | + "map_to": map_to, |
| 168 | + "skip_row": skip_row, |
| 169 | + "strip_whitespaces": strip_whitespaces, |
| 170 | + "to_decimal": to_decimal, |
| 171 | + "to_int": to_int, |
| 172 | + "type_of": type_of, |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | +def _register_builtin_functions(): |
| 177 | + for function_name, function in Transform.get_builtin_functions().items(): |
| 178 | + Jsonata.static_frame.bind(function_name, Jsonata.JLambda(function)) |
| 179 | + |
| 180 | + |
| 181 | +_register_builtin_functions() |
0 commit comments