Skip to content

Commit 5f17c5b

Browse files
authored
Support Uuid type in core engine / Python value binding. (#208)
* Support `Uuid` type in core engine / Python value binding. * Update doc.
1 parent 5785304 commit 5f17c5b

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

docs/docs/core/data_types.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This is the list of all basic types supported by CocoIndex:
2424
| float32 | `cocoindex.typing.Float32` |`float` |
2525
| float64 | `cocoindex.typing.Float64` |`float` |
2626
| range | `cocoindex.typing.Range` | `tuple[int, int]` |
27+
| uuid | `uuid.UUId` | `uuid.UUID` |
2728
| vector[*type*, *N*?] |`Annotated[list[type], cocoindex.typing.Vector(dim=N)]` | `list[type]` |
2829
| json | `cocoindex.typing.Json` | Any type convertible to JSON by `json` package |
2930

@@ -73,6 +74,7 @@ Currently, the following types are supported as types for key fields:
7374
- `bool`
7475
- `int64`
7576
- `range`
77+
- `uuid`
7678
- Struct with all fields being key types
7779

7880
### Vector Type

python/cocoindex/convert.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import dataclasses
55
import inspect
6+
import uuid
67

78
from typing import Any, Callable
89
from .typing import analyze_type_info, COLLECTION_TYPES
@@ -13,6 +14,8 @@ def to_engine_value(value: Any) -> Any:
1314
return [to_engine_value(getattr(value, f.name)) for f in dataclasses.fields(value)]
1415
if isinstance(value, (list, tuple)):
1516
return [to_engine_value(v) for v in value]
17+
if isinstance(value, uuid.UUID):
18+
return value.bytes
1619
return value
1720

1821
def make_engine_value_converter(
@@ -62,6 +65,9 @@ def make_engine_value_converter(
6265
field_path.pop()
6366
return lambda value: [elem_converter(v) for v in value] if value is not None else None
6467

68+
if src_type_kind == 'Uuid':
69+
return lambda value: uuid.UUID(bytes=value)
70+
6571
return lambda value: value
6672

6773
def _make_engine_struct_value_converter(

python/cocoindex/typing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dataclasses
44
import types
55
import inspect
6+
import uuid
67
from typing import Annotated, NamedTuple, Any, TypeVar, TYPE_CHECKING, overload
78

89
class Vector(NamedTuple):
@@ -130,6 +131,8 @@ def analyze_type_info(t) -> AnalyzedTypeInfo:
130131
kind = 'Int64'
131132
elif t is float:
132133
kind = 'Float64'
134+
elif t is uuid.UUID:
135+
kind = 'Uuid'
133136
else:
134137
raise ValueError(f"type unsupported yet: {t}")
135138

src/py/convert.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,14 @@ fn basic_value_to_py_object<'py>(
6363
value::BasicValue::Int64(v) => v.into_bound_py_any(py)?,
6464
value::BasicValue::Float32(v) => v.into_bound_py_any(py)?,
6565
value::BasicValue::Float64(v) => v.into_bound_py_any(py)?,
66+
value::BasicValue::Range(v) => pythonize(py, v).into_py_result()?,
67+
value::BasicValue::Uuid(v) => v.as_bytes().into_bound_py_any(py)?,
68+
value::BasicValue::Json(v) => pythonize(py, v).into_py_result()?,
6669
value::BasicValue::Vector(v) => v
6770
.iter()
6871
.map(|v| basic_value_to_py_object(py, v))
6972
.collect::<PyResult<Vec<_>>>()?
7073
.into_bound_py_any(py)?,
71-
_ => {
72-
return Err(PyException::new_err(format!(
73-
"unsupported value type: {}",
74-
v.kind()
75-
)))
76-
}
7774
};
7875
Ok(result)
7976
}
@@ -129,18 +126,19 @@ fn basic_value_from_py_object<'py>(
129126
schema::BasicValueType::Int64 => value::BasicValue::Int64(v.extract::<i64>()?),
130127
schema::BasicValueType::Float32 => value::BasicValue::Float32(v.extract::<f32>()?),
131128
schema::BasicValueType::Float64 => value::BasicValue::Float64(v.extract::<f64>()?),
129+
schema::BasicValueType::Range => value::BasicValue::Range(depythonize(v)?),
130+
schema::BasicValueType::Uuid => {
131+
value::BasicValue::Uuid(uuid::Uuid::from_bytes(v.extract::<uuid::Bytes>()?))
132+
}
133+
schema::BasicValueType::Json => {
134+
value::BasicValue::Json(Arc::from(depythonize::<serde_json::Value>(v)?))
135+
}
132136
schema::BasicValueType::Vector(elem) => value::BasicValue::Vector(Arc::from(
133137
v.extract::<Vec<Bound<'py, PyAny>>>()?
134138
.into_iter()
135139
.map(|v| basic_value_from_py_object(&elem.element_type, &v))
136140
.collect::<PyResult<Vec<_>>>()?,
137141
)),
138-
_ => {
139-
return Err(PyException::new_err(format!(
140-
"unsupported value type: {}",
141-
typ
142-
)))
143-
}
144142
};
145143
Ok(result)
146144
}

0 commit comments

Comments
 (0)