Transformer that gives anonymous datatype the name of the last path segment

This commit is contained in:
Johan Maasing 2025-04-08 19:21:13 +02:00
parent 2a46a92630
commit 67eb11ae3e
5 changed files with 33 additions and 7 deletions

View file

@ -6,7 +6,7 @@ configkey : IDENTIFIER ;
configvalue : (IDENTIFIER|VALUE) ;
namedTypeDeclaration : typeName typeDeclaration ;
typeName : IDENTIFIER ;
typeDeclaration : typeName? '(' typeField (',' typeField)* ')' ;
typeDeclaration : '(' typeField (',' typeField)* ')' ;
typeField : fieldName ':' fieldType ;
fieldName : IDENTIFIER ;
fieldType : IDENTIFIER ;

View file

@ -56,8 +56,24 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
var endpoint = new EndpointNode(new PathsNode(segments), typeReference.getText());
this.endpoints.add(endpoint);
} else {
var compoundTypeNode = extractCompoundTypeNode(ctx.requestBody().namedTypeDeclaration());
var endpoint = new EndpointNode(new PathsNode(segments), compoundTypeNode.name().orElseThrow());
EndpointsParser.RequestBodyContext requestBodyContext = ctx.requestBody() ;
String requestTypeName = null ;
// request can be either a named type declaration, an anonymous type declaration or a type reference
if (requestBodyContext.namedTypeDeclaration() != null) {
var compoundTypeNode = extractCompoundTypeNode(ctx.requestBody().namedTypeDeclaration());
requestTypeName = compoundTypeNode.name() ;
this.dataTypes.add(compoundTypeNode);
} else if (requestBodyContext.typeDeclaration() != null) {
var typeFields = extractTypeFields(requestBodyContext.typeDeclaration().typeField()) ;
requestTypeName = segments.getLast() + "Request" ;
this.dataTypes.add(new TypeNode(requestTypeName, typeFields));
} else if (requestBodyContext.IDENTIFIER() != null) {
requestTypeName = requestBodyContext.IDENTIFIER().getText() ;
}
if (requestTypeName == null) {
throw new ParseException("Unable to create the request body data type" + ctx.toStringTree()) ;
}
var endpoint = new EndpointNode(new PathsNode(segments), requestTypeName);
this.endpoints.add(endpoint);
}
}
@ -65,7 +81,7 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
private TypeNode extractCompoundTypeNode(EndpointsParser.NamedTypeDeclarationContext ctx) {
String typeName = ctx.typeName().getText();
List<FieldNode> fields = extractTypeFields(ctx.typeDeclaration().typeField());
return new TypeNode(Optional.of(typeName), fields);
return new TypeNode(typeName, fields);
}
private List<FieldNode> extractTypeFields(List<EndpointsParser.TypeFieldContext> compoundFieldContexts) {

View file

@ -0,0 +1,7 @@
package nu.zoom.dsl.ast;
public class ParseException extends RuntimeException {
public ParseException(String message) {
super(message);
}
}

View file

@ -3,5 +3,5 @@ package nu.zoom.dsl.ast;
import java.util.List;
import java.util.Optional;
public record TypeNode(Optional<String> name, List<FieldNode> fields) {
public record TypeNode(String name, List<FieldNode> fields) {
}

View file

@ -1,2 +1,5 @@
/some/endpoint =< SomeType(foo:String)
/some/other/endpoint =< SomeOtherType(bar:Seq[Embedded])
AType(data: List[String])
/some/endpoint <- SomeType(foo:String)
/some/other/endpoint <- SomeOtherType(bar:Seq[Embedded])
/yet/other/endpoint <- (bar:Seq[Embedded])