Skip to content

Commit 64a77e0

Browse files
committed
Core: Comma-join groupBy and fields parameters
These used to be dict-ed into a list of string, a custom serializer now sets these into a fixed string.
1 parent 6b0f3da commit 64a77e0

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/obelisk/asynchronous/core.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
Field,
2323
ValidationError,
2424
model_validator,
25+
SerializerFunctionWrapHandler,
26+
WrapSerializer,
2527
)
2628
from typing import (
2729
Annotated,
@@ -124,12 +126,18 @@ def check_metric_type(self) -> Self:
124126
return self
125127

126128

129+
def serialize_comma_string(input: Any, handler: SerializerFunctionWrapHandler) -> str | None:
130+
if val := handler(input):
131+
return ",".join(val)
132+
return None
133+
134+
127135
class QueryParams(BaseModel):
128136
dataset: str
129-
groupBy: list[FieldName] | None = None
137+
groupBy: Annotated[list[FieldName] | None, WrapSerializer(serialize_comma_string)] = None
130138
aggregator: Aggregator | None = None
131-
fields: list[FieldName] | None = None
132-
orderBy: list[str] | None = (
139+
fields: Annotated[list[FieldName] | None, WrapSerializer(serialize_comma_string)] = None
140+
orderBy: Annotated[list[str] | None, WrapSerializer(serialize_comma_string)] = (
133141
None # More complex than just FieldName, can be prefixed with - to invert sort
134142
)
135143
dataType: DataType | None = None
@@ -153,7 +161,7 @@ def check_datatype_needed(self) -> Self:
153161
return self
154162

155163
def to_dict(self) -> dict[str, Any]:
156-
return self.model_dump(exclude_none=True, by_alias=True)
164+
return self.model_dump(exclude_none=True, by_alias=True, mode='json')
157165

158166

159167
class ChunkedParams(BaseModel):

src/tests/asynchronous/core_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,18 @@ def test_query_param_serialize():
99
)
1010
dump = q.to_dict()
1111
assert "filter" in dump
12+
13+
14+
def test_comma_separate_string():
15+
data = QueryParams(
16+
dataset="dummy",
17+
groupBy=["dataset", "timestamp"],
18+
fields=["timestamp", "labels", "value"],
19+
orderBy=["timestamp", "value"],
20+
dataType="string",
21+
).to_dict()
22+
23+
assert data["fields"] == "timestamp,labels,value"
24+
assert data["groupBy"] == "dataset,timestamp"
25+
assert data["orderBy"] == "timestamp,value"
26+

0 commit comments

Comments
 (0)