Github user mxmrlv commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98482667
  
    --- Diff: aria/storage/type.py ---
    @@ -74,16 +125,115 @@ def coerce(cls, key, value):
                 raise exceptions.StorageError('SQL Storage error: 
{0}'.format(str(e)))
     
     
    +class _StrictListMixin(object):
    +    ANY_TYPE = 'any_type'
    +
    +    _item_cls = ANY_TYPE
    +
    +    @classmethod
    +    def coerce(cls, key, value):
    +        "Convert plain dictionaries to MutableDict."
    +        try:
    +            if not isinstance(value, cls):
    +                if isinstance(value, list):
    +                    for item in value:
    +                        cls._assert_item(item)
    +                    return cls(value)
    +                return mutable.MutableList.coerce(key, value)
    +            else:
    +                return value
    +        except ValueError as e:
    +            raise exceptions.StorageError('SQL Storage error: 
{0}'.format(str(e)))
    +
    +    def __setitem__(self, index, value):
    +        """Detect list set events and emit change events."""
    +        self._assert_item(value)
    +        super(_StrictListMixin, self).__setitem__(index, value)
    +
    +    def append(self, item):
    +        self._assert_item(item)
    +        super(_StrictListMixin, self).append(item)
    +
    +    def extend(self, item):
    +        self._assert_item(item)
    +        super(_StrictListMixin, self).extend(item)
    +
    +    def insert(self, index, item):
    +        self._assert_item(item)
    +        super(_StrictListMixin, self).insert(index, item)
    +
    +    @classmethod
    +    def _assert_item(cls, item):
    +        if cls._item_cls is not None and not isinstance(item, 
cls._item_cls):
    +            raise exceptions.StorageError("Key type was set strictly to 
{0}, but was {1}".format(
    +                cls._item_cls, type(item)
    +            ))
    +
    +
     class _MutableList(mutable.MutableList):
     
         @classmethod
         def coerce(cls, key, value):
             "Convert plain dictionaries to MutableDict."
    +
             try:
    -            return mutable.MutableList.coerce(key, value)
    +            if not isinstance(value, cls):
    +                if isinstance(value, list):
    +                    return cls(value)
    +
    +                return mutable.Mutable.coerce(key, value)
    +            else:
    +                return value
    +
             except ValueError as e:
                 raise exceptions.StorageError('SQL Storage error: 
{0}'.format(str(e)))
     
    +StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls')
    +
    +
    +class _StrictDict(object):
    +    _strict_map = {}
    +
    +    def __call__(self, key_cls=None, value_cls=None, *args, **kwargs):
    +        strict_dict_map_key = StrictDictID(key_cls=key_cls, 
value_cls=value_cls)
    +        if strict_dict_map_key not in self._strict_map:
    +            strict_dict_cls = type(
    +                'StrictDict_{0}_{1}'.format(key_cls.__name__, 
value_cls.__name__),
    +                (Dict, ),
    +                {}
    +            )
    +            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
    +
    +        return self._strict_map[strict_dict_map_key]
    +
    +StrictDict = _StrictDict()
    +
    +
    +class _StrictList(object):
    +    _strict_map = {}
    +
    +    def __call__(self, item_cls=None):
    +        if item_cls not in self._strict_map:
    +            strict_list_cls = type(
    +                'StrictList_{0}'.format(item_cls.__name__),
    +                (List, ),
    +                {}
    +            )
    +            type(
    +                'StrictMutableList_{0}'.format(item_cls.__name__),
    +                (_StrictListMixin, _MutableList),
    +                {'_item_cls': item_cls}
    +            ).associate_with(strict_list_cls)
    --- End diff --
    
    integrate with _register_mutable_association_listener


---
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.
---

Reply via email to