mirror of
https://github.com/manifoldbt/manifoldbt.git
synced 2026-08-24 22:48:05 +00:00
Python DSL, examples, docs, benchmarks, and tests. Rust engine distributed as pre-compiled wheel via PyPI.
31 lines
892 B
Python
31 lines
892 B
Python
"""Internal helpers to produce JSON matching Rust serde externally-tagged enums."""
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
from typing import Any
|
|
|
|
|
|
def scalar_value_to_json(value: Any) -> Any:
|
|
"""Convert a Python value to a Rust ScalarValue JSON representation.
|
|
|
|
Rust serde format (externally tagged):
|
|
None -> "Null"
|
|
bool -> {"Bool": v}
|
|
int -> {"Int64": v}
|
|
float -> {"Float64": v}
|
|
str -> {"Utf8": v}
|
|
"""
|
|
if value is None:
|
|
return "Null"
|
|
if isinstance(value, bool):
|
|
return {"Bool": value}
|
|
if isinstance(value, int):
|
|
return {"Int64": value}
|
|
if isinstance(value, float):
|
|
if math.isnan(value):
|
|
return "NaN"
|
|
return {"Float64": value}
|
|
if isinstance(value, str):
|
|
return {"Utf8": value}
|
|
raise TypeError(f"Cannot convert {type(value).__name__} to ScalarValue")
|