Skip to content

Commit

Permalink
Merge branch 'revert-271-revert-268-main' of https://github.com/Bhash…
Browse files Browse the repository at this point in the history
…inee/module-ballerina-persist into revert-271-revert-268-main
  • Loading branch information
Bhashinee committed Apr 1, 2024
2 parents 19832c0 + e450261 commit e91fe0c
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_306;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_307;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_308;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_309;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_401;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_402;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_403;
Expand Down Expand Up @@ -688,6 +689,63 @@ public void validateEntityNamesCaseSensitivity() {
);
}

@Test
public void validateFieldTypesForRedisDB() {
List<Diagnostic> diagnostics = getErrorDiagnostics("project_10",
"field-types.bal", 14);
testDiagnostic(
diagnostics,
new String[]{
PERSIST_309.getCode(),
PERSIST_308.getCode(),
PERSIST_308.getCode(),
PERSIST_305.getCode(),
PERSIST_305.getCode(),
PERSIST_305.getCode(),
PERSIST_305.getCode(),
PERSIST_306.getCode(),
PERSIST_306.getCode(),
PERSIST_306.getCode(),
PERSIST_306.getCode(),
PERSIST_306.getCode(),
PERSIST_306.getCode(),
PERSIST_306.getCode()
},
new String[]{
"an entity does not support optional readonly field",
"an entity does not support nillable field",
"an entity does not support nillable field",
"an entity does not support redis:Client-typed field",
"an entity does not support json-typed field",
"an entity does not support error-typed field",
"an entity does not support union-typed field",
"an entity does not support enum array field type",
"an entity does not support byte array field type",
"an entity does not support boolean array field type",
"an entity does not support json array field type",
"an entity does not support time:Civil array field type",
"an entity does not support error array field type",
"an entity does not support redis:Client array field type"
},
new String[]{
"(11:4,11:34)",
"(18:4,18:11)",
"(20:4,20:14)",
"(25:4,25:16)",
"(26:4,26:8)",
"(27:4,27:9)",
"(30:4,30:21)",
"(32:4,32:12)",
"(33:4,33:10)",
"(34:4,34:13)",
"(35:4,35:10)",
"(36:4,36:16)",
"(37:4,37:11)",
"(38:4,38:18)"
}
);
}

