Search in sources :

Example 1 with ExternalFormatSchemaSet

use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet in project legend-engine by finos.

the class TestModelToFlatDataGeneration method testSimpleCsv.

@Test
public void testSimpleCsv() {
    String modelCode = "Class test::gen::Data\n" + "{\n" + "  name        : String[1];\n" + "  employed    : Boolean[0..1];\n" + "  iq          : Integer[0..1];\n" + "  weightKg    : Float[0..1];\n" + "  heightM     : Decimal[1];\n" + "  dateOfBirth : StrictDate[1];\n" + "  timeOfDeath : DateTime[1];\n" + "}";
    PureModelContextData generated = generateSchema(modelCode, config("test::gen"));
    Binding binding = generated.getElementsOfType(Binding.class).stream().findFirst().get();
    Assert.assertEquals("test::gen::TestBinding", binding.getPath());
    Assert.assertEquals("test::gen::TestSchemaSet", binding.schemaSet);
    Assert.assertEquals(Collections.singletonList("test::gen::Data"), binding.modelUnit.packageableElementIncludes);
    ExternalFormatSchemaSet schemaSet = generated.getElementsOfType(ExternalFormatSchemaSet.class).stream().findFirst().get();
    String expectedDefiniiton = "section Data: DelimitedWithHeadings\n" + "{\n" + "  scope.untilEof;\n" + "  delimiter: ',';\n" + "\n" + "  Record\n" + "  {\n" + "    name: STRING;\n" + "    employed: BOOLEAN(optional);\n" + "    iq: INTEGER(optional);\n" + "    weightKg: DECIMAL(optional);\n" + "    heightM: DECIMAL;\n" + "    dateOfBirth: DATE;\n" + "    timeOfDeath: DATETIME;\n" + "  }\n" + "}";
    Assert.assertEquals(expectedDefiniiton, schemaSet.schemas.get(0).content);
}
Also used : Binding(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.Binding) ExternalFormatSchemaSet(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) ModelToSchemaGenerationTest(org.finos.legend.engine.external.shared.format.model.test.ModelToSchemaGenerationTest) Test(org.junit.Test)

Example 2 with ExternalFormatSchemaSet

use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet in project legend-engine by finos.

the class TestModelToJsonSchemaGeneration method testSimpleJsonSchema.

@Test
public void testSimpleJsonSchema() {
    String modelCode = "Class test::gen::Data\n" + "{\n" + "  name        : String[1];\n" + "  employed    : Boolean[0..1];\n" + "  iq          : Integer[0..1];\n" + "  weightKg    : Float[0..1];\n" + "  heightM     : Decimal[1];\n" + "  dateOfBirth : StrictDate[1];\n" + "  timeOfDeath : DateTime[1];\n" + "}";
    PureModelContextData generated = generateSchema(modelCode, config("test::gen", Lists.mutable.with("test::gen::Data")));
    Binding binding = generated.getElementsOfType(Binding.class).stream().findFirst().get();
    Assert.assertEquals("test::gen::TestBinding", binding.getPath());
    Assert.assertEquals("test::gen::TestSchemaSet", binding.schemaSet);
    Assert.assertEquals(Collections.singletonList("test::gen::Data"), binding.modelUnit.packageableElementIncludes);
    ExternalFormatSchemaSet schemaSet = generated.getElementsOfType(ExternalFormatSchemaSet.class).stream().findFirst().get();
    String expectedDefiniiton = "{\n" + "  \"$schema\": \"http:\\/\\/json-schema.org\\/draft-07\\/schema#\",\n" + "  \"title\": \"test::gen::Data\",\n" + "  \"type\": \"object\",\n" + "  \"properties\":   {\n" + "    \"name\":     {\n" + "      \"type\": \"string\"\n" + "    },\n" + "    \"employed\":     {\n" + "      \"type\": \"boolean\"\n" + "    },\n" + "    \"iq\":     {\n" + "      \"type\": \"integer\"\n" + "    },\n" + "    \"weightKg\":     {\n" + "      \"type\": \"number\"\n" + "    },\n" + "    \"heightM\":     {\n" + "      \"type\": \"number\"\n" + "    },\n" + "    \"dateOfBirth\":     {\n" + "      \"type\": \"string\",\n" + "      \"format\": \"date\"\n" + "    },\n" + "    \"timeOfDeath\":     {\n" + "      \"type\": \"string\",\n" + "      \"format\": \"date-time\"\n" + "    }\n" + "  },\n" + "  \"required\": [\n" + "\"name\",\n" + "\"heightM\",\n" + "\"dateOfBirth\",\n" + "\"timeOfDeath\"\n" + "  ]\n" + "}\n";
    Assert.assertEquals(expectedDefiniiton, schemaSet.schemas.get(0).content);
}
Also used : Binding(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.Binding) ExternalFormatSchemaSet(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) ModelToSchemaGenerationTest(org.finos.legend.engine.external.shared.format.model.test.ModelToSchemaGenerationTest) Test(org.junit.Test)

