use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-sdlc by finos.
the class PureDomainDeserializer method deserialize.
@Override
public Entity deserialize(String content) throws IOException {
PureModelContextData pureModelContextData;
try {
pureModelContextData = this.pureParser.parseModel(content);
} catch (EngineException e) {
throw new RuntimeException(EngineException.buildPrettyErrorMessage(e.getMessage(), e.getSourceInformation(), e.getErrorType()), e);
}
List<PackageableElement> elements = pureModelContextData.getElements();
if ((elements.size() != 2) || Iterate.noneSatisfy(elements, e -> e instanceof SectionIndex)) {
throw new RuntimeException("Unexpected parsing result (element count: " + elements.size() + ", SectionIndex present: " + Iterate.anySatisfy(elements, e -> e instanceof SectionIndex) + ")");
}
PackageableElement element = elements.get((elements.get(0) instanceof SectionIndex) ? 1 : 0);
String classifierPath = getClassifierPath(element);
String intermediateJson = this.jsonMapper.writeValueAsString(element);
Map<String, Object> entityContent = this.jsonMapper.readValue(intermediateJson, this.jsonMapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class));
return Entity.newEntity(element.getPath(), classifierPath, entityContent);
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-engine by finos.
the class TestSectionRoundtrip method testGrammarRoundtripWithoutSectionIndex.
@Test
public void testGrammarRoundtripWithoutSectionIndex() {
ObjectMapper objectMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();
// NOTE: stress test to account for flakiness
for (int i = 0; i < 500; i++) {
StringBuilder code = new StringBuilder();
for (int t = 200; t > 0; t--) {
code.append("Class model::class").append(t).append("\n").append("{\n").append(" prop: String[1];\n").append("}\n");
if (t != 1) {
code.append("\n");
}
}
PureModelContextData modelData = null;
try {
modelData = PureGrammarParser.newInstance().parseModel(code.toString());
String json = objectMapper.writeValueAsString(modelData);
modelData = objectMapper.readValue(json, PureModelContextData.class);
List<PackageableElement> elements = ListIterate.select(modelData.getElements(), element -> !(element instanceof SectionIndex));
modelData = PureModelContextData.newPureModelContextData(modelData.getSerializer(), modelData.getOrigin(), elements);
} catch (IOException e) {
throw new RuntimeException(e);
}
PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
Assert.assertEquals(code.toString(), grammarTransformer.renderPureModelContextData(modelData));
}
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-sdlc by finos.
the class PureEntitySerializer method deserializeToElement.
private PackageableElement deserializeToElement(String content) {
PureModelContextData pureModelContextData = this.pureParser.parseModel(content);
List<PackageableElement> elements = pureModelContextData.getElements();
switch(elements.size()) {
case 0:
{
throw new RuntimeException("No element found");
}
case 1:
{
if (elements.get(0) instanceof SectionIndex) {
throw new RuntimeException("No element found");
}
return elements.get(0);
}
case 2:
{
if (elements.get(0) instanceof SectionIndex) {
validateSectionIndex((SectionIndex) elements.get(0));
if (elements.get(1) instanceof SectionIndex) {
throw new RuntimeException("No element found");
}
return elements.get(1);
}
if (elements.get(1) instanceof SectionIndex) {
validateSectionIndex((SectionIndex) elements.get(1));
return elements.get(0);
}
throw new RuntimeException("Expected one element, found 2");
}
default:
{
int sectionIndexCount = Iterate.count(elements, e -> e instanceof SectionIndex);
String message = (sectionIndexCount > 1) ? ("Expected at most one SectionIndex, found " + sectionIndexCount) : ("Expected one element, found " + (elements.size() - sectionIndexCount));
throw new RuntimeException(message);
}
}
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-engine by finos.
the class TestRelationalMappingGrammarParser method testLocalMappingPropertyParsing.
@Test
public void testLocalMappingPropertyParsing() throws Exception {
String val = "###Mapping\n" + "Mapping mappingPackage::myMapping\n" + "(\n" + " Person: Relational\n" + " {\n" + " firstName : [db]personTable.firstName,\n" + " +localProp : String[1] : [db]personTable.firstName\n" + " }\n" + ")\n";
PureModelContextData data = PureGrammarParser.newInstance().parseModel(val);
String expected = "{\"_type\":\"data\",\"serializer\":null,\"origin\":null,\"elements\":[{\"_type\":\"mapping\",\"name\":\"myMapping\",\"sourceInformation\":{\"sourceId\":\"\",\"startLine\":2,\"startColumn\":1,\"endLine\":9,\"endColumn\":1},\"classMappings\":[{\"_type\":\"relational\",\"id\":null,\"mappingClass\":null,\"extendsClassMappingId\":null,\"root\":false,\"sourceInformation\":{\"sourceId\":\"\",\"startLine\":4,\"startColumn\":5,\"endLine\":8,\"endColumn\":5},\"classSourceInformation\":{\"sourceId\":\"\",\"startLine\":4,\"startColumn\":5,\"endLine\":4,\"endColumn\":10},\"primaryKey\":[],\"propertyMappings\":[{\"_type\":\"relationalPropertyMapping\",\"property\":{\"property\":\"firstName\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":6,\"startColumn\":9,\"endLine\":6,\"endColumn\":17},\"class\":\"Person\"},\"source\":null,\"target\":null,\"localMappingProperty\":null,\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":6,\"startColumn\":19,\"endLine\":6,\"endColumn\":45},\"enumMappingId\":null,\"relationalOperation\":{\"_type\":\"column\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":6,\"startColumn\":21,\"endLine\":6,\"endColumn\":45},\"table\":{\"_type\":\"Table\",\"table\":\"personTable\",\"schema\":\"default\",\"database\":\"db\",\"mainTableDb\":\"db\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":6,\"startColumn\":25,\"endLine\":6,\"endColumn\":35}},\"tableAlias\":\"personTable\",\"column\":\"firstName\"},\"bindingTransformer\":null},{\"_type\":\"relationalPropertyMapping\",\"property\":{\"property\":\"localProp\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":10,\"endLine\":7,\"endColumn\":18},\"class\":null},\"source\":null,\"target\":null,\"localMappingProperty\":{\"type\":\"String\",\"multiplicity\":{\"lowerBound\":1,\"upperBound\":1},\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":20,\"endLine\":7,\"endColumn\":30}},\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":32,\"endLine\":7,\"endColumn\":58},\"enumMappingId\":null,\"relationalOperation\":{\"_type\":\"column\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":34,\"endLine\":7,\"endColumn\":58},\"table\":{\"_type\":\"Table\",\"table\":\"personTable\",\"schema\":\"default\",\"database\":\"db\",\"mainTableDb\":\"db\",\"sourceInformation\":{\"sourceId\":\"mappingPackage::myMapping\",\"startLine\":7,\"startColumn\":38,\"endLine\":7,\"endColumn\":48}},\"tableAlias\":\"personTable\",\"column\":\"firstName\"},\"bindingTransformer\":null}],\"mainTable\":null,\"distinct\":false,\"groupBy\":[],\"filter\":null,\"class\":\"Person\"}],\"includedMappings\":[],\"associationMappings\":[],\"enumerationMappings\":[],\"tests\":[],\"package\":\"mappingPackage\"},{\"_type\":\"sectionIndex\",\"name\":\"SectionIndex\",\"sourceInformation\":null,\"sections\":[{\"_type\":\"importAware\",\"parserName\":\"Pure\",\"elements\":[],\"sourceInformation\":{\"sourceId\":\"\",\"startLine\":1,\"startColumn\":1,\"endLine\":1,\"endColumn\":8},\"imports\":[]},{\"_type\":\"importAware\",\"parserName\":\"Mapping\",\"elements\":[\"mappingPackage::myMapping\"],\"sourceInformation\":{\"sourceId\":\"\",\"startLine\":2,\"startColumn\":8,\"endLine\":11,\"endColumn\":2},\"imports\":[]}],\"package\":\"__internal__\"}]}";
Assert.assertEquals(expected, PureProtocolObjectMapperFactory.getNewObjectMapper().writeValueAsString(data));
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.section.SectionIndex in project legend-engine by finos.
the class PackageableElementFirstPassBuilder method visit.
@Override
public PackageableElement visit(SectionIndex sectionIndex) {
// NOTE: we stub out since this element doesn't have an equivalent packageable element form in PURE metamodel
org.finos.legend.pure.m3.coreinstance.Package pack = this.context.pureModel.getOrCreatePackage(sectionIndex._package);
PackageableElement stub = new Root_meta_pure_metamodel_PackageableElement_Impl("")._package(pack)._name(sectionIndex.name);
pack._childrenAdd(stub);
// NOTE: we don't really need to add section index to the PURE graph
ListIterate.forEach(sectionIndex.sections, section -> ListIterate.forEach(section.elements, elementPath -> this.context.pureModel.sectionsIndex.putIfAbsent(elementPath, section)));
return stub;
}
Aggregations