Skip to content

Commit

Permalink
adds support for 'required' in relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Feb 24, 2024
1 parent aedd8e5 commit 8a0a00c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/antlr4/io.github.zenwave360.zdl.antlr/Zdl.g4
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ relationship_type: MANY_TO_MANY | MANY_TO_ONE| ONE_TO_MANY | ONE_TO_ONE;
relationship: relationship_from TO relationship_to;
relationship_from: javadoc? annotations relationship_definition;
relationship_to: javadoc? annotations relationship_definition;
relationship_definition: relationship_entity_name (LBRACE relationship_field_name relationship_description_field? relationship_field_required? RBRACE)?;
relationship_definition: relationship_entity_name (LBRACE relationship_field_name (LPAREN relationship_description_field RPAREN)? relationship_field_required? RBRACE)?;
relationship_entity_name: ID;
relationship_field_name: keyword;
relationship_description_field: LPAREN ID RPAREN;
relationship_description_field: ID;
relationship_field_required: REQUIRED;
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/io/github/zenwave360/zdl/antlr/ZdlListenerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public void enterEnum_value(io.github.zenwave360.zdl.antlr.ZdlParser.Enum_valueC
public void enterRelationship(io.github.zenwave360.zdl.antlr.ZdlParser.RelationshipContext ctx) {
var parent = (io.github.zenwave360.zdl.antlr.ZdlParser.RelationshipsContext) ctx.parent;
var relationshipType = parent.relationship_type().getText();
var relationshipName = removeJavadoc(relationshipType + "_" + ctx.relationship_from().getText() + "_" + ctx.relationship_to().getText());
var relationshipName = removeJavadoc(relationshipType + "_" + relationshipDescription(ctx.relationship_from().relationship_definition()) + "_" + relationshipDescription(ctx.relationship_to().relationship_definition()));

var relationship = new FluentMap().with("type", relationshipType).with("name", relationshipName);
var location = "relationships." + relationshipName;
Expand All @@ -357,27 +357,33 @@ public void enterRelationship(io.github.zenwave360.zdl.antlr.ZdlParser.Relations
var fromField = getText(ctx.relationship_from().relationship_definition().relationship_field_name());
var commentInFrom = javadoc(ctx.relationship_from().javadoc());
var fromOptions = relationshipOptions(ctx.relationship_from().annotations().option());
var isInjectedFieldInFromRequired = ctx.relationship_from().relationship_definition().relationship_field_required() != null;
var injectedFieldInFromDescription = getText(ctx.relationship_from().relationship_definition().relationship_description_field());
model.setLocation(location + ".from.entity", getLocations(ctx.relationship_from().relationship_definition().relationship_entity_name()));
model.setLocation(location + ".from.field", getLocations(ctx.relationship_from().relationship_definition().relationship_field_name()));
relationship.with("from", from)
.with("commentInFrom", commentInFrom)
.with("injectedFieldInFrom", fromField)
.with("fromOptions", fromOptions)
.with("isInjectedFieldInFromRequired", false); // FIXME review this
.with("injectedFieldInFromDescription", injectedFieldInFromDescription)
.with("isInjectedFieldInFromRequired", isInjectedFieldInFromRequired);
}

if(ctx.relationship_to() != null && ctx.relationship_to().relationship_definition() != null) {
var to = getText(ctx.relationship_to().relationship_definition().relationship_entity_name());
var toField = getText(ctx.relationship_to().relationship_definition().relationship_field_name());
var commentInTo = javadoc(ctx.relationship_to().javadoc());
var toOptions = relationshipOptions(ctx.relationship_to().annotations().option());
var isInjectedFieldInToRequired = ctx.relationship_to().relationship_definition().relationship_field_required() != null;
var injectedFieldInToDescription = getText(ctx.relationship_to().relationship_definition().relationship_description_field());
model.setLocation(location + ".to.entity", getLocations(ctx.relationship_to().relationship_definition().relationship_entity_name()));
model.setLocation(location + ".to.field", getLocations(ctx.relationship_to().relationship_definition().relationship_field_name()));
relationship.with("to", to)
.with("commentInTo", commentInTo)
.with("injectedFieldInTo", toField)
.with("toOptions", toOptions)
.with("isInjectedFieldInToRequired", false); // FIXME review this
.with("injectedFieldInToDescription", injectedFieldInToDescription)
.with("isInjectedFieldInToRequired", isInjectedFieldInToRequired);
}

model.getRelationships().appendTo(relationshipType, relationshipName, relationship);
Expand All @@ -390,6 +396,17 @@ private String removeJavadoc(String text) {
return text.replaceAll(regex, "");
}

private String relationshipDescription(ZdlParser.Relationship_definitionContext ctx) {
var description = "";
if(ctx != null) {
description = description + getText(ctx.relationship_entity_name());
if (ctx.relationship_field_name() != null) {
description = description + "{" + getText(ctx.relationship_field_name()) + "}";
}
}
return description;
}

private Map<String, Object> relationshipOptions(List<io.github.zenwave360.zdl.antlr.ZdlParser.OptionContext> options) {
return options.stream().collect(Collectors.toMap(o ->
getText(o.option_name()).replace("@", ""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ public void parseZdl_CompleteZdl() throws Exception {
assertEquals("customer", get(model, "$.relationships.ManyToOne.ManyToOne_Address{customer}_Customer.injectedFieldInFrom"));
assertNull(get(model, "$.relationships.ManyToOne.ManyToOne_Address{customer}_Customer.injectedFieldInTo"));

assertEquals(true, get(model, "$.relationships.ManyToOne.ManyToOne_Address{customer2}_Customer.isInjectedFieldInFromRequired"));
assertEquals(false, get(model, "$.relationships.ManyToOne.ManyToOne_Address{customer2}_Customer.isInjectedFieldInToRequired"));

assertEquals("lastname", get(model, "$.relationships.OneToMany.OneToMany_Customer{addresses}_Address{customer}.injectedFieldInFromDescription"));
assertEquals("Address.customer javadoc", get(model, "$.relationships.OneToMany.OneToMany_Customer{addresses}_Address{customer}.commentInTo"));

assertEquals("Customer", get(model, "$.relationships.OneToOne.OneToOne_Customer{address}_@IdAddress{customer}.from"));
assertEquals(true, get(model, "$.relationships.OneToOne.OneToOne_Customer{address}_@IdAddress{customer}.toOptions.Id"));
assertEquals("Customer", get(model, "$.relationships.OneToOne.OneToOne_Customer{address}_Address{customer}.from"));
assertEquals(true, get(model, "$.relationships.OneToOne.OneToOne_Customer{address}_Address{customer}.toOptions.Id"));


// SERVICES
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/complete.zdl
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ relationship ManyToOne {
Address{customer} to Customer
}
relationship ManyToOne {
Address{customer2} to Customer
Address{customer3} to Customer
Address{customer2 required} to Customer
Address{customer3(lastname) required} to Customer
}
relationship OneToMany {
Customer{addresses} to /** Address.customer javadoc */ Address{customer}
Customer{addresses(lastname)} to /** Address.customer javadoc */ Address{customer}
}
relationship OneToOne {
Customer{address} to @Id Address{customer}
Expand Down

0 comments on commit 8a0a00c

Please sign in to comment.