private List<Diagnostic> getErrorDiagnostics(String modelDirectory, String modelFileName, int count) {
DiagnosticResult diagnosticResult = loadPersistModelFile(modelDirectory, modelFileName).getCompilation()
.diagnosticResult();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
org = "root"
name = "project_10"
version = "0.1.0"

[[tool.persist]]
id = "project10"
options.datastore = "redis"
filePath = "persist/model.bal"
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import ballerina/time;
import ballerinax/redis;
import ballerina/persist as _;

public enum Gender {
M,
F
}

public type MedicalNeed record {|
readonly int needId;
readonly string stringNeedId?;

boolean booleanType;
int intType;
float floatType;
decimal decimalType;
string stringType;
string? stringNillableType;
time:Date dateType;
time:Date? dateNillableType;
time:TimeOfDay timeOfDayType;
time:Utc utcType;
time:Civil civilType;
Gender gender;
redis:Client clientType;
json jsonTest;
error errorType;

boolean booleanTypeOptional?;
time:Civil|string unionType;

Gender[] genderArray;
byte[] beneficiaryIdByteArray;
boolean[] booleanArray;
json[] jsonArray;
time:Civil[] periodArray;
error[] errorArrayType;
redis:Client[] clientArrayType;
|};

type Employee record {|
readonly string empNo;
string firstName;
string lastName;
time:Date birthDate;
Gender gender;
time:Date hireDate;

Department department;
Workspace workspace;
|};

type Workspace record {|
readonly string workspaceId;
string workspaceType;

Building location;
Employee[] employees;
|};

type Building record {|
readonly string buildingCode;
string city;
string state;
string country;
string postalCode;
string 'type;

Workspace[] workspaces;
|};

type Department record {|
readonly string deptNo;
string deptName;

Employee[] employees;
|};

type OrderItem record {|
readonly string orderId;
readonly string itemId;
int quantity;
string notes;
|};
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class Constants {
public static final String ARRAY = "[]";
public static final String LS = System.lineSeparator();
public static final String SQL_RELATION_MAPPING_ANNOTATION_NAME = "sql:Relation";
public static final String ANNOTATION_REFS_FIELD = "refs";
public static final String ANNOTATION_KEYS_FIELD = "keys";

private Constants() {
}
Expand Down Expand Up @@ -78,6 +78,7 @@ public static final class Datastores {
public static final String POSTGRESQL = "postgresql";
public static final String IN_MEMORY = "inmemory";
public static final String GOOGLE_SHEETS = "googlesheets";
public static final String REDIS = "redis";

private Datastores() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public enum DiagnosticsCodes {
PERSIST_306("PERSIST_306", "an entity does not support {0} array field type", ERROR),
PERSIST_307("PERSIST_307", "redeclared field ''{0}''", ERROR),
PERSIST_308("PERSIST_308", "an entity does not support nillable field", ERROR),
PERSIST_309("PERSIST_309", "an entity does not support optional readonly field", ERROR),


PERSIST_401("PERSIST_401", "an entity cannot reference itself in a relation field", ERROR),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
import java.util.Locale;
import java.util.Map;

import static io.ballerina.stdlib.persist.compiler.Constants.ANNOTATION_REFS_FIELD;
import static io.ballerina.stdlib.persist.compiler.Constants.ANNOTATION_KEYS_FIELD;
import static io.ballerina.stdlib.persist.compiler.Constants.BallerinaTypes.BOOLEAN;
import static io.ballerina.stdlib.persist.compiler.Constants.BallerinaTypes.DECIMAL;
import static io.ballerina.stdlib.persist.compiler.Constants.BallerinaTypes.FLOAT;
Expand All @@ -89,6 +89,7 @@
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_305;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_306;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_307;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_309;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_401;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_402;
import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_403;
Expand Down Expand Up @@ -279,11 +280,21 @@ private void validateEntityFields(Entity entity, String datastore) {

// Check if optional field
if (recordFieldNode.questionMarkToken().isPresent()) {
int startOffset = recordFieldNode.questionMarkToken().get().textRange().startOffset();
int length = recordFieldNode.semicolonToken().textRange().startOffset() - startOffset;
entity.reportDiagnostic(PERSIST_304.getCode(), PERSIST_304.getMessage(), PERSIST_304.getSeverity(),
recordFieldNode.location(),
List.of(new BNumericProperty(startOffset), new BNumericProperty(length)));
if (datastore.equals(Constants.Datastores.REDIS)) {
if (recordFieldNode.readonlyKeyword().isPresent()) {
int startOffset = recordFieldNode.questionMarkToken().get().textRange().startOffset();
int length = recordFieldNode.semicolonToken().textRange().startOffset() - startOffset;
entity.reportDiagnostic(PERSIST_309.getCode(), PERSIST_309.getMessage(),
PERSIST_309.getSeverity(), recordFieldNode.location(),
List.of(new BNumericProperty(startOffset), new BNumericProperty(length)));
}
} else {
int startOffset = recordFieldNode.questionMarkToken().get().textRange().startOffset();
int length = recordFieldNode.semicolonToken().textRange().startOffset() - startOffset;
entity.reportDiagnostic(PERSIST_304.getCode(), PERSIST_304.getMessage(), PERSIST_304.getSeverity(),
recordFieldNode.location(),
List.of(new BNumericProperty(startOffset), new BNumericProperty(length)));
}
}

Node typeNode = recordFieldNode.typeName();
Expand Down Expand Up @@ -330,31 +341,13 @@ private void validateEntityFields(Entity entity, String datastore) {
String modulePrefix = stripEscapeCharacter(qualifiedName.modulePrefix().text());
String identifier = stripEscapeCharacter(qualifiedName.identifier().text());
fieldType = modulePrefix + ":" + identifier;
if (ValidatorsByDatastore.isValidImportedType(modulePrefix, identifier, datastore)) {
if (isArrayType && !ValidatorsByDatastore.isValidArrayType(fieldType, datastore)) {
fieldType = isOptionalType
? modulePrefix + ":" + identifier + "?"
: modulePrefix + ":" + identifier;
entity.reportDiagnostic(PERSIST_306.getCode(),
MessageFormat.format(PERSIST_306.getMessage(),
modulePrefix + ":" + identifier),
PERSIST_306.getSeverity(), typeNode.location(),
List.of(new BNumericProperty(arrayStartOffset), new BNumericProperty(arrayLength),
new BStringProperty(fieldType)));
} else {
isValidType = true;
}
} else {
if (isArrayType) {
entity.reportDiagnostic(PERSIST_306.getCode(), MessageFormat.format(PERSIST_306.getMessage(),
modulePrefix + ":" + identifier), PERSIST_305.getSeverity(),
typeNode.location());
} else {
entity.reportDiagnostic(PERSIST_305.getCode(), MessageFormat.format(PERSIST_305.getMessage(),
modulePrefix + ":" + identifier), PERSIST_305.getSeverity(),
typeNode.location());
}
}
List<DiagnosticProperty<?>> properties = List.of(
new BNumericProperty(arrayStartOffset),
new BNumericProperty(arrayLength),
new BStringProperty(isOptionalType ? fieldType + "?" : fieldType));

isValidType = ValidatorsByDatastore.validateImportedTypes(
entity, typeNode, isArrayType, isOptionalType, properties, modulePrefix, identifier, datastore);
isSimpleType = true;
} else if (processedTypeNode instanceof SimpleNameReferenceNode) {
String typeName = stripEscapeCharacter(
Expand Down Expand Up @@ -852,7 +845,7 @@ private void validateRelationAnnotationFieldName(SimpleTypeField field, Entity r
RelationField ownerRelationField) {
List<String> references = readStringArrayValueFromAnnotation
(ownerRelationField.getAnnotations(), Constants.SQL_RELATION_MAPPING_ANNOTATION_NAME,
ANNOTATION_REFS_FIELD);
ANNOTATION_KEYS_FIELD);
if (references == null || !references.contains(field.getName())) {
reportDiagnosticsEntity.reportDiagnostic(PERSIST_422.getCode(), MessageFormat.format(
PERSIST_422.getMessage(), foreignKey, referredEntity.getEntityName()),
Expand Down
Loading

0 comments on commit e91fe0c

Please sign in to comment.