Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98646660 --- Diff: aria/storage/type.py --- @@ -175,72 +172,105 @@ class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." - try: - if not isinstance(value, cls): - if isinstance(value, list): - return cls(value) - - return mutable.Mutable.coerce(key, value) - else: - return value - + return mutable.MutableList.coerce(key, value) except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') +StrictDictID = namedtuple('StrictDictID', 'key_cls, value_cls') +StrictValue = namedtuple('StrictValue', 'type_cls, listener_cls') class _StrictDict(object): + """ + This entire class functions as a factory for strict dicts and their listeners. + No type class, and no listener type class is created (and associated) more than once. If a + relevant type class exists it is returned. + """ _strict_map = {} - def __call__(self, key_cls=None, value_cls=None, *args, **kwargs): + def __call__(self, key_cls=None, value_cls=None): strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) if strict_dict_map_key not in self._strict_map: + # Creating the type class itself. this class would be returned (used by the sqlalchemy + # Column). strict_dict_cls = type( 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), (Dict, ), {} ) - type( + # Creating the type listening class and associating it with the newly created type. + # The new class inherits from both the _MutableList class and the _StrictListMixin, + # while setting the necessary _key_cls and _value_cls as class attributes. + listener_cls = type( 'StrictMutableDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), (_StrictDictMixin, _MutableDict), {'_key_cls': key_cls, '_value_cls': value_cls} - ).associate_with(strict_dict_cls) - self._strict_map[strict_dict_map_key] = strict_dict_cls + ) + listener_cls.associate_with(strict_dict_cls) + self._strict_map[strict_dict_map_key] = StrictValue(type_cls=strict_dict_cls, + listener_cls=listener_cls) - return self._strict_map[strict_dict_map_key] + return self._strict_map[strict_dict_map_key].type_cls StrictDict = _StrictDict() class _StrictList(object): + """ + This entire class functions as a factory for strict dicts and their listeners. + No type class, and no listener type class is created (and associated) more than once. If a + relevant type class exists it is returned. + """ _strict_map = {} def __call__(self, item_cls=None): + if item_cls not in self._strict_map: + # Creating the type class itself. this class would be returned (used by the sqlalchemy + # Column). strict_list_cls = type( 'StrictList_{0}'.format(item_cls.__name__), (List, ), {} ) - type( + # Creating the type listening class and associating it with the newly created type. + # The new class inherits from both the _MutableList class and the _StrictListMixin, + # while setting the necessary _item_cls as class attribute. + listener_cls = type( 'StrictMutableList_{0}'.format(item_cls.__name__), (_StrictListMixin, _MutableList), {'_item_cls': item_cls} - ).associate_with(strict_list_cls) - self._strict_map[item_cls] = strict_list_cls + ) + self._strict_map[item_cls] = StrictValue(type_cls=strict_list_cls, + listener_cls=listener_cls) - return self._strict_map[item_cls] + return self._strict_map[item_cls].type_cls StrictList = _StrictList() + def _mutable_association_listener(mapper, cls): + strict_dict_type_to_listener = \ + dict((v.type_cls, v.listener_cls) for v in _StrictDict._strict_map.values()) + + strict_list_type_to_listener = \ + dict((v.type_cls, v.listener_cls) for v in _StrictList._strict_map.values()) + for prop in mapper.column_attrs: column_type = prop.columns[0].type - if isinstance(column_type, Dict): + # Dict Listeners + if any(isinstance(column_type, v) for v in strict_dict_type_to_listener): --- End diff -- well, not really, it's some sqlalcehmy magic, it isn't really an instance of the class (so if was doing something like isintance it would return True, but it doesn't hash the same way [for dicts])
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---