Skip to content

Commit

Permalink
Track augmented copies to apply deviation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhuebner committed Jan 23, 2024
1 parent 66583db commit 378c212
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
import org.eclipse.xtext.EcoreUtil2;
Expand Down Expand Up @@ -103,6 +105,7 @@ protected EObject createCopy(EObject eObject) {
// store text information. e.g. to serialize XPath
createCopy.eAdapters().add((CompositeNodeWithSemanticElement) node);
}
eObject.eAdapters().add(new CopiedObjectAdapter(createCopy));
return createCopy;
}
};
Expand All @@ -112,7 +115,7 @@ protected EObject createCopy(EObject eObject) {
}

public static String serializedXpath(XpathExpression reference) {
if(reference == null) {
if (reference == null) {
return null;
}
// TODO use serializer or implement a an own simple one
Expand All @@ -130,4 +133,21 @@ public static String serializedXpath(XpathExpression reference) {
return "leafref";
}

public static class CopiedObjectAdapter extends AdapterImpl {
final EObject copy;

public CopiedObjectAdapter(EObject copy) {
this.copy = copy;
}

public EObject getCopy() {
return copy;
}

public static Stream<CopiedObjectAdapter> findAll(EObject eObject) {
return eObject.eAdapters().stream().filter(a -> a instanceof CopiedObjectAdapter)
.map(a -> ((CopiedObjectAdapter) a));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.typefox.yang.processor.ProcessedDataModel.HasStatements;
import io.typefox.yang.processor.ProcessedDataModel.ListData;
import io.typefox.yang.processor.ProcessedDataModel.ModuleData;
import io.typefox.yang.processor.ProcessorUtility.CopiedObjectAdapter;
import io.typefox.yang.utils.YangNameUtils;
import io.typefox.yang.yang.AbstractModule;
import io.typefox.yang.yang.Action;
Expand Down Expand Up @@ -103,9 +104,9 @@ protected ProcessedDataModel processInternal(List<AbstractModule> modules, List<
augments.add((Augment) ele);
}
}));

deviations.forEach(dev -> processDeviate(dev, processedModel));

augments.forEach(augm -> processAugment(augm, evalCtx));
deviations.forEach(dev -> processDeviate(dev, processedModel));

modules.forEach((module) -> {
String prefix = null;
Expand Down Expand Up @@ -207,12 +208,19 @@ protected void processDeviate(Deviate deviate, ProcessedDataModel processedModel
}
break;
}
Object eGet = targetNode.eContainer().eGet(targetNode.eContainingFeature(), true);
removeFromContainer(targetNode);
DeviationAdapter.add(targetNode, argument);
break;
}
}

private void removeFromContainer(EObject objToRemove) {
if (objToRemove.eContainer() != null) {
Object eGet = objToRemove.eContainer().eGet(objToRemove.eContainingFeature(), true);
if (eGet instanceof EList) {
DeviationAdapter.add(targetNode, argument);
((EList<?>) eGet).remove(targetNode);
((EList<?>) eGet).remove(objToRemove);
}
break;
CopiedObjectAdapter.findAll(objToRemove).forEach(adapter -> removeFromContainer(adapter.getCopy()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class DeviationTest extends AbstractYangTest {
}

@Test
def void testDeviateAugmentedNode() {
def void testDeviateAugmentedNode_01() {

val mainModule = '''
module base-test-module {
Expand Down Expand Up @@ -345,4 +345,5 @@ class DeviationTest extends AbstractYangTest {
+--rw simple-leaf? string
'''.toString, new DataTreeSerializer().serialize(processedData.modules.get(0)).toString)
}

}

0 comments on commit 378c212

Please sign in to comment.