def _load(self, body, resource, nested_in=None): """Load this field from a JSON object. :param body: parsed JSON body. :param resource: ResourceBase instance for which the field is loaded. :param nested_in: parent resource path (for error reporting only), must be a list of strings or None. :raises: MissingAttributeError if a required field is missing. :raises: MalformedAttributeError on invalid field value or type. :returns: loaded and verified value """ name = self._path[-1] for path_item in self._path[:-1]: body = body.get(path_item, {}) if name not in body: if self._required: path = (nested_in or []) + self._path raise exceptions.MissingAttributeError( attribute='/'.join(path), resource=resource.path) else: # Do not run the adapter on the default value return self._default try: value = self._adapter(body[name]) except (UnicodeError, ValueError, TypeError) as exc: path = (nested_in or []) + self._path raise exceptions.MalformedAttributeError(attribute='/'.join(path), resource=resource.path, error=exc) return value
def _load(self, body, resource, nested_in=None): """Load this field from a JSON object. :param body: parsed JSON body. :param resource: ResourceBase instance for which the field is loaded. :param nested_in: parent resource path (for error reporting only), must be a list of strings or None. :raises: MissingAttributeError if a required field is missing and not defaulted. :raises: MalformedAttributeError on invalid field value or type. :returns: loaded and verified value """ name = self._path[-1] for path_item in self._path[:-1]: body = body.get(path_item, {}) try: item = self._get_item(body, name) except KeyError: if self._required: path = (nested_in or []) + self._path if self._default is None: raise exceptions.MissingAttributeError( attribute='/'.join(path), resource=resource.path) logging.warning( 'Applying default "%s" on required, but missing ' 'attribute "%s"' % (self._default, path)) # Do not run the adapter on the default value return self._default # NOTE(etingof): this is just to account for schema violation if item is None: return try: return self._adapter(item) except (UnicodeError, ValueError, TypeError) as exc: path = (nested_in or []) + self._path raise exceptions.MalformedAttributeError( attribute='/'.join(path), resource=resource.path, error=exc)
def _load(self, body, resource, nested_in=None): """Load this field from a JSON object. :param body: parsed JSON body. :param resource: ResourceBase instance for which the field is loaded. :param nested_in: parent resource path (for error reporting only), must be a list of strings or None. :raises: MissingAttributeError if a required field is missing. :raises: MalformedAttributeError on invalid field value or type. :returns: loaded and verified value """ name = self._path[-1] for path_item in self._path[:-1]: body = body.get(path_item, {}) try: item = self._get_item(body, name) except KeyError: if self._required: path = (nested_in or []) + self._path raise exceptions.MissingAttributeError( attribute='/'.join(path), resource=resource.path) else: # Do not run the adapter on the default value return self._default try: # Get the value based on the name, defaulting to an empty dict # Check to ensure that value is implemented by OEM # TODO(etingof): we should revisit this logic/code if (item is not None and item != {} and str(item).lower() != 'none'): value = self._adapter(item) else: value = item except (UnicodeError, ValueError, TypeError) as exc: path = (nested_in or []) + self._path raise exceptions.MalformedAttributeError(attribute='/'.join(path), resource=resource.path, error=exc) return value