use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchema in project legend-engine by finos.
the class ExternalFormatParseTreeWalker method visitSchema.
private ExternalFormatSchema visitSchema(ExternalFormatParserGrammar.SchemaContext ctx) {
SourceInformation sourceInformation = this.walkerSourceInformation.getSourceInformation(ctx);
ExternalFormatSchema schema = new ExternalFormatSchema();
schema.sourceInformation = sourceInformation;
// id
ExternalFormatParserGrammar.SchemaIdContext idContext = PureGrammarParserUtility.validateAndExtractOptionalField(ctx.schemaId(), "id", sourceInformation);
schema.id = idContext == null ? null : PureGrammarParserUtility.fromIdentifier(idContext.identifier());
// location
ExternalFormatParserGrammar.SchemaLocationContext locationContext = PureGrammarParserUtility.validateAndExtractOptionalField(ctx.schemaLocation(), "location", sourceInformation);
schema.location = locationContext == null ? null : PureGrammarParserUtility.fromGrammarString(locationContext.STRING().getText(), true);
// content
ExternalFormatParserGrammar.SchemaContentContext contentContext = PureGrammarParserUtility.validateAndExtractRequiredField(ctx.schemaContent(), "content", sourceInformation);
schema.content = PureGrammarParserUtility.fromGrammarString(contentContext.STRING().getText(), true);
schema.contentSourceInformation = this.walkerSourceInformation.getSourceInformation(contentContext);
return schema;
}
use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchema in project legend-engine by finos.
the class ExternalFormatGrammarComposerExtension method renderSchemaSet.
private static String renderSchemaSet(ExternalFormatSchemaSet schemaSet) {
StringBuilder builder = new StringBuilder().append("SchemaSet ").append(PureGrammarComposerUtility.convertPath(schemaSet.getPath())).append("\n{\n");
PureGrammarComposerUtility.appendTabString(builder).append("format: ").append(schemaSet.format).append(";\n");
PureGrammarComposerUtility.appendTabString(builder).append("schemas: [\n");
for (int i = 0; i < schemaSet.schemas.size(); i++) {
ExternalFormatSchema schema = schemaSet.schemas.get(i);
PureGrammarComposerUtility.appendTabString(builder, 2).append("{\n");
if (schema.id != null) {
PureGrammarComposerUtility.appendTabString(builder, 3).append("id: ").append(PureGrammarComposerUtility.convertIdentifier(schema.id)).append(";\n");
}
if (schema.location != null) {
PureGrammarComposerUtility.appendTabString(builder, 3).append("location: ").append(PureGrammarComposerUtility.convertString(schema.location, true)).append(";\n");
}
PureGrammarComposerUtility.appendTabString(builder, 3).append("content: ").append(PureGrammarComposerUtility.convertString(schema.content, true)).append(";\n");
PureGrammarComposerUtility.appendTabString(builder, 2).append("}").append(i < schemaSet.schemas.size() - 1 ? ",\n" : "\n");
}
PureGrammarComposerUtility.appendTabString(builder).append("];\n}");
return builder.toString();
}
use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchema in project legend-engine by finos.
the class TestExternalFormatGrammarComposer method testFreeSectionGrammarCompose.
@Test
public void testFreeSectionGrammarCompose() {
ExternalFormatSchema one = new ExternalFormatSchema();
one.id = "one";
one.location = "first.place";
one.content = "Example one";
ExternalFormatSchema two = new ExternalFormatSchema();
two.id = "two";
two.location = "second.place";
two.content = "Example two";
ExternalFormatSchemaSet set = new ExternalFormatSchemaSet();
set._package = "test.grammar";
set.name = "ExampleSet";
set.format = "Example";
set.schemas = Arrays.asList(one, two);
ModelUnit modelUnit = new ModelUnit();
modelUnit.packageableElementIncludes = Collections.singletonList("test.grammar.AClass");
Binding binding = new Binding();
binding._package = "test.grammar";
binding.name = "ExampleBinding";
binding.contentType = "test/example";
binding.schemaSet = "test.grammar.ExampleSet";
binding.schemaId = "one";
binding.modelUnit = modelUnit;
PureModelContextData context = PureModelContextData.newBuilder().withElement(set).withElement(binding).build();
PureGrammarComposer grammarTransformer = PureGrammarComposer.newInstance(PureGrammarComposerContext.Builder.newInstance().build());
String formatted = grammarTransformer.renderPureModelContextData(context);
String expected = "###ExternalFormat\n" + "SchemaSet 'test.grammar'::ExampleSet\n" + "{\n" + " format: Example;\n" + " schemas: [\n" + " {\n" + " id: one;\n" + " location: 'first.place';\n" + " content: 'Example one';\n" + " },\n" + " {\n" + " id: two;\n" + " location: 'second.place';\n" + " content: 'Example two';\n" + " }\n" + " ];\n" + "}\n" + "\n" + "Binding 'test.grammar'::ExampleBinding\n" + "{\n" + " schemaSet: 'test.grammar.ExampleSet';\n" + " schemaId: one;\n" + " contentType: 'test/example';\n" + " modelIncludes: [\n" + " 'test.grammar.AClass'\n" + " ];\n" + "}\n";
Assert.assertEquals(expected, formatted);
}
use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchema in project legend-engine by finos.
the class SchemaSetCompiler method firstPass.
// First pass - create and index schemas
private PackageableElement firstPass(ExternalFormatSchemaSet srcSchemaSet, CompileContext context) {
if (externalFormatExtensions.get(srcSchemaSet.format) == null) {
throw new EngineException("Unknown schema format: " + srcSchemaSet.format, srcSchemaSet.formatSourceInformation, EngineErrorType.COMPILATION);
}
Root_meta_external_shared_format_metamodel_SchemaSet schemaSet = new Root_meta_external_shared_format_metamodel_SchemaSet_Impl(srcSchemaSet.name)._name(srcSchemaSet.name)._classifierGenericType(new Root_meta_pure_metamodel_type_generics_GenericType_Impl("")._rawType(context.pureModel.getType("meta::external::shared::format::metamodel::SchemaSet")))._format(srcSchemaSet.format);
Set<String> ids = Sets.mutable.empty();
Set<String> locations = Sets.mutable.empty();
for (ExternalFormatSchema srcSchema : srcSchemaSet.schemas) {
if (srcSchema.id != null) {
if (!ids.add(srcSchema.id)) {
throw new EngineException("Schema id '" + srcSchema.id + "' is duplicated", srcSchema.sourceInformation, EngineErrorType.COMPILATION);
}
}
if (srcSchema.location != null) {
if (!locations.add(srcSchema.location)) {
throw new EngineException("Schema location '" + srcSchema.location + "' is duplicated", srcSchema.sourceInformation, EngineErrorType.COMPILATION);
}
}
Root_meta_external_shared_format_metamodel_Schema schema = new Root_meta_external_shared_format_metamodel_Schema_Impl("")._id(srcSchema.id)._location(srcSchema.location);
schemaSet._schemasAdd(schema);
}
String path = context.pureModel.buildPackageString(srcSchemaSet._package, srcSchemaSet.name);
this.schemaSetIndex.put(path, schemaSet);
return schemaSet;
}
use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchema in project legend-engine by finos.
the class SchemaSetCompiler method secondPass.
// Second pass - compile external schemas using extension
private void secondPass(ExternalFormatSchemaSet srcSchemaSet, CompileContext context) {
String path = context.pureModel.buildPackageString(srcSchemaSet._package, srcSchemaSet.name);
Root_meta_external_shared_format_metamodel_SchemaSet compiled = schemaSetIndex.get(path);
ExternalFormatExtension schemaExtension = externalFormatExtensions.get(srcSchemaSet.format);
compiled._schemas(Lists.mutable.empty());
for (ExternalFormatSchema srcSchema : srcSchemaSet.schemas) {
try {
Root_meta_external_shared_format_metamodel_SchemaDetail detail = schemaExtension.compileSchema(new SchemaCompileContext(srcSchema, srcSchemaSet, context));
Root_meta_external_shared_format_metamodel_Schema schema = new Root_meta_external_shared_format_metamodel_Schema_Impl("")._id(srcSchema.id)._location(srcSchema.location)._detail(detail);
compiled._schemasAdd(schema);
} catch (ExternalFormatSchemaException e) {
throw new EngineException(e.getMessage(), srcSchema.contentSourceInformation, EngineErrorType.COMPILATION, e);
}
}
}
Aggregations