Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for relationships validations and enums values only ints allowed. #13

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/main/antlr4/io.github.zenwave360.zdl.antlr/Zdl.g4
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,16 @@ field_validations: field_validation_name (LPAREN field_validation_value RPAREN)?
field_validation_name: REQUIRED | UNIQUE | MIN | MAX | MINLENGTH | MAXLENGTH | PATTERN;
field_validation_value: INT | ID | PATTERN_REGEX;
nested_field_validations: nested_field_validation_name (LPAREN nested_field_validation_value RPAREN)?;
nested_field_validation_name: REQUIRED | UNIQUE | MIN | MAX;
nested_field_validation_value: INT | ID | PATTERN_REGEX;
nested_field_validation_name: REQUIRED | UNIQUE | MINLENGTH | MAXLENGTH;
nested_field_validation_value: INT | ID;

// enums
enum: javadoc? annotations ENUM enum_name enum_body;
enum_name: ID;
enum_body: LBRACE (enum_value)* RBRACE;
enum_value: javadoc? enum_value_name (LPAREN enum_value_value RPAREN)? suffix_javadoc? COMMA?;
enum_value_name: ID;
enum_value_value: simple;
enum_value_value: INT;

// inputs
input: javadoc? annotations INPUT input_name LBRACE fields RBRACE;
Expand All @@ -233,8 +233,9 @@ relationship_field_name: keyword;
relationship_description_field: ID;
relationship_field_validations: relationship_field_required? relationship_field_min? relationship_field_max?;
relationship_field_required: REQUIRED;
relationship_field_min: MIN(INT);
relationship_field_max: MAX(INT);
relationship_field_min: MINLENGTH LPAREN relationship_field_value RPAREN;
relationship_field_max: MAXLENGTH LPAREN relationship_field_value RPAREN;
relationship_field_value: INT;

// aggregates
aggregate: javadoc? annotations AGGREGATE aggregate_name LPAREN aggregate_root RPAREN LBRACE aggregate_command* RBRACE;
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/io/github/zenwave360/zdl/antlr/ZdlListenerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void enterLegacy_constants(io.github.zenwave360.zdl.antlr.ZdlParser.Legac
}

@Override
public void enterImports(ZdlParser.ImportsContext ctx) {
for (ZdlParser.Import_valueContext importValue : ctx.import_value()) {
public void enterImports(io.github.zenwave360.zdl.antlr.ZdlParser.ImportsContext ctx) {
for (io.github.zenwave360.zdl.antlr.ZdlParser.Import_valueContext importValue : ctx.import_value()) {
model.appendToList("imports", getValueText(importValue.string()));
}
}
Expand Down Expand Up @@ -344,7 +344,10 @@ public void exitEnum(io.github.zenwave360.zdl.antlr.ZdlParser.EnumContext ctx) {
public void enterEnum_value(io.github.zenwave360.zdl.antlr.ZdlParser.Enum_valueContext ctx) {
var name = getText(ctx.enum_value_name());
var javadoc = javadoc(first(ctx.javadoc(), ctx.suffix_javadoc()));
var value = ctx.enum_value_value() != null? getValueText(ctx.enum_value_value().simple()) : null;
var value = getText(ctx.enum_value_value());
if(value != null) {
currentStack.peek().with("hasValue", true);
}
currentStack.peek().appendTo("values", name, new FluentMap()
.with("name", name)
.with("javadoc", javadoc)
Expand Down Expand Up @@ -427,13 +430,13 @@ private Object relationshipValidations(io.github.zenwave360.zdl.antlr.ZdlParser.
validations.with(name, Map.of("name", name, "value", true));
}
if (relationshipDefinitionContext.relationship_field_validations().relationship_field_min() != null) {
var name = "min";
var value = getText(relationshipDefinitionContext.relationship_field_validations().relationship_field_min());
var name = "minlength";
var value = getText(relationshipDefinitionContext.relationship_field_validations().relationship_field_min().relationship_field_value());
validations.with(name, Map.of("name", name, "value", value));
}
if (relationshipDefinitionContext.relationship_field_validations().relationship_field_max() != null) {
var name = "max";
var value = getText(relationshipDefinitionContext.relationship_field_validations().relationship_field_min());
var name = "maxlength";
var value = getText(relationshipDefinitionContext.relationship_field_validations().relationship_field_max().relationship_field_value());
validations.with(name, Map.of("name", name, "value", value));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

import static io.github.zenwave360.zdl.antlr.JSONPath.get;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ZdlListenerTest {

Expand Down Expand Up @@ -53,6 +55,10 @@ public void parseZdl_CompleteZdl() throws Exception {
assertEquals(5, get(model, "$.plugins", Map.of()).size());
assertEquals(3, get(model, "$.plugins.ZDLToAsyncAPIPlugin.config", Map.of()).size());

// ENUMS
assertFalse(get(model, "$.enums.OrderStatus.hasValue", false));
assertTrue(get(model, "$.enums.EnumWithValue.hasValue", false));

// ENTITIES
assertEquals(6, get(model, "$.entities", Map.of()).size());
assertEquals("CustomerOrder", get(model, "$.entities.CustomerOrder.name"));
Expand Down Expand Up @@ -92,8 +98,8 @@ public void parseZdl_CompleteZdl() throws Exception {
assertEquals("OrderItem", get(model, "$.entities.CustomerOrder.fields.orderItems.type"));
assertNull(get(model, "$.entities.CustomerOrder.fields.orderItems.initialValue"));
assertNull(get(model, "$.entities.CustomerOrder.fields.orderItems.validations.required"));
assertEquals("1", get(model, "$.entities.CustomerOrder.fields.orderItems.validations.min.value"));
assertEquals("200", get(model, "$.entities.CustomerOrder.fields.orderItems.validations.max.value"));
assertEquals("1", get(model, "$.entities.CustomerOrder.fields.orderItems.validations.minlength.value"));
assertEquals("200", get(model, "$.entities.CustomerOrder.fields.orderItems.validations.maxlength.value"));
assertEquals(false, get(model, "$.entities.CustomerOrder.fields.orderItems.isEnum"));
assertEquals(true, get(model, "$.entities.CustomerOrder.fields.orderItems.isEntity"));
assertEquals(true, get(model, "$.entities.CustomerOrder.fields.orderItems.isArray"));
Expand Down
10 changes: 7 additions & 3 deletions src/test/resources/complete.zdl
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ entity CustomerOrder {
/**
* orderItems javadoc
*/
orderItems OrderItem[] min(1) max(100) {
orderItems OrderItem[] minlength(1) maxlength(100) {
menuItemId String required
name String required
description String
price BigDecimal required
quantity Integer required
} max(200)
} maxlength(200)
}

@aggregate
Expand All @@ -129,6 +129,10 @@ enum OrderStatus {
ON_DELIVERY, DELIVERED, CANCELLED
}

enum EnumWithValue {
VALUE1(1), VALUE2(2)
}

// relationships don't make sense for documental databases like MongoDB
relationship ManyToOne {
/**
Expand All @@ -144,7 +148,7 @@ relationship OneToMany {
Customer{addresses(lastname)} to /** Address.customer javadoc */ Address{customer}
}
relationship OneToOne {
Customer{address} to @Id Address{customer}
Customer{address required minlength(1) maxlength(3)} to @Id Address{customer}
}
relationship ManyToMany {
Customer{addresses} to Address{customer}
Expand Down
Loading