Skip to content

Commit e3db67c

Browse files
author
patrick
committed
add jmesh query
1 parent c3df847 commit e3db67c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/qpystructs/operations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ def parse_file_as(file_path: Path | str, to_type: T, data_format=DataFormatType.
112112

113113

114114
def get_value(target_object: str | dict | BaseModel | GenericDataModel, path_exp: str,
115-
) -> Any:
115+
**kwargs) -> Any:
116116
if isinstance(target_object, Dict):
117117
return jmespath.search(expression=path_exp, data=target_object)
118118
if isinstance(target_object, str):
119119
return jmespath.search(data=json.loads(target_object), expression=path_exp)
120120
if isinstance(target_object, BaseModel):
121121
return jmespath.search(data=target_object.dict(), expression=path_exp)
122+
122123
raise NotImplementedError("not support type " + type(target_object))
123124

124125

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"locations": [
3+
{"name": "Seattle", "state": "WA"},
4+
{"name": "New York", "state": "NY"},
5+
{"name": "Bellevue", "state": "WA"},
6+
{"name": "Olympia", "state": "WA"}
7+
]
8+
}

tests/test_operations.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ def test_differ():
2727
result = qpystructs.differ(more_dict, more_json_dict)
2828
print(result)
2929
assert len(result) > 1
30+
31+
32+
def test_query_expression():
33+
query = "locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}"
34+
result = qpystructs.load_from_file("./structured-data/query-data.json")
35+
query_result = qpystructs.get_value(result, query)
36+
assert query_result == {
37+
"WashingtonCities": "Bellevue, Olympia, Seattle"
38+
}

0 commit comments

Comments
 (0)