Exemplo n.º 1
0
    def __getitem__(self, dot_path):
        """
        Return the type of the given item.

        :type dot_path: ast.DotPath
        :param dot_path: The path to follow
        :return:
        """
        if self.ctor_properties is None:
            raise exception.BananaTypeCheckerBug(
                "Component type can't have properties")

        if len(dot_path.properties) == 0:
            for arg in self.ctor_properties:
                if arg.param_name == dot_path.varname.inner_val():
                    return arg.param_type
        else:
            for arg in self.ctor_properties:
                if arg.param_name == dot_path.varname.inner_val():
                    if isinstance(arg.param_type, Object):
                        return arg.param_type[dot_path.next_dot_path()]
                    else:
                        raise exception.BananaPropertyDoesNotExists(
                            dot_path.next_dot_path(), arg.param_type)

        raise exception.BananaPropertyDoesNotExists(dot_path, on_type=self)
Exemplo n.º 2
0
    def __getitem__(self, key):
        # a.b or a."b"
        if isinstance(key, ast.Ident) or isinstance(key, ast.StringLit):
            if key.inner_val() not in self.props:
                raise exception.BananaPropertyDoesNotExists(key, on_type=self)
            return self.props[key.inner_val()]

        # a.b.c
        if isinstance(key, ast.DotPath):
            if key.varname.inner_val() not in self.props:
                raise exception.BananaPropertyDoesNotExists(key.varname,
                                                            on_type=self)
            sub_object = self.props[key.varname.inner_val()]
            if len(key.properties) == 0:
                return sub_object
            # Recurse
            if isinstance(sub_object, Object):
                return sub_object[key.next_dot_path()]
            if isinstance(sub_object, Any):
                return sub_object

            raise exception.BananaPropertyDoesNotExists(key.next_dot_path(),
                                                        on_type=sub_object)

        raise exception.BananaTypeCheckerBug(
            "Unreachable code in Object.__getitem__ reached.")
Exemplo n.º 3
0
    def __setitem__(self, dot_path, value):
        """
        Attempt to set the value at 'dot_path' to 'value'.

        :type dot_path: ast.DotPath
        :param dot_path: The path of the property
        :type value: String | Enum | Object | Number
        :param value: The new type to set.
        """
        if self.ctor_properties is None:
            raise exception.BananaTypeCheckerBug(
                "Component type can't have properties")

        if len(dot_path.properties) == 0:
            for arg in self.ctor_properties:
                if arg.param_name == dot_path.varname.inner_val():
                    if not can_be_cast_to(value, arg.param_type):
                        raise exception.BananaArgumentTypeError(
                            expected_type=arg.param_type,
                            received_type=value,
                            where=dot_path.span)
                    else:
                        return
        else:
            for arg in self.ctor_properties:
                if arg.param_name == dot_path.varname.inner_val():
                    if isinstance(arg.param_type, Any):
                        return
                    elif isinstance(arg.param_type, Object):
                        next_dot_path = dot_path.next_dot_path()
                        sub_arg_type = arg.param_type[next_dot_path]
                        if not can_be_cast_to(value, sub_arg_type):
                            raise exception.BananaArgumentTypeError(
                                expected_type=sub_arg_type,
                                received_type=value,
                                where=next_dot_path.span)
                        else:
                            return
                    else:
                        raise exception.BananaPropertyDoesNotExists(
                            dot_path.next_dot_path(), arg.param_type)

        raise exception.BananaPropertyDoesNotExists(dot_path, on_type=self)