Example 3 with ExternalFormatSchemaSet

use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet 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();
}
Also used : ExternalFormatSchema(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchema)

Example 4 with ExternalFormatSchemaSet

use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet 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);
}
Also used : Binding(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.Binding) ExternalFormatSchema(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchema) ModelUnit(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.ModelUnit) ExternalFormatSchemaSet(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureGrammarComposer(org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer) Test(org.junit.Test)

Example 5 with ExternalFormatSchemaSet

use of org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet in project legend-engine by finos.

the class TestModelToJsonSchemaGeneration method testMultiLevelNestingWithJsonSchema.

@Test
public void testMultiLevelNestingWithJsonSchema() {
    String modelCode = "Enum test::Simple::AddressType\n" + "{\n" + "  HOME,\n" + "  OFFICE,\n" + "  WORKSHOP\n" + "}\n" + "\n" + "Class test::Simple::Person\n" + "{\n" + "  firstName: String[1];\n" + "  lastName: String[1];\n" + "  middleName: String[0..1];\n" + "  age: Integer[0..1];\n" + "  addresses: test::Simple::Address[*];\n" + "  firm: test::Simple::Firm[1];\n" + "}\n" + "\n" + "Class test::Simple::Address\n" + "{\n" + "  addressType: test::Simple::AddressType[1];\n" + "  addressLine1: String[1];\n" + "  addressLine2: String[0..1];\n" + "  addressLine3: String[0..1];\n" + "}\n" + "\n" + "Class test::Simple::Firm\n" + "{\n" + "  legalName: String[1];\n" + "  addresses: test::Simple::Address[*];\n" + "}\n";
    PureModelContextData generated = generateSchema(modelCode, config("test::gen", Lists.mutable.with("test::Simple::Person", "test::Simple::Firm", "test::Simple::Address", "test::Simple::AddressType")));
    Binding binding = generated.getElementsOfType(Binding.class).stream().findFirst().get();
    Assert.assertEquals("test::gen::TestBinding", binding.getPath());
    Assert.assertEquals("test::gen::TestSchemaSet", binding.schemaSet);
    Assert.assertEquals(Lists.mutable.with("test::Simple::Person", "test::Simple::Firm", "test::Simple::Address", "test::Simple::AddressType"), binding.modelUnit.packageableElementIncludes);
    ExternalFormatSchemaSet schemaSet = generated.getElementsOfType(ExternalFormatSchemaSet.class).stream().findFirst().get();
    String expectedDefiniiton = "{\n" + "  \"$schema\": \"http:\\/\\/json-schema.org\\/draft-07\\/schema#\",\n" + "  \"title\": \"test::Simple::Person\",\n" + "  \"type\": \"object\",\n" + "  \"properties\":   {\n" + "    \"firstName\":     {\n" + "      \"type\": \"string\"\n" + "    },\n" + "    \"lastName\":     {\n" + "      \"type\": \"string\"\n" + "    },\n" + "    \"middleName\":     {\n" + "      \"type\": \"string\"\n" + "    },\n" + "    \"age\":     {\n" + "      \"type\": \"integer\"\n" + "    },\n" + "    \"addresses\":     {\n" + "      \"type\": \"array\",\n" + "      \"items\":       {\n" + "        \"$ref\": \"#\\/definitions\\/test::Simple::Address\"\n" + "      }\n" + "    },\n" + "    \"firm\":     {\n" + "      \"$ref\": \"#\\/definitions\\/test::Simple::Firm\"\n" + "    }\n" + "  },\n" + "  \"required\": [\n" + "\"firstName\",\n" + "\"lastName\",\n" + "\"firm\"\n" + "  ],\n" + "  \"definitions\":   {\n" + "    \"test::Simple::Address\":     {\n" + "      \"$schema\": \"http:\\/\\/json-schema.org\\/draft-07\\/schema#\",\n" + "      \"title\": \"test::Simple::Address\",\n" + "      \"type\": \"object\",\n" + "      \"properties\":       {\n" + "        \"addressType\":         {\n" + "          \"$ref\": \"#\\/definitions\\/test::Simple::AddressType\"\n" + "        },\n" + "        \"addressLine1\":         {\n" + "          \"type\": \"string\"\n" + "        },\n" + "        \"addressLine2\":         {\n" + "          \"type\": \"string\"\n" + "        },\n" + "        \"addressLine3\":         {\n" + "          \"type\": \"string\"\n" + "        }\n" + "      },\n" + "      \"required\": [\n" + "\"addressType\",\n" + "\"addressLine1\"\n" + "      ]\n" + "    },\n" + "    \"test::Simple::AddressType\":     {\n" + "      \"$schema\": \"http:\\/\\/json-schema.org\\/draft-07\\/schema#\",\n" + "      \"title\": \"test::Simple::AddressType\",\n" + "      \"enum\": [\n" + "\"HOME\",\n" + "\"OFFICE\",\n" + "\"WORKSHOP\"\n" + "      ],\n" + "      \"type\": \"string\"\n" + "    },\n" + "    \"test::Simple::Firm\":     {\n" + "      \"$schema\": \"http:\\/\\/json-schema.org\\/draft-07\\/schema#\",\n" + "      \"title\": \"test::Simple::Firm\",\n" + "      \"type\": \"object\",\n" + "      \"properties\":       {\n" + "        \"legalName\":         {\n" + "          \"type\": \"string\"\n" + "        },\n" + "        \"addresses\":         {\n" + "          \"type\": \"array\",\n" + "          \"items\":           {\n" + "            \"$ref\": \"#\\/definitions\\/test::Simple::Address\"\n" + "          }\n" + "        }\n" + "      },\n" + "      \"required\": [\n" + "\"legalName\"\n" + "      ]\n" + "    }\n" + "  }\n" + "}\n";
    Assert.assertEquals(expectedDefiniiton, schemaSet.schemas.get(0).content);
}
Also used : Binding(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.Binding) ExternalFormatSchemaSet(org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) ModelToSchemaGenerationTest(org.finos.legend.engine.external.shared.format.model.test.ModelToSchemaGenerationTest) Test(org.junit.Test)

Aggregations

ExternalFormatSchemaSet (org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchemaSet)8 PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)6 Binding (org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.Binding)6 Test (org.junit.Test)6 ModelToSchemaGenerationTest (org.finos.legend.engine.external.shared.format.model.test.ModelToSchemaGenerationTest)5 ExternalFormatSchema (org.finos.legend.engine.protocol.pure.v1.packageableElement.external.shared.ExternalFormatSchema)5 EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)3 Root_meta_external_shared_format_metamodel_Schema (org.finos.legend.pure.generated.Root_meta_external_shared_format_metamodel_Schema)3 ExternalFormatExtension (org.finos.legend.engine.external.shared.format.model.ExternalFormatExtension)2 Root_meta_external_shared_format_metamodel_SchemaSet (org.finos.legend.pure.generated.Root_meta_external_shared_format_metamodel_SchemaSet)2 Root_meta_external_shared_format_metamodel_Schema_Impl (org.finos.legend.pure.generated.Root_meta_external_shared_format_metamodel_Schema_Impl)2 ExternalSchemaCompileContext (org.finos.legend.engine.external.shared.format.model.ExternalSchemaCompileContext)1 ExternalFormatSchemaException (org.finos.legend.engine.external.shared.format.model.compile.ExternalFormatSchemaException)1 ExternalFormatParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.ExternalFormatParserGrammar)1 PureGrammarComposer (org.finos.legend.engine.language.pure.grammar.to.PureGrammarComposer)1 SourceInformation (org.finos.legend.engine.protocol.pure.v1.model.SourceInformation)1 ModelUnit (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.ModelUnit)1 Root_meta_external_shared_format_metamodel_SchemaDetail (org.finos.legend.pure.generated.Root_meta_external_shared_format_metamodel_SchemaDetail)1 Root_meta_external_shared_format_metamodel_SchemaSet_Impl (org.finos.legend.pure.generated.Root_meta_external_shared_format_metamodel_SchemaSet_Impl)1 Root_meta_pure_metamodel_type_generics_GenericType_Impl (org.finos.legend.pure.generated.Root_meta_pure_metamodel_type_generics_GenericType_Impl)1