-
Notifications
You must be signed in to change notification settings - Fork 12
feat: introduce default values #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -682,6 +682,32 @@ def get_category_data(db): | |||||
| return globals()[f"{db.module_name}_get_category_data"] | ||||||
|
|
||||||
|
|
||||||
| # ------------------------------------------------ | ||||||
| # data_templates helpers | ||||||
|
|
||||||
|
|
||||||
| def apply_data_defaults(map_data: dict, point: dict) -> dict: | ||||||
| """Merge data_templates defaults into a point. Point fields always win. | ||||||
|
|
||||||
| data_templates structure: | ||||||
| "data_templates": { | ||||||
| "type_of_place": { | ||||||
| "big bridge": {"accessible_by": ["pedestrians", "cars"]}, | ||||||
| "small bridge": {"accessible_by": ["pedestrians"]} | ||||||
| } | ||||||
| } | ||||||
| """ | ||||||
| type_defaults: dict = {} | ||||||
| for field, variants in map_data.get("data_templates", {}).items(): | ||||||
| value = point.get(field) | ||||||
| if value is not None and value in variants: | ||||||
| type_defaults.update(variants[value]) | ||||||
|
|
||||||
| if not type_defaults: | ||||||
| return point | ||||||
| return {**type_defaults, **point} | ||||||
|
|
||||||
|
|
||||||
| # ------------------------------------------------ | ||||||
| # get_location | ||||||
|
|
||||||
|
|
@@ -697,8 +723,10 @@ def get_location_from_raw_data(raw_data, uuid, location_model): | |||||
| Returns: | ||||||
| Validated location model instance, or None if not found. | ||||||
| """ | ||||||
| point = next((point for point in raw_data["data"] if point["uuid"] == uuid), None) | ||||||
| return location_model.model_validate(point) if point else None | ||||||
| point = next((p for p in raw_data["data"] if p["uuid"] == uuid), None) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid hard failure when one row is missing Using Proposed fix- point = next((p for p in raw_data["data"] if p["uuid"] == uuid), None)
+ point = next((p for p in raw_data["data"] if p.get("uuid") == uuid), None)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| if point is None: | ||||||
| return None | ||||||
| return location_model.model_validate(apply_data_defaults(raw_data, point)) | ||||||
|
|
||||||
|
|
||||||
| def google_json_db_get_location(self, uuid, location_model): | ||||||
|
|
@@ -744,7 +772,8 @@ def get_locations_list_from_raw_data(map_data, query, location_model): | |||||
| Returns: | ||||||
| List of validated location model instances. | ||||||
| """ | ||||||
| filtered_locations = get_queried_data(map_data["data"], map_data["categories"], query) | ||||||
| merged_data = [apply_data_defaults(map_data, p) for p in map_data["data"]] | ||||||
| filtered_locations = get_queried_data(merged_data, map_data["categories"], query) | ||||||
| return [location_model.model_validate(point) for point in filtered_locations] | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard template matching against malformed template/value shapes.
value in variants/variants[value]can raise at runtime (e.g., unhashable discriminator values or non-dict template payloads), which would fail the whole read path.Proposed fix
🤖 Prompt for AI Agents