-
Notifications
You must be signed in to change notification settings - Fork 87
fix: Always load object values when converting MappedObjects to dict #400
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2171f77
18ef93e
6a01614
89c3dfd
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import time | ||
| from datetime import datetime, timedelta | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from linode_api4.objects.serializable import JSONObject | ||
|
|
||
|
|
@@ -99,6 +100,18 @@ def _expand_vals(self, target, **vals): | |
| def __repr__(self): | ||
| return "Mapping containing {}".format(vars(self).keys()) | ||
|
|
||
| @staticmethod | ||
| def _flatten_base_subclass(obj: "Base") -> Optional[Dict[str, Any]]: | ||
| if obj is None: | ||
| return None | ||
|
|
||
| # If the object hasn't already been lazy-loaded, | ||
| # manually refresh it | ||
| if not getattr(obj, "_populated", False): | ||
| obj._api_get() | ||
|
|
||
| return obj._raw_json | ||
|
|
||
| @property | ||
| def dict(self): | ||
| result = vars(self).copy() | ||
|
|
@@ -112,12 +125,17 @@ def dict(self): | |
| ( | ||
| item.dict | ||
| if isinstance(item, cls) | ||
| else item._raw_json if isinstance(item, Base) else item | ||
| else ( | ||
| self._flatten_base_subclass(item) | ||
| if isinstance(item, Base) | ||
| else item | ||
| ) | ||
| ) | ||
| for item in v | ||
| ] | ||
| elif isinstance(v, Base): | ||
| result[k] = v._raw_json | ||
| result[k] = self._flatten_base_subclass(v) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
|
|
@@ -140,7 +158,10 @@ def __init__(self, client: object, id: object, json: object = {}) -> object: | |
| #: be updated on access. | ||
| self._set("_raw_json", None) | ||
|
|
||
| for k in type(self).properties: | ||
| for k, v in type(self).properties.items(): | ||
| if v.identifier: | ||
| continue | ||
|
Comment on lines
+162
to
+163
Contributor
Author
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. We need to make sure we don't override identifier attributes |
||
|
|
||
| self._set(k, None) | ||
|
|
||
| self._set("id", id) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,10 +12,10 @@ class DerivedBase(Base): | |
| parent_id_name = "parent_id" # override in child classes | ||
|
|
||
| def __init__(self, client, id, parent_id, json={}): | ||
| Base.__init__(self, client, id, json=json) | ||
|
|
||
| self._set(type(self).parent_id_name, parent_id) | ||
|
Contributor
Author
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. This needed to be repositioned so the |
||
|
|
||
| Base.__init__(self, client, id, json=json) | ||
|
|
||
| @classmethod | ||
| def _api_get_derived(cls, parent, client): | ||
| base_url = "{}/{}".format( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,10 +31,10 @@ class ObjectStorageBucket(DerivedBase): | |
| id_attribute = "label" | ||
|
|
||
| properties = { | ||
| "cluster": Property(), | ||
| "cluster": Property(identifier=True), | ||
| "created": Property(is_datetime=True), | ||
| "hostname": Property(), | ||
| "label": Property(), | ||
| "label": Property(identifier=True), | ||
|
Comment on lines
+34
to
+37
Contributor
Author
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. These weren't flagged as IDs so they were always overridden after the above change. This should fix that up 馃檪 |
||
| "objects": Property(), | ||
| "size": Property(), | ||
| } | ||
|
|
||
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.
Just curious, in which cases that a object isn't lazy-loaded?
Uh oh!
There was an error while loading. Please reload this page.
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.
The most notable example would be the underlying Disk/Volume objects for an Instance config's
devicemapping. They're not loaded when their objects are created here to save on unnecessary API requests and are explicitly lazy-loaded when one of their attributes is accessed.Since we access _raw_json directly, we need to forcibly load them since they won't be automatically lazy-loaded 馃檪