def get_type_instance(cls, ty): """ Maps a type class from the hub SDK to its corresponding TypeClass in the compiler. """ assert isinstance(ty, OutputBase), ty if isinstance(ty, OutputBoolean): return BooleanType.instance() if isinstance(ty, OutputInt): return IntType.instance() if isinstance(ty, OutputFloat): return FloatType.instance() if isinstance(ty, OutputString): return StringType.instance() if isinstance(ty, OutputAny): return AnyType.instance() if isinstance(ty, OutputObject): return ObjectType({ k: cls.get_type_instance(v) for k, v in ty.properties().items() }) if isinstance(ty, OutputList): return ListType(cls.get_type_instance(ty.elements()), ) if isinstance(ty, OutputNone): return NoneType.instance() if isinstance(ty, OutputRegex): return RegExpType.instance() if isinstance(ty, OutputEnum): return StringType.instance() assert isinstance(ty, OutputMap), f"Unknown Hub Type: {ty!r}" return MapType( cls.get_type_instance(ty.keys()), cls.get_type_instance(ty.values()), )
def map_type(self, tree): """ Resolves a map type expression to a type """ assert tree.data == 'map_type' key_type = self.base_type(tree.child(0)).type() value_type = self.types(tree.child(1)).type() return base_symbol(MapType(key_type, value_type))
def test_type_to_tree_map(magic, patch): tree = magic() mt = MapType(IntType.instance(), BooleanType.instance()) type_to_tree = SymbolExpressionVisitor.type_to_tree patch.object(SymbolExpressionVisitor, 'type_to_tree') se = type_to_tree(tree, mt) assert SymbolExpressionVisitor.type_to_tree.call_args_list == [ call(tree, IntType.instance()), call(tree, BooleanType.instance()) ] assert se == Tree('map_type', [ SymbolExpressionVisitor.type_to_tree(), Tree('types', [SymbolExpressionVisitor.type_to_tree()]) ])
def map(self, tree): assert tree.data == 'map' keys = [] values = [] for i, item in enumerate(tree.children): assert isinstance(item, Tree) key_child = item.child(0) if key_child.data == 'string': new_key = self.string(key_child).type() elif key_child.data == 'number': new_key = self.number(key_child).type() elif key_child.data == 'boolean': new_key = self.boolean(key_child).type() else: assert key_child.data == 'path' new_key = self.path(key_child).type() keys.append(new_key) values.append(self.base_expression(item.child(1)).type()) # check all keys - even if they don't match key_child.expect(new_key.hashable(), 'type_key_not_hashable', key=new_key) key = None value = None for i, p in enumerate(zip(keys, values)): new_key, new_value = p if i >= 1: # type mismatch in the list if key != new_key: key = AnyType.instance() break if value != new_value: value = AnyType.instance() break else: key = new_key value = new_value if not self.with_as: tree.expect(key is not None, 'map_type_no_any') tree.expect(value is not None, 'map_type_no_any') if key is None: key = AnyType.instance() if value is None: value = AnyType.instance() return base_symbol(MapType(key, value))
def get_type_instance(var, obj=None): """ Returns the correctly mapped type class instance of the given type. Params: var: A Symbol from which type could be retrieved. object: In case the Symbol is of type Object, the object that should be wrapped inside the ObjectType instance. """ type_class = TypeMappings.type_class_mapping(var.type()) if type_class == ObjectType: output_type = ObjectType(obj=obj) elif type_class == ListType: output_type = ListType(AnyType.instance()) elif type_class == MapType: output_type = MapType(AnyType.instance(), AnyType.instance()) else: assert type_class in (AnyType, BooleanType, FloatType, IntType, StringType) output_type = type_class.instance() return output_type
# -*- coding: utf-8 -*- from storyscript.compiler.semantics.types.Types import ( MapType, ObjectType, StringType, ) from .Symbols import StorageClass, Symbol, Symbols app_props = { "secrets": Symbol( name="secrets", type_=ObjectType( obj=MapType(StringType.instance(), StringType.instance())), storage_class=StorageClass.read(), desc="Secret variables of this application", ), "hostname": Symbol( name="hostname", type_=StringType.instance(), storage_class=StorageClass.read(), desc="Server hostname of this application", ), "version": Symbol( name="version", type_=StringType.instance(), storage_class=StorageClass.read(),
single_fn = singleton(test_fn) assert single_fn() == 1 assert single_fn() == 1 assert c == 1 @mark.parametrize('type_,expected', [ (BooleanType.instance(), 'boolean'), (IntType.instance(), 'int'), (FloatType.instance(), 'float'), (NoneType.instance(), 'none'), (AnyType.instance(), 'any'), (RegExpType.instance(), 'regexp'), (ListType(AnyType.instance()), 'List[any]'), (MapType(IntType.instance(), StringType.instance()), 'Map[int,string]'), ]) def test_boolean_str(type_, expected): assert str(type_) == expected def test_none_eq(): assert NoneType.instance() == NoneType.instance() assert NoneType.instance() != IntType.instance() assert NoneType.instance() != AnyType.instance() def test_none_assign(): assert not NoneType.instance().can_be_assigned(IntType.instance()) assert not NoneType.instance().can_be_assigned(AnyType.instance())
# -*- coding: utf-8 -*- from storyscript.compiler.semantics.types.Types import MapType, ObjectType, \ StringType from .Symbols import StorageClass, Symbol, Symbols app_props = { 'secrets': Symbol(name='secrets', type_=ObjectType(obj=MapType(StringType.instance(), StringType.instance())), storage_class=StorageClass.read()), 'hostname': Symbol(name='hostname', type_=StringType.instance(), storage_class=StorageClass.read()), 'version': Symbol(name='version', type_=StringType.instance(), storage_class=StorageClass.read()), } app_keyword = Symbol(name='app', type_=ObjectType(obj=app_props), storage_class=StorageClass.read()) class Scope: """ Manages an individual scope """ def __init__(self, parent=None): self._parent = parent self._symbols = Symbols()
(IntType.instance(), OutputInt(data={})), (FloatType.instance(), OutputFloat(data={})), (NoneType.instance(), OutputNone(data={})), (AnyType.instance(), OutputAny(data={})), (RegExpType.instance(), OutputRegex(data={})), (StringType.instance(), OutputEnum(data={})), ( ListType(AnyType.instance()), OutputList(OutputAny.create(), data={}), ), ( ListType(IntType.instance()), OutputList(OutputInt(data={}), data={}), ), ( MapType(IntType.instance(), StringType.instance()), OutputMap(OutputInt(data={}), OutputString(data={}), data={}), ), ( ObjectType({ "i": IntType.instance(), "s": StringType.instance() }), OutputObject({ "i": OutputInt(data={}), "s": OutputString(data={}) }, data={}), ), ( ObjectType({"p": IntType.instance()}),