Clean up inspection warnings

This commit is contained in:
Johan Maasing 2025-03-29 15:13:35 +01:00
parent ea99135dab
commit 66434a6bc1
6 changed files with 79 additions and 78 deletions

View file

@ -10,7 +10,6 @@
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>nu.zoom.dsl</groupId>
<artifactId>assembly</artifactId>
<dependencies>

View file

@ -10,8 +10,8 @@
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>nu.zoom.dsl</groupId>
<artifactId>parser</artifactId>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>

View file

@ -1,7 +1,5 @@
package nu.zoom.dsl.ast;
import java.util.Optional;
public record EndpointNode(
PathsNode paths,
String inputType) {

View file

@ -8,80 +8,81 @@ import java.util.ArrayList;
import java.util.List;
public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsParser.DocumentContext> {
private ArrayList<EndpointNode> endpoints = new ArrayList<>();
private ArrayList<ConfigItemNode> config = new ArrayList<>();
private ArrayList<CompoundTypeNode> dataTypes = new ArrayList<>();
public EndpointsVisitorTransformer() {
}
private final ArrayList<EndpointNode> endpoints = new ArrayList<>();
private final ArrayList<ConfigItemNode> config = new ArrayList<>();
private final ArrayList<CompoundTypeNode> dataTypes = new ArrayList<>();
public List<EndpointNode> getEndpoints() {
return List.copyOf(endpoints);
}
public EndpointsVisitorTransformer() {
}
public List<ConfigItemNode> getConfig() {
return List.copyOf(config);
}
public List<EndpointNode> getEndpoints() {
return List.copyOf(endpoints);
}
public List<CompoundTypeNode> getDataTypes() {
return List.copyOf(dataTypes);
}
public List<ConfigItemNode> getConfig() {
return List.copyOf(config);
}
@Override
public EndpointsParser.DocumentContext visitConfigitem(EndpointsParser.ConfigitemContext ctx) {
String configKey = ctx.configkey().IDENTIFIER().getText() ;
String configValue = getText(ctx.configvalue().IDENTIFIER(), ctx.configvalue().VALUE()) ;
this.config.add(new ConfigItemNode(configKey, configValue));
return super.visitConfigitem(ctx) ;
}
public List<CompoundTypeNode> getDataTypes() {
return List.copyOf(dataTypes);
}
@Override
public EndpointsParser.DocumentContext visitCompoundType(EndpointsParser.CompoundTypeContext ctx) {
this.dataTypes.add(extractCompoundTypeNode(ctx));
return super.visitCompoundType(ctx);
}
@Override
public EndpointsParser.DocumentContext visitConfigitem(EndpointsParser.ConfigitemContext ctx) {
String configKey = ctx.configkey().IDENTIFIER().getText();
String configValue = getText(ctx.configvalue().IDENTIFIER(), ctx.configvalue().VALUE());
this.config.add(new ConfigItemNode(configKey, configValue));
return super.visitConfigitem(ctx);
}
@Override
public EndpointsParser.DocumentContext visitEndpoint(EndpointsParser.EndpointContext ctx) {
List<String> segments = ctx
.path()
.pathSegment()
.stream()
.map(
segment -> getText(segment.IDENTIFIER(), segment.VALUE())
).toList() ;
TerminalNode typeReference = ctx.IDENTIFIER() ;
if (typeReference != null) {
var endpoint = new EndpointNode(new PathsNode(segments), typeReference.getText());
this.endpoints.add(endpoint);
} else {
var compoundTypeNode = extractCompoundTypeNode(ctx.compoundType()) ;
var endpoint = new EndpointNode(new PathsNode(segments), compoundTypeNode.name());
this.dataTypes.add(compoundTypeNode);
this.endpoints.add(endpoint);
}
return super.visitEndpoint(ctx);
}
@Override
public EndpointsParser.DocumentContext visitCompoundType(EndpointsParser.CompoundTypeContext ctx) {
this.dataTypes.add(extractCompoundTypeNode(ctx));
return super.visitCompoundType(ctx);
}
private CompoundTypeNode extractCompoundTypeNode(EndpointsParser.CompoundTypeContext ctx) {
String typeName = ctx.compoundTypeName().getText() ;
List<FieldNode> fields = extractTypeFields(ctx.compoundFields().compoundField()) ;
return new CompoundTypeNode(typeName, fields) ;
}
@Override
public EndpointsParser.DocumentContext visitEndpoint(EndpointsParser.EndpointContext ctx) {
List<String> segments = ctx
.path()
.pathSegment()
.stream()
.map(
segment -> getText(segment.IDENTIFIER(), segment.VALUE())
).toList();
TerminalNode typeReference = ctx.IDENTIFIER();
if (typeReference != null) {
var endpoint = new EndpointNode(new PathsNode(segments), typeReference.getText());
this.endpoints.add(endpoint);
} else {
var compoundTypeNode = extractCompoundTypeNode(ctx.compoundType());
var endpoint = new EndpointNode(new PathsNode(segments), compoundTypeNode.name());
this.dataTypes.add(compoundTypeNode);
this.endpoints.add(endpoint);
}
return super.visitEndpoint(ctx);
}
private List<FieldNode> extractTypeFields(List<EndpointsParser.CompoundFieldContext> compoundFieldContexts) {
return compoundFieldContexts.stream().map(
ctx -> new FieldNode(
ctx.fieldName().getText(),
ctx.fieldType().getText()
)
).toList() ;
}
private CompoundTypeNode extractCompoundTypeNode(EndpointsParser.CompoundTypeContext ctx) {
String typeName = ctx.compoundTypeName().getText();
List<FieldNode> fields = extractTypeFields(ctx.compoundFields().compoundField());
return new CompoundTypeNode(typeName, fields);
}
// Concatenate the text from to terminal nodes. Useful for contexts that are either an identifier or a value,
// and you just want the text from whichever is not null.
private String getText(TerminalNode identifier, TerminalNode value) {
return
identifier != null ? identifier.getText() : ""
+ value != null ? value.getText() : "" ;
}
private List<FieldNode> extractTypeFields(List<EndpointsParser.CompoundFieldContext> compoundFieldContexts) {
return compoundFieldContexts.stream().map(
ctx -> new FieldNode(
ctx.fieldName().getText(),
ctx.fieldType().getText()
)
).toList();
}
// Concatenate the text from to terminal nodes. Useful for contexts that are either an identifier or a value,
// and you just want the text from whichever is not null.
private String getText(TerminalNode identifier, TerminalNode value) {
return
((identifier != null) ? identifier.getText() : "") +
((value != null) ? value.getText() : "");
}
}

View file

@ -7,7 +7,6 @@ import org.antlr.v4.runtime.CommonTokenStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class ParserWrapper {

View file

@ -19,16 +19,20 @@ import java.util.concurrent.Callable;
description = "Generate source code from an endpoints specification file."
)
public class EndpointsCLI implements Callable<Integer> {
@Parameters(index = "0", description = "The source endpoints DSL file.")
@SuppressWarnings("unused")
@Parameters(index = "0", description = "The source endpoints DSL file.")
private Path file;
@Option(names = {"-t", "--template"}, description = "The template directory. Default is ~/endpoints-templates")
@SuppressWarnings("CanBeFinal")
@Option(names = {"-t", "--template"}, description = "The template directory. Default is ~/endpoints-templates")
private Path templateDir = Paths.get(System.getProperty("user.dir"), "endpoints-templates");
@Option(names = {"-o", "--output"}, description = "The directory to write the generated code to. Default is ~/endpoints-output")
@SuppressWarnings("CanBeFinal")
@Option(names = {"-o", "--output"}, description = "The directory to write the generated code to. Default is ~/endpoints-output")
private Path outputDir = Paths.get(System.getProperty("user.dir"), "endpoints-output");
@Option(names = {"-v", "--verbose"}, description = "Print verbose debug messages.")
@SuppressWarnings("unused")
@Option(names = {"-v", "--verbose"}, description = "Print verbose debug messages.")
private Boolean verbose = false;
public static void main(String[] args) {
@ -60,13 +64,13 @@ public class EndpointsCLI implements Callable<Integer> {
}
}
private void validateTemplateDirectory() throws IOException {
private void validateTemplateDirectory() {
if (!Files.isDirectory(this.templateDir)) {
throw new IllegalArgumentException("Template directory '" + this.templateDir + "' is not a directory.");
}
}
private void validateInputFile() throws IOException {
private void validateInputFile() {
if (Files.notExists(this.file)) {
throw new IllegalArgumentException("Input file '" + this.file + "' does not exist.");
}