66create_node = nodes .ResourceNode .create_node
77
88
9- def create_reduce_children (ctx ):
9+ def create_reduce_children (ctx , exclude_primitive_extensions ):
1010 model = ctx ["model" ]
1111
1212 def func (acc , res ):
@@ -21,35 +21,61 @@ def func(acc, res):
2121 value = data [prop ]
2222 childPath = ""
2323
24+ # extensions shouldn't filter through here, yet they should for descendants?
25+ # unless this item is the node that is being processed (primitive extension)
26+ # though if you filter it, descendants will not work too
27+ if prop .startswith ("_" ) and exclude_primitive_extensions :
28+ continue
29+
2430 if res .path is not None :
2531 childPath = res .path + "." + prop
2632
33+ fullPath = f"{ res .propName } .{ prop } " if res .propName else childPath # The full path to the node (weill evenutally be) e.g. Patient.name[0].given
34+ fullPath = fullPath .replace ("_" , "" )
35+
36+ if prop == "extension" :
37+ childPath = "Extension"
38+
2739 if (
2840 isinstance (model , dict )
2941 and "pathsDefinedElsewhere" in model
3042 and childPath in model ["pathsDefinedElsewhere" ]
3143 ):
3244 childPath = model ["pathsDefinedElsewhere" ][childPath ]
3345
46+ childPath = (
47+ model ["path2Type" ].get (childPath , childPath )
48+ if isinstance (model , dict ) and "path2Type" in model
49+ else childPath
50+ )
51+
52+ # If the prop tolower ends with the type tolower
53+ if prop .lower ().endswith (childPath .lower ()) and len (prop ) > len (childPath ):
54+ # Check if the path is actually in the choice types
55+ altPropName = res .path + "." + prop [:- len (childPath )]
56+ actualTypes = model ["choiceTypePaths" ].get (altPropName , [])
57+ if len (actualTypes ) > 0 :
58+ # If it is, we can use it
59+ fullPath = f"{ res .propName } .{ prop [:- len (childPath )]} "
60+
3461 if isinstance (value , list ):
35- mapped = [create_node (n , childPath ) for n in value ]
62+ mapped = [create_node (n , childPath , propName = f" { fullPath } [ { i } ]" , index = i ) for i , n in enumerate ( value ) ]
3663 acc = acc + mapped
3764 else :
38- acc .append (create_node (value , childPath ))
65+ acc .append (create_node (value , childPath , propName = fullPath ))
3966 return acc
4067
4168 return func
4269
4370
4471def children (ctx , coll ):
45- return reduce (create_reduce_children (ctx ), coll , [])
72+ return reduce (create_reduce_children (ctx , True ), coll , [])
4673
4774
4875def descendants (ctx , coll ):
4976 res = []
50- ch = children ( ctx , coll )
77+ ch = reduce ( create_reduce_children ( ctx , False ), coll , [] )
5178 while len (ch ) > 0 :
5279 res = res + ch
53- ch = children (ctx , ch )
54-
80+ ch = reduce (create_reduce_children (ctx , False ), ch , [])
5581 return res
0 commit comments