You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2022/05/13 15:19:50 UTC
[plc4x] 01/02: feat(codegen): changed validation to fail parsing conditionally
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit c21a1842f2dd9f0d4755bd634e1a38acc48b24bc
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri May 13 17:19:10 2022 +0200
feat(codegen): changed validation to fail parsing conditionally
+ added option to switch between failing and non failing behavior
---
.../templates/go/complex-type-template.go.ftlh | 4 ++-
.../templates/java/complex-type-template.java.ftlh | 4 ++-
.../plugins/codegenerator/language/mspec/MSpec.g4 | 2 +-
.../mspec/model/fields/DefaultValidationField.java | 12 ++++++--
.../mspec/parser/MessageFormatListener.java | 6 +++-
.../spi/generation/ParseValidationException.java | 33 ++++++++++++++++++++++
6 files changed, 55 insertions(+), 6 deletions(-)
diff --git a/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh b/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
index dc2255aea5..3cdb2e54f7 100644
--- a/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
+++ b/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
@@ -1362,7 +1362,9 @@ func ${type.name}Parse(readBuffer utils.ReadBuffer<#if hasParserArguments>, ${pa
// Validation
if (!(${helper.toParseExpression(validationField, helper.boolTypeReference, validationField.getValidationExpression(), null)})) {
- return nil, utils.ParseAssertError{${validationField.getDescription().orElse("Validation failed")}}
+ <#assign errorType="ParseValidationError">
+ <#if !validationField.shouldFail()><#assign errorType="ParseAssertError"></#if>
+ return nil, utils.${errorType}{${validationField.getDescription().orElse("Validation failed")}}
}
<#break>
<#case "peek">
diff --git a/code-generation/language-java/src/main/resources/templates/java/complex-type-template.java.ftlh b/code-generation/language-java/src/main/resources/templates/java/complex-type-template.java.ftlh
index 237aee2aca..589e1e8a97 100644
--- a/code-generation/language-java/src/main/resources/templates/java/complex-type-template.java.ftlh
+++ b/code-generation/language-java/src/main/resources/templates/java/complex-type-template.java.ftlh
@@ -757,7 +757,9 @@ public<#if type.isDiscriminatedParentTypeDefinition()> abstract</#if> class ${ty
<#assign validationField = field.asValidationField().orElseThrow()>
// Validation
if (!(${helper.toParseExpression(validationField, helper.boolTypeReference, validationField.getValidationExpression(), null)})) {
- throw new ParseAssertException(${validationField.getDescription().orElse("Validation failed")});
+ <#assign errorType="ParseValidationException">
+ <#if !validationField.shouldFail()><#assign errorType="ParseAssertException"></#if>
+ throw new ${errorType}(${validationField.getDescription().orElse("Validation failed")});
}
<#break>
<#case "peek">
diff --git a/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4 b/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
index 332d3b2e21..ddbc405da1 100644
--- a/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
+++ b/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
@@ -137,7 +137,7 @@ virtualField
;
validationField
- : 'validation' validationExpression=expression (description=STRING_LITERAL)?
+ : 'validation' validationExpression=expression (description=STRING_LITERAL)? ('shouldFail='shouldFail=BOOLEAN_LITERAL)?
;
peekField
diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultValidationField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultValidationField.java
index 14db0e8771..75cbdcb8d2 100644
--- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultValidationField.java
+++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultValidationField.java
@@ -30,9 +30,12 @@ public class DefaultValidationField implements ValidationField, Field {
private final Term validationExpression;
private final String description;
- public DefaultValidationField(Term validationExpression, String description) {
+ private final boolean shouldFail;
+
+ public DefaultValidationField(Term validationExpression, String description, boolean shouldFail) {
this.validationExpression = Objects.requireNonNull(validationExpression);
- this.description=description;
+ this.description = description;
+ this.shouldFail = shouldFail;
}
@Override
@@ -45,6 +48,11 @@ public class DefaultValidationField implements ValidationField, Field {
return Optional.ofNullable(description);
}
+ @Override
+ public boolean shouldFail() {
+ return shouldFail;
+ }
+
// TODO: dummy implementation as this is not really a field
@Override
public String getTypeName() {
diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
index e43e6fdb93..579e6e7b11 100644
--- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
+++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
@@ -480,7 +480,11 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType
@Override
public void enterValidationField(MSpecParser.ValidationFieldContext ctx) {
Term validationExpression = getExpressionTerm(ctx.validationExpression);
- Field field = new DefaultValidationField(validationExpression, ctx.description.getText());
+ boolean shouldFail = true;
+ if (ctx.shouldFail!=null){
+ shouldFail = "true".equalsIgnoreCase(ctx.shouldFail.getText());
+ }
+ Field field = new DefaultValidationField(validationExpression, ctx.description.getText(), shouldFail);
if (parserContexts.peek() != null) {
parserContexts.peek().add(field);
}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ParseValidationException.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ParseValidationException.java
new file mode 100644
index 0000000000..af09dbd64c
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ParseValidationException.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.plc4x.java.spi.generation;
+
+/**
+ * Is thrown when an validation field in mspec says no.
+ */
+public class ParseValidationException extends ParseException {
+ public ParseValidationException(String message) {
+ super(message);
+ }
+
+ public ParseValidationException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}