@@ -176,15 +176,14 @@ def parse(value):
176
176
177
177
178
178
class Types :
179
- """Repository of all available OData types
179
+ """Repository of all available OData V2 primitive types + their Collection variants
180
180
181
181
Since each type has instance of appropriate type, this
182
182
repository acts as central storage for all instances. The
183
183
rule is: don't create any type instances if not necessary,
184
184
always reuse existing instances if possible
185
185
"""
186
186
187
- # dictionary of all registered types (primitive, complex and collection variants)
188
187
Types = None
189
188
190
189
@staticmethod
@@ -794,6 +793,10 @@ def __init__(self, namespace):
794
793
self .associations = dict ()
795
794
self .association_sets = dict ()
796
795
796
+ # generated collections for ease of lookup (e.g. function import return type)
797
+ self ._collections_entity_types = dict ()
798
+ self ._collections_complex_types = dict ()
799
+
797
800
def list_entity_types (self ):
798
801
return list (self .entity_types .values ())
799
802
@@ -823,9 +826,9 @@ def add_entity_type(self, etype):
823
826
# automatically create and register collection variant if not exists
824
827
if isinstance (etype , NullType ):
825
828
return
826
-
827
829
collection_type_name = f'Collection({ etype .name } )'
828
- self .entity_types [collection_type_name ] = Collection (etype .name , etype )
830
+ self ._collections_entity_types [collection_type_name ] = Collection (etype .name , etype )
831
+ # TODO performance memory: this is generating collection for every entity type encoutered, regardless of such collection is really used.
829
832
830
833
def add_complex_type (self , ctype ):
831
834
"""Add new complex type to the type repository as well as its collection variant"""
@@ -835,9 +838,9 @@ def add_complex_type(self, ctype):
835
838
# automatically create and register collection variant if not exists
836
839
if isinstance (ctype , NullType ):
837
840
return
838
-
839
841
collection_type_name = f'Collection({ ctype .name } )'
840
- self .complex_types [collection_type_name ] = Collection (ctype .name , ctype )
842
+ self ._collections_complex_types [collection_type_name ] = Collection (ctype .name , ctype )
843
+ # TODO performance memory: this is generating collection for every entity type encoutered, regardless of such collection is really used.
841
844
842
845
def add_enum_type (self , etype ):
843
846
"""Add new enum type to the type repository"""
@@ -881,7 +884,7 @@ def typ(self, type_name, namespace=None):
881
884
"""Returns either EntityType, ComplexType or EnumType that matches the name.
882
885
"""
883
886
884
- for type_space in (self .entity_type , self .complex_type , self .enum_type ):
887
+ for type_space in (self .entity_type , self ._collections_entity_types , self . complex_type , self . _collections_complex_types , self .enum_type ):
885
888
try :
886
889
return type_space (type_name , namespace = namespace )
887
890
except KeyError :
@@ -905,6 +908,21 @@ def entity_type(self, type_name, namespace=None):
905
908
906
909
raise KeyError (f'EntityType { type_name } does not exist in any Schema Namespace' )
907
910
911
+ def _collections_entity_types (self , type_name , namespace = None ):
912
+ if namespace is not None :
913
+ try :
914
+ return self ._decls [namespace ]._collections_entity_types [type_name ]
915
+ except KeyError :
916
+ raise KeyError (f'EntityType collection { type_name } does not exist in Schema Namespace { namespace } ' )
917
+
918
+ for decl in list (self ._decls .values ()):
919
+ try :
920
+ return decl ._collections_entity_types [type_name ]
921
+ except KeyError :
922
+ pass
923
+
924
+ raise KeyError (f'EntityType collection { type_name } does not exist in any Schema Namespace' )
925
+
908
926
def complex_type (self , type_name , namespace = None ):
909
927
if namespace is not None :
910
928
try :
@@ -920,6 +938,21 @@ def complex_type(self, type_name, namespace=None):
920
938
921
939
raise KeyError (f'ComplexType { type_name } does not exist in any Schema Namespace' )
922
940
941
+ def _collections_complex_types (self , type_name , namespace = None ):
942
+ if namespace is not None :
943
+ try :
944
+ return self ._decls [namespace ]._collections_complex_types [type_name ]
945
+ except KeyError :
946
+ raise KeyError (f'ComplexType collection { type_name } does not exist in Schema Namespace { namespace } ' )
947
+
948
+ for decl in list (self ._decls .values ()):
949
+ try :
950
+ return decl ._collections_complex_types [type_name ]
951
+ except KeyError :
952
+ pass
953
+
954
+ raise KeyError (f'ComplexType collection { type_name } does not exist in any Schema Namespace' )
955
+
923
956
def enum_type (self , type_name , namespace = None ):
924
957
if namespace is not None :
925
958
try :
@@ -946,18 +979,28 @@ def get_type(self, type_info):
946
979
except KeyError :
947
980
pass
948
981
949
- # then look for type in entity types
982
+ # then look for type in entity types and collections of entity types
950
983
try :
951
984
return self .entity_type (search_name , type_info .namespace )
952
985
except KeyError :
953
986
pass
954
987
955
- # then look for type in complex types
988
+ try :
989
+ return self ._collections_entity_types (search_name , type_info .namespace )
990
+ except KeyError :
991
+ pass
992
+
993
+ # then look for type in complex types and collections of complex types
956
994
try :
957
995
return self .complex_type (search_name , type_info .namespace )
958
996
except KeyError :
959
997
pass
960
998
999
+ try :
1000
+ return self ._collections_complex_types (search_name , type_info .namespace )
1001
+ except KeyError :
1002
+ pass
1003
+
961
1004
# then look for type in enum types
962
1005
try :
963
1006
return self .enum_type (search_name , type_info .namespace )
0 commit comments