Skip to content

Commit

Permalink
Avoid duplicate enum class generations (finos#641)
Browse files Browse the repository at this point in the history
* Avoid duplicate enum class generations

* Update writeEnumJavaClass

* Fix build

* Update Exception

Co-authored-by: Kevin Knight <[email protected]>

---------

Co-authored-by: Kevin Knight <[email protected]>
  • Loading branch information
mrudula-gs and kevin-m-knight-gs authored May 17, 2023
1 parent 83043c2 commit bb4bf2d
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import javax.lang.model.SourceVersion;

public class ServiceExecutionGenerator
Expand Down Expand Up @@ -113,7 +115,7 @@ public void generate() throws IOException
GeneratedJavaCode generatedJavaClass = EnumerationClassGenerator.newGenerator(this.packagePrefix)
.withEnumeration(enumeration)
.generate();
writeJavaClass(generatedJavaClass);
writeEnumJavaClass(generatedJavaClass);
}

// Generate execution plan for service
Expand Down Expand Up @@ -173,6 +175,25 @@ private void writeJavaClass(GeneratedJavaCode generatedJavaClass) throws IOExcep
}
}

private void writeEnumJavaClass(GeneratedJavaCode generatedJavaClass) throws IOException
{
Path javaClassPath = this.javaSourceOutputDirectory.resolve(getJavaSourceFileRelativePath(generatedJavaClass.getClassName()));
byte[] content = generatedJavaClass.getText().getBytes(StandardCharsets.UTF_8);
if (Files.exists(javaClassPath))
{
byte[] foundContent = Files.readAllBytes(javaClassPath);
if (!Arrays.equals(content, foundContent))
{
throw new FileAlreadyExistsException(javaClassPath.toString(), null, "Duplicate file path found with different content for enum: " + generatedJavaClass.getClassName());
}
}
else
{
Files.createDirectories(javaClassPath.getParent());
Files.write(javaClassPath, content, StandardOpenOption.CREATE_NEW);
}
}

