Skip to content

Commit

Permalink
Run converters on standard deserializer flow + during parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey committed Nov 25, 2024
1 parent 925c37f commit a7a21a1
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,10 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<scope>test</scope>
</dependency>
<!-- JACKSON -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.finos.legend.engine.language.pure.grammar.from;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.eclipse.collections.api.factory.Lists;
Expand All @@ -37,6 +38,7 @@
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.Lambda;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.classInstance.graph.RootGraphFetchTree;
import org.finos.legend.engine.shared.core.ObjectMapperFactory;
import org.finos.legend.engine.shared.core.identity.Identity;
import org.finos.legend.engine.shared.core.operational.errorManagement.EngineException;
import org.finos.legend.engine.shared.core.operational.logs.LogInfo;
Expand All @@ -52,6 +54,7 @@ public class PureGrammarParser

private final DEPRECATED_PureGrammarParserLibrary parsers;
private final PureGrammarParserExtensions extensions;
private ObjectMapper converterMapper;

private PureGrammarParser(PureGrammarParserExtensions extensions)
{
Expand All @@ -63,6 +66,7 @@ private PureGrammarParser(PureGrammarParserExtensions extensions)
connectionParser,
RuntimeParser.newInstance(connectionParser)
));
this.converterMapper = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports();
}

public static PureGrammarParser newInstance(PureGrammarParserExtensions extensions)
Expand Down Expand Up @@ -127,7 +131,9 @@ private PureModelContextData parse(String code, DEPRECATED_PureGrammarParserLibr
sectionIndex.name = "SectionIndex";
sectionIndex._package = "__internal__";
sectionIndex.sections = ListIterate.collect(parser.definition().section(), sectionCtx -> this.visitSection(sectionCtx, parserLibrary, walkerSourceInformation, parserContext, builder::addElement, returnSourceInfo));
return builder.withElement(sectionIndex).build();
// tactically run parsed values thru converters to fix old/legacy code
PureModelContextData pmcd = builder.withElement(sectionIndex).build();
return this.converterMapper.convertValue(pmcd, PureModelContextData.class);
}

