中文 | English
The ResultMapper provides type conversion and model mapping for query results. Use it when embedding or extending the query module programmatically.
from pt_snap_cli.query.mapper import map_result, map_results
# Map a single row
mapped = map_result(row, schema)
# Map multiple rows
mapped_all = map_results(rows, schema)from pt_snap_cli.query.mapper import ResultMapper
mapper = ResultMapper()Register custom type converters:
mapper.register_type_converter("custom", lambda x: f"custom_{x}")Register model factories:
mapper.register_model_factory("MyModel", lambda d: MyModel(d))Map to model objects:
model = mapper.map_to_model(row, "MyModel")
models = mapper.map_all_to_model(rows, "MyModel")row = {"id": "1", "size": "1024"}
schema = [
{"column": "id", "type": "int"},
{"column": "size", "type": "int"},
]
result = mapper.map(row, schema)
results = mapper.map_all(rows, schema)| Type | Description |
|---|---|
int |
Integer |
float |
Float |
str |
String |
bool |
Boolean |
hex |
Hexadecimal (integer to hex string) |
datetime |
Pass through the original value without parsing |