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> <version>1.0-SNAPSHOT</version>
</parent> </parent>
<groupId>nu.zoom.dsl</groupId>
<artifactId>assembly</artifactId> <artifactId>assembly</artifactId>
<dependencies> <dependencies>

View file

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

View file

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

View file

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

View file

@ -19,16 +19,20 @@ import java.util.concurrent.Callable;
description = "Generate source code from an endpoints specification file." description = "Generate source code from an endpoints specification file."
) )
public class EndpointsCLI implements Callable<Integer> { 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; 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"); 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"); 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; private Boolean verbose = false;
public static void main(String[] args) { 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)) { if (!Files.isDirectory(this.templateDir)) {
throw new IllegalArgumentException("Template directory '" + this.templateDir + "' is not a directory."); throw new IllegalArgumentException("Template directory '" + this.templateDir + "' is not a directory.");
} }
} }
private void validateInputFile() throws IOException { private void validateInputFile() {
if (Files.notExists(this.file)) { if (Files.notExists(this.file)) {
throw new IllegalArgumentException("Input file '" + this.file + "' does not exist."); throw new IllegalArgumentException("Input file '" + this.file + "' does not exist.");
} }