11""" Reusable implementation of from_etree methods for the most of edm elements """
22
33# pylint: disable=unused-argument, missing-docstring, invalid-name
4+ import copy
45import logging
56
67from pyodata .config import Config
@@ -40,12 +41,23 @@ def struct_type_property_from_etree(entity_type_property_node, config: Config):
4041
4142
4243# pylint: disable=protected-access
43- def struct_type_from_etree (type_node , config : Config , kwargs ):
44+ def struct_type_from_etree (type_node , config : Config , typ , schema = None ):
4445 name = type_node .get ('Name' )
45- label = sap_attribute_get_string (type_node , 'label' )
46- is_value_list = sap_attribute_get_bool (type_node , 'value-list' , False )
46+ base_type = type_node .get ('BaseType' )
4747
48- stype = kwargs ['type' ](name , label , is_value_list )
48+ if base_type is None :
49+ label = sap_attribute_get_string (type_node , 'label' )
50+ is_value_list = sap_attribute_get_bool (type_node , 'value-list' , False )
51+ stype = typ (name , label , is_value_list )
52+ else :
53+ base_type = Types .parse_type_name (base_type )
54+
55+ try :
56+ stype = copy .deepcopy (schema .get_type (base_type ))
57+ except KeyError :
58+ raise PyODataParserError (f'BaseType \' { base_type .name } \' not found in schema' )
59+
60+ stype ._name = name
4961
5062 for proprty in type_node .xpath ('edm:Property' , namespaces = config .namespaces ):
5163 stp = StructTypeProperty .from_etree (proprty , config )
@@ -59,23 +71,19 @@ def struct_type_from_etree(type_node, config: Config, kwargs):
5971 # all properites are loaded because
6072 # there might be links between them.
6173 for ctp in list (stype ._properties .values ()):
62- ctp .struct_type = stype
74+ if ctp .struct_type is None : # TODO: Is it correct
75+ ctp .struct_type = stype
6376
6477 return stype
6578
6679
67- def navigation_type_property_from_etree (node , config : Config ):
68- return NavigationTypeProperty (
69- node .get ('Name' ), node .get ('FromRole' ), node .get ('ToRole' ), Identifier .parse (node .get ('Relationship' )))
70-
71-
72- def complex_type_from_etree (etree , config : Config ):
73- return StructType .from_etree (etree , config , type = ComplexType )
80+ def complex_type_from_etree (etree , config : Config , schema = None ):
81+ return StructType .from_etree (etree , config , typ = ComplexType , schema = schema )
7482
7583
7684# pylint: disable=protected-access
77- def entity_type_from_etree (etree , config : Config ):
78- etype = StructType .from_etree (etree , config , type = EntityType )
85+ def entity_type_from_etree (etree , config : Config , schema = None ):
86+ etype = StructType .from_etree (etree , config , typ = EntityType , schema = schema )
7987
8088 for proprty in etree .xpath ('edm:Key/edm:PropertyRef' , namespaces = config .namespaces ):
8189 etype ._key .append (etype .proprty (proprty .get ('Name' )))
@@ -92,14 +100,18 @@ def entity_type_from_etree(etree, config: Config):
92100
93101
94102# pylint: disable=protected-access, too-many-locals
95- def enum_type_from_etree (type_node , config : Config , kwargs ):
103+ def enum_type_from_etree (type_node , config : Config , namespace ):
96104 ename = type_node .get ('Name' )
97105 is_flags = type_node .get ('IsFlags' )
98106
99- namespace = kwargs ['namespace' ]
107+ # namespace = kwargs['namespace']
100108
101109 underlying_type = type_node .get ('UnderlyingType' )
102110
111+ # https://docs.oasis-open.org/odata/odata-csdl-json/v4.01/csprd04/odata-csdl-json-v4.01-csprd04.html#sec_EnumerationType
112+ if underlying_type is None :
113+ underlying_type = 'Edm.Int32'
114+
103115 valid_types = {
104116 'Edm.Byte' : [0 , 2 ** 8 - 1 ],
105117 'Edm.Int16' : [- 2 ** 15 , 2 ** 15 - 1 ],
@@ -263,8 +275,7 @@ def external_annotation_from_etree(annotations_node, config):
263275 yield annot
264276
265277
266- def annotation_from_etree (target , config , kwargs ):
267- annotation_node = kwargs ['annotation_node' ]
278+ def annotation_from_etree (target , config , annotation_node ):
268279 term = annotation_node .get ('Term' )
269280
270281 if term in config .sap_annotation_value_list :
@@ -274,13 +285,12 @@ def annotation_from_etree(target, config, kwargs):
274285 return None
275286
276287
277- def value_helper_from_etree (target , config , kwargs ):
288+ def value_helper_from_etree (target , config , annotation_node ):
278289 label = None
279290 collection_path = None
280291 search_supported = False
281292 params_node = None
282293
283- annotation_node = kwargs ['annotation_node' ]
284294 for prop_value in annotation_node .xpath ('edm:Record/edm:PropertyValue' , namespaces = config .annotation_namespace ):
285295 rprop = prop_value .get ('Property' )
286296 if rprop == 'Label' :
0 commit comments