public static ServiceExecutionGenerator newGenerator(Service service, PureModel pureModel, String packagePrefix, Path javaSourceOutputDirectory, Path resourceOutputDirectory)
{
return newGenerator(service, pureModel, packagePrefix, javaSourceOutputDirectory, resourceOutputDirectory, null, Lists.mutable.empty(), Lists.mutable.empty(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,11 @@ public void testRelationalWithParams() throws Exception
public void testRelationalWithEnumParams() throws Exception
{
String packagePrefix = "org.finos";
Service service = getService("service::RelationalServiceWithEnumParams");
ClassLoader classLoader = generateAndCompile(packagePrefix, service);
Service enumParamService = getService("service::RelationalServiceWithEnumParams");
Service reusedEnumParamService = getService("service::RelationalServiceWithEnumParamsReused");
ClassLoader classLoader = generateAndCompile(packagePrefix, Lists.fixedSize.of(enumParamService, reusedEnumParamService));
assertExecuteMethods(classLoader, "org.finos.service.RelationalServiceWithEnumParams", classLoader.loadClass("org.finos.model.Country"), classLoader.loadClass("org.finos.model._enum.Country"), String.class);
assertExecuteMethods(classLoader, "org.finos.service.RelationalServiceWithEnumParamsReused", classLoader.loadClass("org.finos.model.Country"), classLoader.loadClass("org.finos.model._enum.Country"), String.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.finos.service;

import org.eclipse.collections.api.factory.Lists;
import org.finos.legend.engine.language.pure.dsl.service.execution.AbstractServicePlanExecutor;
import org.finos.legend.engine.language.pure.dsl.service.execution.ServiceVariable;
import org.finos.legend.engine.plan.execution.result.Result;
import org.finos.legend.engine.plan.execution.stores.StoreExecutorConfiguration;
import org.finos.legend.engine.shared.core.url.StreamProvider;

import java.util.List;

public class RelationalServiceWithEnumParamsReused extends AbstractServicePlanExecutor
{
public RelationalServiceWithEnumParamsReused()
{
super("service::RelationalServiceWithEnumParamsReused", "org/finos/legend/sdlc/generation/service/entities/service/RelationalServiceWithEnumParamsReused.json", false);
}

public RelationalServiceWithEnumParamsReused(StoreExecutorConfiguration... storeExecutorConfigurations)
{
super("service::RelationalServiceWithEnumParamsReused", "org/finos/legend/sdlc/generation/service/entities/service/RelationalServiceWithEnumParamsReused.json", storeExecutorConfigurations);
}

public Result execute(org.finos.model.Country cou, org.finos.model._enum.Country couName, String name)
{
return this.execute(cou, couName, name, null);
}

public Result execute(org.finos.model.Country cou, org.finos.model._enum.Country couName, String name, StreamProvider streamProvider)
{
return this.newExecutionBuilder(3)
.withStreamProvider(streamProvider)
.withParameter("cou", cou)
.withParameter("couName", couName)
.withParameter("name", name)
.execute();
}

@Override
public final List<ServiceVariable> getServiceVariables()
{
return Lists.mutable.of(
this.newServiceVariable("cou", org.finos.model.Country.class, 0, 1),
this.newServiceVariable("couName", org.finos.model._enum.Country.class, 1, 1),
this.newServiceVariable("name", String.class, 1, 1)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
{
"content": {
"_type": "service",
"autoActivateUpdates": true,
"documentation": "",
"execution": {
"_type": "pureSingleExecution",
"func": {
"_type": "lambda",
"body": [
{
"_type": "func",
"function": "project",
"parameters": [
{
"_type": "func",
"function": "filter",
"parameters": [
{
"_type": "func",
"function": "getAll",
"parameters": [
{
"_type": "packageableElementPtr",
"fullPath": "model::SourcePerson"
}
]
},
{
"_type": "lambda",
"body": [
{
"_type": "func",
"function": "and",
"parameters": [
{
"_type": "func",
"function": "equal",
"parameters": [
{
"_type": "property",
"parameters": [
{
"_type": "var",
"name": "p"
}
],
"property": "countryName"
},
{
"_type": "var",
"name": "couName"
}
]
},
{
"_type": "func",
"function": "equal",
"parameters": [
{
"_type": "property",
"parameters": [
{
"_type": "var",
"name": "p"
}
],
"property": "firstName"
},
{
"_type": "var",
"name": "name"
}
]
}
]
}
],
"parameters": [
{
"_type": "var",
"name": "p"
}
]
}
]
},
{
"_type": "collection",
"multiplicity": {
"lowerBound": 2,
"upperBound": 2
},
"values": [
{
"_type": "lambda",
"body": [
{
"_type": "property",
"parameters": [
{
"_type": "var",
"name": "x"
}
],
"property": "lastName"
}
],
"parameters": [
{
"_type": "var",
"name": "x"
}
]
},
{
"_type": "lambda",
"body": [
{
"_type": "func",
"function": "if",
"parameters": [
{
"_type": "func",
"function": "and",
"parameters": [
{
"_type": "func",
"function": "isNotEmpty",
"parameters": [
{
"_type": "var",
"name": "cou"
}
]
},
{
"_type": "func",
"function": "equal",
"parameters": [
{
"_type": "var",
"name": "cou"
},
{
"_type": "property",
"parameters": [
{
"_type": "packageableElementPtr",
"fullPath": "model::Country"
}
],
"property": "AMEA"
}
]
}
]
},
{
"_type": "lambda",
"body": [
{
"_type": "string",
"value": "America"
}
],
"parameters": []
},
{
"_type": "lambda",
"body": [
{
"_type": "string",
"value": "India"
}
],
"parameters": []
}
]
}
],
"parameters": [
{
"_type": "var",
"name": "x"
}
]
}
]
},
{
"_type": "collection",
"multiplicity": {
"lowerBound": 2,
"upperBound": 2
},
"values": [
{
"_type": "string",
"value": "Last Name"
},
{
"_type": "string",
"value": "Country"
}
]
}
]
}
],
"parameters": [
{
"_type": "var",
"class": "model::Country",
"multiplicity": {
"lowerBound": 0,
"upperBound": 1
},
"name": "cou"
},
{
"_type": "var",
"class": "model::enum::Country",
"multiplicity": {
"lowerBound": 1,
"upperBound": 1
},
"name": "couName"
},
{
"_type": "var",
"class": "String",
"multiplicity": {
"lowerBound": 1,
"upperBound": 1
},
"name": "name"
}
]
},
"mapping": "model::RelationalMapping",
"runtime": {
"_type": "engineRuntime",
"connections": [
{
"store": {
"path": "store::MyDatabase",
"type": "STORE"
},
"storeConnections": [
{
"connection": {
"_type": "RelationalDatabaseConnection",
"authenticationStrategy": {
"_type": "h2Default"
},
"databaseType": "H2",
"datasourceSpecification": {
"_type": "h2Local",
"testDataSetupCsv": "MAIN_SCHEMA\nPERSON_TABLE\nID,FIRST_NAME,LAST_NAME,AGE,COUNTRY,COUNTRY_NAME\n1,Peter,Smith,22,AMEA,Europe\n2,John,Johnson,23,AMEA,America\n3,John,Peterson,26,AMEA,America\n"
},
"element": "store::MyDatabase",
"type": "H2"
},
"id": "connection_1"
}
]
}
],
"mappings": [
{
"path": "model::RelationalMapping",
"type": "MAPPING"
}
]
}
},
"name": "RelationalServiceWithEnumParamsReused",
"owners": [],
"package": "service",
"pattern": "/relationalService/{couName}/{name}",
"test": {
"_type": "singleExecutionTest",
"asserts": [],
"data": ""
}
},
"classifierPath": "meta::legend::service::metamodel::Service"
}

0 comments on commit bb4bf2d

Please sign in to comment.