Skip to content

Commit

Permalink
adds policies
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Sep 17, 2023
1 parent 637a567 commit 9cd6dbd
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 37 deletions.
11 changes: 10 additions & 1 deletion src/main/antlr4/io.github.zenwave360.zdl.antlr/Zdl.g4
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ OPTIONAL: '?';
CONFIG: 'config';
APIS: 'apis';
PLUGINS: 'plugins';
POLICIES: 'policies';
DISABLED: 'disabled';
ASYNCAPI: 'asyncapi';
OPENAPI: 'openapi';
Expand Down Expand Up @@ -82,6 +83,7 @@ OPTION_NAME: '@' [a-zA-Z_][a-zA-Z0-9_]*;

fragment DIGIT : [0-9] ;
ID: [a-zA-Z_][a-zA-Z0-9_.]*;
POLICY_ID: [a-zA-Z_][a-zA-Z0-9_-]*;
INT: DIGIT+ ;
NUMBER: DIGIT+ ([.] DIGIT+)? ;

Expand Down Expand Up @@ -113,7 +115,7 @@ PATTERN_REGEX: '/' .*? '/' ; // TODO: improve regex
ERRCHAR: . -> channel(HIDDEN);
// Rules
zdl: global_javadoc? legacy_constants config? apis? (entity | enum | input | output | event | relationships | service | service_legacy)* EOF;
zdl: global_javadoc? legacy_constants config? apis? (policies | entity | enum | input | output | event | relationships | service | service_legacy)* EOF;
global_javadoc: JAVADOC;
javadoc: JAVADOC;
suffix_javadoc: JAVADOC;
Expand Down Expand Up @@ -157,6 +159,13 @@ plugin_config: plugin_config_cli_option | plugin_config_option;
plugin_config_option: field_name complex_value;
plugin_config_cli_option: '--' keyword (EQUALS simple)?;
policies: POLICIES (LPAREN policy_aggregate RPAREN)? policies_body;
policy_aggregate: ID;
policies_body: LBRACE policie_body* RBRACE;
policie_body: policie_name policie_value;
policie_name: ID | POLICY_ID;
policie_value: simple;
// @options
annotations: option*;
option: option_name (LPAREN option_value RPAREN)?; // (LPAREN option_value RPAREN)? | '@' option_name (LPAREN option_value RPAREN)?;
Expand Down
26 changes: 26 additions & 0 deletions src/main/antlr4/io.github.zenwave360.zdl.antlr/json-objects.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
object
: '{' key_value* '}'
;

key_value
: key '=' value
;

key
: SPECIFIC_VALUE
| BOOL
// More tokens that can be a key?
;

value
: object
| array
| BOOL
| STRING
| NUMBER
| SPECIFIC_VALUE
;

