Skip to content

Commit

Permalink
Merge pull request #278 from Bhashinee/revert-271-revert-268-main
Browse files Browse the repository at this point in the history
Fix persist compiler crash when no persist configurations are found
  • Loading branch information
daneshk committed Apr 2, 2024
2 parents 7048f30 + 125487e commit b981b10
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- [Fixed the persist compiler crash when no persist configurations are found](https://github.com/ballerina-platform/ballerina-library/issues/6187)

### Added
- [Added compiler plugin validations for Postgresql as a datasource](https://github.com/ballerina-platform/ballerina-library/issues/5829)

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

import io.ballerina.projects.DiagnosticResult;
import io.ballerina.projects.Package;
import io.ballerina.projects.PackageCompilation;
import io.ballerina.projects.directory.BuildProject;
import io.ballerina.projects.directory.SingleFileProject;
import io.ballerina.tools.diagnostics.Diagnostic;
Expand Down Expand Up @@ -105,13 +106,8 @@ public void validateWithOldPersistTomlConfig() {

@Test
public void testWithoutDatastore() {
try {
loadPersistModelFile("project_9", "field-types.bal").getCompilation();
Assert.fail("Compilation should fail");
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("the persist.datastore configuration does not exist in " +
"the Ballerina.toml file"));
}
PackageCompilation loadedProject = loadPersistModelFile("project_9", "field-types.bal").getCompilation();
Assert.assertEquals(loadedProject.diagnosticResult().errorCount(), 0);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ private static String getDataStoreName(Path configPath) throws BalException {
}
}
}
throw new BalException("the persist.datastore configuration does not exist in the Ballerina.toml file");
// Skip data store-specific validations if the `tool.persist` configuration does not exist in the `toml`
// files, instead of returning an error.
return null;
} catch (IOException e) {
throw new BalException("error while reading persist configurations. " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static boolean validateSimpleTypes(Entity entity, Node typeNode, String t
List<DiagnosticProperty<?>> properties, String type, String datastore) {
boolean validFlag = true;

if (isOptionalType && (datastore.equals(Constants.Datastores.GOOGLE_SHEETS)
|| datastore.equals(Constants.Datastores.REDIS))) {
if (isOptionalType && (Constants.Datastores.GOOGLE_SHEETS.equals(datastore)
|| Constants.Datastores.REDIS.equals(datastore))) {
entity.reportDiagnostic(PERSIST_308.getCode(),
MessageFormat.format(PERSIST_308.getMessage(), type),
PERSIST_308.getSeverity(), typeNode.location(), properties);
Expand Down Expand Up @@ -127,6 +127,10 @@ public static boolean validateImportedTypes(Entity entity, Node typeNode,
}

public static boolean isValidSimpleType(String type, String datastore) {
// If the datastore is null(ex: before executing the generate command), ignore the data type validation.
if (null == datastore) {
return true;
}
switch (datastore) {
case Constants.Datastores.MYSQL:
return isValidMysqlType(type);
Expand All @@ -146,6 +150,10 @@ public static boolean isValidSimpleType(String type, String datastore) {
}

public static boolean isValidArrayType(String type, String datastore) {
// If the datastore is null(ex: before executing the generate command), ignore the data type validation.
if (null == datastore) {
return true;
}
switch (datastore) {
case Constants.Datastores.MYSQL:
return isValidMysqlArrayType(type);
Expand All @@ -165,7 +173,10 @@ public static boolean isValidArrayType(String type, String datastore) {
}

public static boolean isValidImportedType(String modulePrefix, String identifier, String datastore) {

// If the datastore is null(ex: before executing the generate command), ignore the data type validation.
if (null == datastore) {
return true;
}
switch (datastore) {
case Constants.Datastores.MYSQL:
return isValidMysqlImportedType(modulePrefix, identifier);
Expand Down

0 comments on commit b981b10

Please sign in to comment.