private Section visitSection(CodeParserGrammar.SectionContext ctx, DEPRECATED_PureGrammarParserLibrary parserLibrary, ParseTreeWalkerSourceInformation walkerSourceInformation, PureGrammarParserContext parserContext, Consumer<PackageableElement> elementConsumer, boolean returnSourceInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,12 @@ private static void test(String code, String message, boolean keepSectionIndex)

public static void testFormatWithSectionInfoPreserved(String code, String unformattedCode)
{
testFormat(code, unformattedCode, false, false);
testFormat(code, unformattedCode, false);
}

public static void testFormat(String code, String unformattedCode)
{
testFormat(code, unformattedCode, true, false);
}

public static void testConvert(String code, String unformattedCode)
{
testFormat(code, unformattedCode, true, true);
testFormat(code, unformattedCode, true);
}

/**
Expand All @@ -148,7 +143,7 @@ public static void testConvert(String code, String unformattedCode)
* ...
* // end of example
*/
private static void testFormat(String code, String unformattedCode, boolean omitSectionIndex, boolean withConvert)
private static void testFormat(String code, String unformattedCode, boolean omitSectionIndex)
{
PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
// NOTE: no need to get source information
Expand All @@ -158,12 +153,6 @@ private static void testFormat(String code, String unformattedCode, boolean omit
parsedModel = PureModelContextData.newPureModelContextData(parsedModel.getSerializer(), parsedModel.getOrigin(), LazyIterate.reject(parsedModel.getElements(), e -> e instanceof SectionIndex));
}

if (withConvert)
{
// run json deserializer to apply protocol updates (migrate from old to new)...
parsedModel = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolConverterSupports().convertValue(parsedModel, PureModelContextData.class);
}

String formatted = grammarTransformer.renderPureModelContextData(parsedModel);
Assert.assertEquals(code, formatted);
// NOTE: do not remove the round-trip test for formatted code as this is a very good way to ensure that grammar <-> >protocol is bijective
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ public void testTypeArgumentsAreBackwardCompatibleForOldProtocolMissingIt() thro
" {\n" +
// ---------- type to construct, no type arguments
" \"_type\": \"packageableElementPtr\",\n" +
" \"fullPath\": \"BasicColumnSpecification\"\n" +
" \"fullPath\": \"TdsOlapRank\"\n" +
// ---------- type to construct, no type arguments
" },\n" +
" {\n" +
Expand Down Expand Up @@ -608,7 +608,7 @@ public void testTypeArgumentsAreBackwardCompatibleForOldProtocolMissingIt() thro
"}",
"function test::new(): Any[1]\n" +
"{\n" +
" ^BasicColumnSpecification<TDSRow>(func=r: TDSRow[1]|1)\n" +
" meta::pure::tds::func(r: TDSRow[1]|1)\n" +
"}\n");

testComposedGrammar("{\n" +
Expand Down Expand Up @@ -667,58 +667,58 @@ public void testTypeArgumentsAreBackwardCompatibleForOldProtocolMissingIt() thro
@Test
public void testConvertBasicColumnSpecification()
{
testConvert("function test::new(): Any[1]\n" +
"{\n" +
" meta::pure::tds::col(r: TDSRow[1]|1, 'hello')\n" +
"}\n",
testFormat("function test::new(): Any[1]\n" +
"{\n" +
" meta::pure::tds::col(r: TDSRow[1]|1, 'hello')\n" +
"}\n",
"function test::new(): Any[1]\n" +
"{\n" +
" ^BasicColumnSpecification(func=r: TDSRow[1]|1, name='hello')\n" +
"}\n");

testConvert("function test::new(): Any[1]\n" +
"{\n" +
" meta::pure::tds::col(r: TDSRow[1]|1, 'hello', 'documentation here')\n" +
"}\n",
"{\n" +
" ^BasicColumnSpecification(func=r: TDSRow[1]|1, name='hello')\n" +
"}\n");

testFormat("function test::new(): Any[1]\n" +
"{\n" +
" meta::pure::tds::col(r: TDSRow[1]|1, 'hello', 'documentation here')\n" +
"}\n",
"function test::new(): Any[1]\n" +
"{\n" +
" ^meta::pure::tds::BasicColumnSpecification(documentation='documentation here', func=r: TDSRow[1]|1, name='hello')\n" +
"}\n");
"{\n" +
" ^meta::pure::tds::BasicColumnSpecification(documentation='documentation here', func=r: TDSRow[1]|1, name='hello')\n" +
"}\n");
}

@Test
public void testConvertTdsOlapRank()
{
testConvert("function test::new(): Any[1]\n" +
"{\n" +
" meta::pure::tds::func(r: TDSRow[1]|1)\n" +
"}\n",
testFormat("function test::new(): Any[1]\n" +
"{\n" +
" meta::pure::tds::func(r: TDSRow[1]|1)\n" +
"}\n",
"function test::new(): Any[1]\n" +
"{\n" +
" ^meta::pure::tds::TdsOlapRank(func=r: TDSRow[1]|1)\n" +
"}\n");

testConvert("function test::new(): Any[1]\n" +
"{\n" +
" meta::pure::tds::func(r: TDSRow[1]|1)\n" +
"}\n",
"{\n" +
" ^meta::pure::tds::TdsOlapRank(func=r: TDSRow[1]|1)\n" +
"}\n");

testFormat("function test::new(): Any[1]\n" +
"{\n" +
" meta::pure::tds::func(r: TDSRow[1]|1)\n" +
"}\n",
"function test::new(): Any[1]\n" +
"{\n" +
" ^TdsOlapRank(func=r: TDSRow[1]|1)\n" +
"}\n");
"{\n" +
" ^TdsOlapRank(func=r: TDSRow[1]|1)\n" +
"}\n");
}

@Test
public void testConvertResult()
{
testConvert("function test::new(res: Result<meta::pure::metamodel::type::Any|1..*>[1]): Any[1]\n" +
"{\n" +
" 1\n" +
"}\n",
testFormat("function test::new(res: Result<meta::pure::metamodel::type::Any|1..*>[1]): Any[1]\n" +
"{\n" +
" 1\n" +
"}\n",
"function test::new(res: Result[1]): Any[1]\n" +
"{\n" +
" 1\n" +
"}\n");
"{\n" +
" 1\n" +
"}\n");
}

@Test
Expand Down
Loading

0 comments on commit a7a21a1

Please sign in to comment.