Skip to content

Commit

Permalink
SourceTreeCalc: handle owner class nested in property tree (#2728)
Browse files Browse the repository at this point in the history
  • Loading branch information
aziemchawdhary-gs authored Mar 27, 2024
1 parent 8c35156 commit ea6829a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,15 @@ function <<access.private>> meta::pure::graphFetch::enrichSourceTreeNodeForPrope

let inlinedPropertyTree = $propertyPaths.result->buildPropertyTree()->inlineQualifiedPropertyNodes();

let inlinedGraphTree = $inlinedPropertyTree->propertyTreeToGraphFetchTree($owner)->removeDummyProperties();
// Property tree root node may not start at owner (e.g. when the source of a mapping is wrapped in a new class and passed to a property mapping).
// Here we try and find the part of the tree that starts at the owner - in usual case we will return the same tree but now also handle case mentioned above.
// Due to subTypes we could end up returning multiple trees hence we check if there is only one and start there, otherwise continue with old behaviour.
let treeStartingAtOwner = findSubTreeWithOwner($inlinedPropertyTree, $owner);

let inlinedGraphTree = if($treeStartingAtOwner->size() == 1,
| $treeStartingAtOwner->toOne()->propertyTreeToGraphFetchTree($owner)->removeDummyProperties(),
| $inlinedPropertyTree->propertyTreeToGraphFetchTree($owner)->removeDummyProperties()
);

// copy common properties from base type tree to subtype trees at property level
// TODO - remove when propertySubTypes are embedded in propertyTrees
Expand Down Expand Up @@ -639,6 +647,21 @@ function <<access.private>> meta::pure::graphFetch::addPassThroughSubTreesAtPath
);
}

function meta::pure::graphFetch::findSubTreeWithOwner(pTree:PropertyPathTree[1], ownerClass:Class<Any>[1]) : PropertyPathTree[*]
{
$pTree.value->match([
node:PropertyPathNode[1] | if($ownerClass == $node.class || $node.class->isStrictSubType($ownerClass),
| $pTree,
| $pTree.children->map(ch | findSubTreeWithOwner($ch, $ownerClass))
),
clz:Class<Any>[1] | if($clz == $ownerClass || $ownerClass->isStrictSubType($clz),
| $pTree,
| $pTree.children->map(ch | findSubTreeWithOwner($ch, $ownerClass))
),
any:Any[1] | $pTree.children->map(ch | findSubTreeWithOwner($ch, $ownerClass))
]);
}

function meta::pure::graphFetch::propertyTreeToGraphFetchTree(pTree:PropertyPathTree[1], ownerClass:Class<Any>[1]): RootGraphFetchTree<Any>[1]
{
let root = ^RootGraphFetchTree<Any>(class = $ownerClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2069,3 +2069,62 @@ Mapping meta::pure::graphFetch::tests::sourceTreeCalc::withFlatteningInTransform
}
)

###Pure
import meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::*;

Class meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::Source
{
id : Integer[1];
}

Class meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::B
{
b : Integer[1];
}

Class meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::Target
{
b : meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::B[1];
}

Class meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::SourceWrapper
{
s : meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::Source[1];
}



function <<test.Test>> meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::testSourceTreeCalc():Boolean[1]
{
let tree = #{
Target {b {b}}
}#;

let expectedString = 'Source\n' +
'(\n' +
' id\n'+
')';

let sourceTree = meta::pure::graphFetch::calculateSourceTree($tree,
meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::M,
meta::pure::extension::defaultExtensions());

assertEquals($expectedString, $sourceTree->meta::pure::graphFetch::sortTree()->meta::pure::graphFetch::treeToString());
}

###Mapping
Mapping meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::M
(
*meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::Target : Pure
{
~src meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::Source
b[prop_b] : ^meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::SourceWrapper(s = $src)
}
meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::B[prop_b] : Pure
{
~src meta::pure::graphFetch::tests::sourceTreeCalc::ownerInPropertySubTree::SourceWrapper
b : $src.s.id
}
)


0 comments on commit ea6829a

Please sign in to comment.