Transformer that gives anonymous datatype the name of the last path segment
This commit is contained in:
parent
2a46a92630
commit
67eb11ae3e
5 changed files with 33 additions and 7 deletions
|
@ -6,7 +6,7 @@ configkey : IDENTIFIER ;
|
||||||
configvalue : (IDENTIFIER|VALUE) ;
|
configvalue : (IDENTIFIER|VALUE) ;
|
||||||
namedTypeDeclaration : typeName typeDeclaration ;
|
namedTypeDeclaration : typeName typeDeclaration ;
|
||||||
typeName : IDENTIFIER ;
|
typeName : IDENTIFIER ;
|
||||||
typeDeclaration : typeName? '(' typeField (',' typeField)* ')' ;
|
typeDeclaration : '(' typeField (',' typeField)* ')' ;
|
||||||
typeField : fieldName ':' fieldType ;
|
typeField : fieldName ':' fieldType ;
|
||||||
fieldName : IDENTIFIER ;
|
fieldName : IDENTIFIER ;
|
||||||
fieldType : IDENTIFIER ;
|
fieldType : IDENTIFIER ;
|
||||||
|
|
|
@ -56,8 +56,24 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
|
||||||
var endpoint = new EndpointNode(new PathsNode(segments), typeReference.getText());
|
var endpoint = new EndpointNode(new PathsNode(segments), typeReference.getText());
|
||||||
this.endpoints.add(endpoint);
|
this.endpoints.add(endpoint);
|
||||||
} else {
|
} else {
|
||||||
|
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());
|
var compoundTypeNode = extractCompoundTypeNode(ctx.requestBody().namedTypeDeclaration());
|
||||||
var endpoint = new EndpointNode(new PathsNode(segments), compoundTypeNode.name().orElseThrow());
|
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);
|
this.endpoints.add(endpoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,7 +81,7 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
|
||||||
private TypeNode extractCompoundTypeNode(EndpointsParser.NamedTypeDeclarationContext ctx) {
|
private TypeNode extractCompoundTypeNode(EndpointsParser.NamedTypeDeclarationContext ctx) {
|
||||||
String typeName = ctx.typeName().getText();
|
String typeName = ctx.typeName().getText();
|
||||||
List<FieldNode> fields = extractTypeFields(ctx.typeDeclaration().typeField());
|
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) {
|
private List<FieldNode> extractTypeFields(List<EndpointsParser.TypeFieldContext> compoundFieldContexts) {
|
||||||
|
|
7
parser/src/main/java/nu/zoom/dsl/ast/ParseException.java
Normal file
7
parser/src/main/java/nu/zoom/dsl/ast/ParseException.java
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package nu.zoom.dsl.ast;
|
||||||
|
|
||||||
|
public class ParseException extends RuntimeException {
|
||||||
|
public ParseException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,5 +3,5 @@ package nu.zoom.dsl.ast;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public record TypeNode(Optional<String> name, List<FieldNode> fields) {
|
public record TypeNode(String name, List<FieldNode> fields) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,5 @@
|
||||||
/some/endpoint =< SomeType(foo:String)
|
AType(data: List[String])
|
||||||
/some/other/endpoint =< SomeOtherType(bar:Seq[Embedded])
|
|
||||||
|
/some/endpoint <- SomeType(foo:String)
|
||||||
|
/some/other/endpoint <- SomeOtherType(bar:Seq[Embedded])
|
||||||
|
/yet/other/endpoint <- (bar:Seq[Embedded])
|
Loading…
Add table
Reference in a new issue