Exemplo n.º 1
0
    def to_type(self, arg_type, resolver):
        """
            Convert a string representation of a type to a type
        """
        if arg_type is None:
            return None

        if not isinstance(arg_type, str):
            raise CompilerException(
                "bad annotation in plugin %s::%s, expected str but got %s (%s)"
                % (self.ns, self.__class__.__function_name__, type(arg_type),
                   arg_type))

        if arg_type == "any":
            return None

        if arg_type == "list":
            return list

        if arg_type == "expression":
            return None

        if arg_type.endswith("[]"):
            basetypename = arg_type[0:-2]
            basetype = resolver.get_type(basetypename)
            return TypedList(basetype)

        return resolver.get_type(arg_type)
Exemplo n.º 2
0
    def __init__(self,
                 entity: "Entity",
                 value_type: "Type",
                 name: str,
                 location: Location,
                 multi: bool = False,
                 nullable: bool = False) -> None:
        Locatable.__init__(self)
        self.location = location
        self.__name: str = name
        entity.add_attribute(self)
        self.__entity = entity
        self.__multi = multi
        self.__nullable = nullable

        self.__type: Type = value_type
        if multi:
            self.__type = TypedList(self.__type)
        if nullable:
            self.__type = NullableType(self.__type)

        self.low: int = 0 if nullable else 1
        # This attribute is only used for the DeprecatedOptionVariable
        self.high: Optional[int] = None
        self.comment = None  # type: Optional[str]
        self.end: Optional[RelationAttribute] = None
Exemplo n.º 3
0
def create_type(base_type: typing.Type[Type],
                multi: bool = False,
                nullable: bool = False) -> Type:
    base: Type = base_type()
    transformations: typing.List[typing.Callable[[Type], Type]] = [
        lambda t: TypedList(t) if multi else t,
        lambda t: NullableType(t) if nullable else t,
    ]
    return reduce(lambda acc, t: t(acc), transformations, base)
Exemplo n.º 4
0
 def get_type(self, namespace: Namespace) -> Type:
     """
     Returns the type for this declaration as a Type.
     """
     tp: Type = self.get_basetype(namespace)
     if self.multi:
         tp = TypedList(tp)
     if self.nullable:
         tp = NullableType(tp)
     return tp
Exemplo n.º 5
0
    def get_new_result_variable(self, instance: "Instance",
                                queue: QueueScheduler) -> ResultVariable:
        if self.__multi:
            mytype = (TypedList(self.__type))
        else:
            mytype = (self.__type)

        if (self.__nullallble):
            # be a 0-1 relation
            self.end = None
            self.low = 0
            self.high = 1
            out = OptionVariable(self, instance, queue)
            mytype = NullableType(mytype)
        else:
            out = ResultVariable()

        out.set_type(mytype)
        out.set_provider(instance)
        return out
Exemplo n.º 6
0
 def apply_multi_if(tp: Type, type_string: str) -> Tuple[Type, str]:
     return (TypedList(tp),
             "%s[]" % type_string) if multi else (tp, type_string)