array
: '[' value+ ']'
;
67 changes: 39 additions & 28 deletions src/main/java/io/github/zenwave360/zdl/antlr/ZdlListenerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void enterSuffix_javadoc(io.github.zenwave360.zdl.antlr.ZdlParser.Suffix_

@Override
public void enterGlobal_javadoc(io.github.zenwave360.zdl.antlr.ZdlParser.Global_javadocContext ctx) {
var javadoc = getText(ctx);
model.put("javadoc", javadoc(javadoc));
model.put("javadoc", javadoc(ctx));
}

@Override
Expand All @@ -71,7 +70,7 @@ public void enterApi(io.github.zenwave360.zdl.antlr.ZdlParser.ApiContext ctx) {
var name = ctx.api_name().getText();
var type = ctx.api_type().getText();
var role = ctx.api_role().getText();
var javadoc = getText(ctx.javadoc());
var javadoc = javadoc(ctx.javadoc());
currentStack.push(new FluentMap()
.with("name", name)
.with("type", type)
Expand All @@ -95,11 +94,20 @@ public void exitApi(io.github.zenwave360.zdl.antlr.ZdlParser.ApiContext ctx) {
currentStack.pop();
}

@Override
public void enterPolicie_body(ZdlParser.Policie_bodyContext ctx) {
var name = getText(ctx.policie_name());
var value = ctx.policie_value() != null? getValueText(ctx.policie_value().simple()) : null;
var aggregate = ((ZdlParser.PoliciesContext) ctx.getParent().getParent()).policy_aggregate();
model.appendTo("policies", new FluentMap().with(name, new FluentMap().with("name", name).with("value", value).with("aggregate", aggregate)));
super.enterPolicie_body(ctx);
}

@Override
public void enterEntity(io.github.zenwave360.zdl.antlr.ZdlParser.EntityContext ctx) {
var entity = ctx.entity_definition();
var name = entity.entity_name().getText();
var javadoc = getText(ctx.javadoc());
var javadoc = javadoc(ctx.javadoc());
var tableName = getText(entity.entity_table_name());
currentStack.push(processEntity(name, javadoc, tableName).with("type", "entity"));
model.appendTo("entities", name, currentStack.peek());
Expand Down Expand Up @@ -130,7 +138,7 @@ private FluentMap processEntity(String name, String javadoc, String tableName) {
.with("instanceNamePlural", pluralize(instanceName))
.with("kebabCase", kebabCase)
.with("kebabCasePlural", pluralize(kebabCase))
.with("javadoc", javadoc(javadoc))
.with("javadoc", javadoc)
.with("options", new FluentMap())
.with("fields", new FluentMap())
;
Expand All @@ -152,16 +160,16 @@ public void enterOption(io.github.zenwave360.zdl.antlr.ZdlParser.OptionContext c
public void enterField(io.github.zenwave360.zdl.antlr.ZdlParser.FieldContext ctx) {
var name = getText(ctx.field_name());
var type = ctx.field_type() != null && ctx.field_type().ID() != null? ctx.field_type().ID().getText() : null;
var javadoc = first(getText(ctx.javadoc(), getText(ctx.suffix_javadoc())));
var javadoc = javadoc(first(ctx.javadoc(), ctx.suffix_javadoc()));
var isEnum = false; // TODO
var isEntity = false; // TODO
var isArray = ctx.field_type().ARRAY() != null;
var validations = processFieldValidations(ctx.field_validations());
currentStack.peek().appendTo("fields", name, new FluentMap()
.with("name", name)
.with("type", type)
.with("javadoc", javadoc(javadoc))
.with("comment", javadoc(javadoc))
.with("javadoc", javadoc)
.with("comment", javadoc)
.with("isEnum", isEnum)
.with("isEntity", isEntity)
.with("isArray", isArray)
Expand All @@ -182,6 +190,9 @@ private Map<String, Object> processFieldValidations(List<io.github.zenwave360.zd
field_validations.forEach(v -> {
var name = getText(v.field_validation_name());
var value = first(getText(v.field_validation_value()), "");
if("pattern".equals(name) && value != null) {
value = value.substring(1, value.length() - 2);
}
validations.with(name, Map.of("name", name, "value", value));
});
}
Expand All @@ -200,7 +211,7 @@ public void enterNested_field(io.github.zenwave360.zdl.antlr.ZdlParser.Nested_fi
var parentEntityFields = ((FluentMap) parentEntity.get("fields"));
var parentField = new ArrayList<>(parentEntityFields.values()).get(parentEntityFields.size() - 1);
String entityName = parent.field_type().ID().getText();
String entityJavadoc = getText(parent.javadoc());
String entityJavadoc = javadoc(parent.javadoc());
String tableName = getText(parent.entity_table_name());
var validations = processNestedFieldValidations(ctx.nested_field_validations());
((Map)parentField).put("validations", validations);
Expand Down Expand Up @@ -229,13 +240,13 @@ public void exitNested_field(io.github.zenwave360.zdl.antlr.ZdlParser.Nested_fie
@Override
public void enterEnum(io.github.zenwave360.zdl.antlr.ZdlParser.EnumContext ctx) {
var name = getText(ctx.enum_name());
var javadoc = getText(ctx.javadoc());
var javadoc = javadoc(ctx.javadoc());
currentStack.push(new FluentMap()
.with("name", name)
.with("type", "enums")
.with("className", camelCase(name))
.with("javadoc", javadoc(javadoc))
.with("comment", javadoc(javadoc)));
.with("javadoc", javadoc)
.with("comment", javadoc));
((FluentMap) model.get("enums")).appendTo("enums", name, currentStack.peek());

var entityLocation = "enums.enums." + name;
Expand All @@ -252,12 +263,12 @@ public void exitEnum(io.github.zenwave360.zdl.antlr.ZdlParser.EnumContext ctx) {
@Override
public void enterEnum_value(io.github.zenwave360.zdl.antlr.ZdlParser.Enum_valueContext ctx) {
var name = getText(ctx.enum_value_name());
var javadoc = first(getText(ctx.javadoc(), getText(ctx.suffix_javadoc())));
var javadoc = javadoc(first(ctx.javadoc(), ctx.suffix_javadoc()));
var value = ctx.enum_value_value() != null? getValueText(ctx.enum_value_value().simple()) : null;
currentStack.peek().appendTo("values", name, new FluentMap()
.with("name", name)
.with("javadoc", javadoc(javadoc))
.with("comment", javadoc(javadoc))
.with("javadoc", javadoc)
.with("comment", javadoc)
.with("value", value)
);
}
Expand All @@ -269,20 +280,20 @@ public void enterRelationship(io.github.zenwave360.zdl.antlr.ZdlParser.Relations

var from = getText(ctx.relationship_from().relationship_definition().relationship_entity_name());
var fromField = getText(ctx.relationship_from().relationship_definition().relationship_field_name());
var commentInFrom = getText(ctx.relationship_from().javadoc());
var commentInFrom = javadoc(ctx.relationship_from().javadoc());
var fromOptions = relationshipOptions(ctx.relationship_from().annotations().option());

var to = getText(ctx.relationship_to().relationship_definition().relationship_entity_name());
var toField = getText(ctx.relationship_to().relationship_definition().relationship_field_name());
var commentInTo = getText(ctx.relationship_to().javadoc());
var commentInTo = javadoc(ctx.relationship_to().javadoc());
var toOptions = relationshipOptions(ctx.relationship_to().annotations().option());

var relationship = new FluentMap()
.with("type", relationshipType)
.with("from", from)
.with("to", to)
.with("commentInFrom", javadoc(commentInFrom))
.with("commentInTo", javadoc(commentInTo))
.with("commentInFrom", commentInFrom)
.with("commentInTo", commentInTo)
.appendTo("options", "source", fromOptions)
.appendTo("options", "destination", toOptions)
.with("injectedFieldInFrom", fromField) // FIXME review this
Expand All @@ -301,13 +312,13 @@ private Map<String, Object> relationshipOptions(List<io.github.zenwave360.zdl.an
@Override
public void enterService_legacy(io.github.zenwave360.zdl.antlr.ZdlParser.Service_legacyContext ctx) {
var serviceName = ctx.ID().getText();
String serviceJavadoc = null; // getText(ctx.javadoc());
String serviceJavadoc = "Legacy service";
var serviceAggregates = ctx.service_aggregates() != null? Arrays.asList(ctx.service_aggregates().getText().split(",")) : null;
currentStack.push(new FluentMap()
.with("name", serviceName)
.with("isLegacy", true)
.with("className", camelCase(serviceName))
.with("javadoc", javadoc(serviceJavadoc))
.with("javadoc", serviceJavadoc)
.with("aggregates", serviceAggregates)
.with("methods", createCRUDMethods(serviceAggregates))
);
Expand All @@ -322,12 +333,12 @@ public void exitService_legacy(io.github.zenwave360.zdl.antlr.ZdlParser.Service_
@Override
public void enterService(io.github.zenwave360.zdl.antlr.ZdlParser.ServiceContext ctx) {
var serviceName = ctx.ID().getText();
var serviceJavadoc = getText(ctx.javadoc());
var serviceJavadoc = javadoc(ctx.javadoc());
var serviceAggregates = ctx.service_aggregates() != null? Arrays.asList(ctx.service_aggregates().getText().split(",")) : null;
currentStack.push(new FluentMap()
.with("name", serviceName)
.with("className", camelCase(serviceName))
.with("javadoc", javadoc(serviceJavadoc))
.with("javadoc", serviceJavadoc)
.with("aggregates", serviceAggregates)
.with("methods", new FluentMap())
);
Expand All @@ -348,7 +359,7 @@ public void enterService_method(io.github.zenwave360.zdl.antlr.ZdlParser.Service
var returnTypeIsArray = ctx.service_method_return() != null? ctx.service_method_return().ARRAY() != null : null;
var returnTypeIsOptional = ctx.service_method_return() != null? ctx.service_method_return().OPTIONAL() != null : null;
var withEvents = getServiceMethodEvents(ctx.service_method_with_events());
var javadoc = first(getText(ctx.javadoc(), getText(ctx.suffix_javadoc())));
var javadoc = javadoc(first(ctx.javadoc(), ctx.suffix_javadoc()));

var method = new FluentMap()
.with("name", methodName)
Expand Down Expand Up @@ -388,13 +399,13 @@ private List<Object> getServiceMethodEvents(io.github.zenwave360.zdl.antlr.ZdlPa
@Override
public void enterEvent(io.github.zenwave360.zdl.antlr.ZdlParser.EventContext ctx) {
var name = ctx.event_name().getText();
var javadoc = getText(ctx.javadoc());
var javadoc = javadoc(ctx.javadoc());
var kebabCase = kebabCase(name);
currentStack.push(new FluentMap()
.with("name", name)
.with("type", "events")
.with("kebabCase", kebabCase)
.with("javadoc", javadoc(javadoc))
.with("javadoc", javadoc)
.with("fields", new FluentMap())
);
model.appendTo("events", name, currentStack.peek());
Expand All @@ -419,7 +430,7 @@ public void exitEvent(io.github.zenwave360.zdl.antlr.ZdlParser.EventContext ctx)
@Override
public void enterInput(io.github.zenwave360.zdl.antlr.ZdlParser.InputContext ctx) {
var name = ctx.input_name().getText();
var javadoc = getText(ctx.javadoc());
var javadoc = javadoc(ctx.javadoc());
currentStack.push(processEntity(name, javadoc, null).with("type", "inputs"));
model.appendTo("inputs", name, currentStack.peek());
currentCollection = "inputs";
Expand All @@ -433,7 +444,7 @@ public void exitInput(io.github.zenwave360.zdl.antlr.ZdlParser.InputContext ctx)
@Override
public void enterOutput(io.github.zenwave360.zdl.antlr.ZdlParser.OutputContext ctx) {
var name = ctx.output_name().getText();
var javadoc = getText(ctx.javadoc());
var javadoc = javadoc(ctx.javadoc());
currentStack.push(processEntity(name, javadoc, null).with("type", "outputs"));
model.appendTo("outputs", name, currentStack.peek());
currentCollection = "outputs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ static String javadoc(Object javadoc) {
if (javadoc == null) {
return null;
}
return javadoc.toString()
return getText((ParserRuleContext) javadoc)
.replaceAll("^/\\*\\*", "")
.replaceAll("\\*/$", "")
.replaceAll("^\\s+\\* ", "")
.replaceAll("\\*/\\s*$", "")
.replaceAll("^\\s*\\* ", "")
.trim();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public void parseZdl_Simple() throws Exception {
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model));
}

@Test
public void parseZdl_Policies() throws Exception {

ZdlModel model = parseZdl("src/test/resources/policies.zdl");

System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model));
}

@Test
public void parseZdl_NestedFields() throws Exception {

Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/policies.zdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

policies {
somename_2 "this is the body of the policy 2"

some-3 "
this is the body of the policy 3
"

some-4
"this is the body of the policy 4"
}
12 changes: 7 additions & 5 deletions src/test/resources/simple.zdl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ apis {
entity Customer {
username String required unique min(5)
tags String[]
email String required unique max(MAX_LENGTH)
email String required unique max(MAX_LENGTH) pattern(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)
}

/**
Expand Down Expand Up @@ -62,7 +62,9 @@ entity CustomerEntityInput {
//}

relationship ManyToOne {
/** shipment details */
/**
* shipment details javadoc comment
*/
ShippingDetails{customer} to
/** customer */
Customer{shipmentDetails}
Expand Down Expand Up @@ -128,13 +130,13 @@ service CustomerService for (Customer) {
}

@topic(tratra)
event CustomerCreated (customerEvents) {
event CustomerCreated {
customerId String
customer Customer
}

@asyncapi({ api: 'BASA', operationId: 'createCustomer', topic: 'tratra' })
event CustomerUpdated(customerEvents) {
event CustomerUpdated {
customerId String
customer CustomerPayload {
username String
Expand All @@ -150,6 +152,6 @@ event CustomerDeleted {


@entity(Customer)
event CustomerEvent(customerEvent) {
event CustomerEvent {

}

0 comments on commit 9cd6dbd

Please sign in to comment.