Make config values into a map
This commit is contained in:
parent
57088238c4
commit
2558a52b42
8 changed files with 24 additions and 27 deletions
|
@ -1,4 +1,4 @@
|
||||||
package se.senashdev.projekt.api
|
package ${config.package}
|
||||||
|
|
||||||
object Codecs:
|
object Codecs:
|
||||||
<#list typeDefinitions as type>
|
<#list typeDefinitions as type>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package se.senashdev.projekt.api
|
package ${config.package}
|
||||||
|
|
||||||
import se.rutdev.projekt.api.HttpProtocol.VersionedResponse
|
import se.rutdev.projekt.api.HttpProtocol.VersionedResponse
|
||||||
import se.rutdev.framework.json.circe.RutUtilsCodec
|
import se.rutdev.framework.json.circe.RutUtilsCodec
|
||||||
|
@ -13,7 +13,7 @@ class Endpoints(override val config: OAuthUtils.OAuthConfig) extends framework.s
|
||||||
type ApiEndpoint[I, O] = OAuthEndpoint[RequestMeta.OAuthRequestMeta, I, ProblemDetail, O]
|
type ApiEndpoint[I, O] = OAuthEndpoint[RequestMeta.OAuthRequestMeta, I, ProblemDetail, O]
|
||||||
|
|
||||||
<#list endpoints as endpoint>
|
<#list endpoints as endpoint>
|
||||||
val ${endpoint.inputType}Endpoint = ApiEndpoint[${endpoint.inputType?cap_first}, VersionedResponse] =
|
val ${endpoint.inputType?uncap_first}Endpoint = ApiEndpoint[${endpoint.inputType?cap_first}, VersionedResponse] =
|
||||||
<#list endpoint.paths.paths>
|
<#list endpoint.paths.paths>
|
||||||
apiV1Endpoint
|
apiV1Endpoint
|
||||||
.post
|
.post
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package se.senashdev.projekt.api
|
package ${config.package}
|
||||||
|
|
||||||
object Protocol:
|
object Protocol:
|
||||||
<#list typeDefinitions as type>
|
<#list typeDefinitions?sort as type>
|
||||||
case class ${type.name?cap_first}(
|
case class ${type.name?cap_first}(
|
||||||
<#list type.fields as field>
|
<#list type.fields as field>
|
||||||
${field.name} : ${field.type},
|
${field.name} : ${field.type},
|
||||||
|
|
|
@ -21,6 +21,7 @@ fragment DIGIT : [0-9] ;
|
||||||
fragment LOWERCASE : [a-z] ;
|
fragment LOWERCASE : [a-z] ;
|
||||||
fragment UPPERCASE : [A-Z] ;
|
fragment UPPERCASE : [A-Z] ;
|
||||||
fragment GENERICS : '['|']'|'<'|'>' ;
|
fragment GENERICS : '['|']'|'<'|'>' ;
|
||||||
|
fragment DOT : '.' ;
|
||||||
fragment COMMENT_BEGIN : '/*' ;
|
fragment COMMENT_BEGIN : '/*' ;
|
||||||
fragment COMMENT_END : '*/' ;
|
fragment COMMENT_END : '*/' ;
|
||||||
WS : [ \t\n\r]+ -> skip;
|
WS : [ \t\n\r]+ -> skip;
|
||||||
|
@ -28,5 +29,5 @@ COMMENT : COMMENT_BEGIN ~[/]* COMMENT_END -> skip;
|
||||||
REQUEST_PREFIX : '<-' ;
|
REQUEST_PREFIX : '<-' ;
|
||||||
RESPONSE_PREFIX : '->' ;
|
RESPONSE_PREFIX : '->' ;
|
||||||
SLASH : '/' ;
|
SLASH : '/' ;
|
||||||
IDENTIFIER : (LOWERCASE | UPPERCASE) (LOWERCASE | UPPERCASE | DIGIT | GENERICS)* ;
|
IDENTIFIER : (LOWERCASE | UPPERCASE) (LOWERCASE | UPPERCASE | DIGIT | GENERICS | DOT)* ;
|
||||||
VALUE : ~[ ,{}:()/="#';*\n\r\t]+ ;
|
VALUE : ~[ ,{}:()/="#';*\n\r\t]+ ;
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
package nu.zoom.dsl.ast;
|
|
||||||
|
|
||||||
public record ConfigItemNode(String key, String value) {
|
|
||||||
}
|
|
|
@ -1,9 +1,10 @@
|
||||||
package nu.zoom.dsl.ast;
|
package nu.zoom.dsl.ast;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public record DocumentNode(
|
public record DocumentNode(
|
||||||
List<ConfigItemNode> configItems,
|
Map<String, String> config,
|
||||||
List<TypeNode> typeDefinitions,
|
List<TypeNode> typeDefinitions,
|
||||||
List<EndpointNode> endpoints) {
|
List<EndpointNode> endpoints) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,15 +4,12 @@ import nu.zoom.dsl.parser.EndpointsBaseVisitor;
|
||||||
import nu.zoom.dsl.parser.EndpointsParser;
|
import nu.zoom.dsl.parser.EndpointsParser;
|
||||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsParser.DocumentContext> {
|
public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsParser.DocumentContext> {
|
||||||
private final ArrayList<EndpointNode> endpoints = new ArrayList<>();
|
private final ArrayList<EndpointNode> endpoints = new ArrayList<>();
|
||||||
private final ArrayList<ConfigItemNode> config = new ArrayList<>();
|
private final HashMap<String,String> config = new HashMap<>();
|
||||||
private final HashSet<TypeNode> dataTypes = new HashSet<>();
|
private final HashSet<TypeNode> dataTypes = new HashSet<>();
|
||||||
|
|
||||||
public EndpointsVisitorTransformer() {
|
public EndpointsVisitorTransformer() {
|
||||||
|
@ -22,8 +19,8 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
|
||||||
return List.copyOf(endpoints);
|
return List.copyOf(endpoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ConfigItemNode> getConfig() {
|
public Map<String,String> getConfig() {
|
||||||
return List.copyOf(config);
|
return Map.copyOf(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<TypeNode> getDataTypes() {
|
public List<TypeNode> getDataTypes() {
|
||||||
|
@ -34,14 +31,14 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
|
||||||
public EndpointsParser.DocumentContext visitConfigitem(EndpointsParser.ConfigitemContext ctx) {
|
public EndpointsParser.DocumentContext visitConfigitem(EndpointsParser.ConfigitemContext ctx) {
|
||||||
String configKey = ctx.configkey().IDENTIFIER().getText();
|
String configKey = ctx.configkey().IDENTIFIER().getText();
|
||||||
String configValue = getText(ctx.configvalue().IDENTIFIER(), ctx.configvalue().VALUE());
|
String configValue = getText(ctx.configvalue().IDENTIFIER(), ctx.configvalue().VALUE());
|
||||||
this.config.add(new ConfigItemNode(configKey, configValue));
|
this.config.put(configKey, configValue);
|
||||||
return super.visitConfigitem(ctx);
|
return super.visitConfigitem(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EndpointsParser.DocumentContext visitNamedTypeDeclaration(EndpointsParser.NamedTypeDeclarationContext ctx) {
|
public EndpointsParser.DocumentContext visitNamedTypeDeclaration(EndpointsParser.NamedTypeDeclarationContext ctx) {
|
||||||
var type = extractCompoundTypeNode(ctx);
|
var type = extractCompoundTypeNode(ctx);
|
||||||
addDatatType(type);
|
addDatatType(type, ctx.start.getLine());
|
||||||
return super.visitChildren(ctx);
|
return super.visitChildren(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +66,7 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
|
||||||
} else if (requestBodyContext.typeDeclaration() != null) {
|
} else if (requestBodyContext.typeDeclaration() != null) {
|
||||||
var typeFields = extractTypeFields(requestBodyContext.typeDeclaration().typeField()) ;
|
var typeFields = extractTypeFields(requestBodyContext.typeDeclaration().typeField()) ;
|
||||||
requestTypeName = segments.getLast() + "Request" ;
|
requestTypeName = segments.getLast() + "Request" ;
|
||||||
addDatatType(new TypeNode(requestTypeName, typeFields));
|
addDatatType(new TypeNode(requestTypeName, typeFields), ctx.start.getLine());
|
||||||
} else if (requestBodyContext.IDENTIFIER() != null) {
|
} else if (requestBodyContext.IDENTIFIER() != null) {
|
||||||
requestTypeName = requestBodyContext.IDENTIFIER().getText() ;
|
requestTypeName = requestBodyContext.IDENTIFIER().getText() ;
|
||||||
}
|
}
|
||||||
|
@ -103,9 +100,9 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
|
||||||
((value != null) ? value.getText() : "");
|
((value != null) ? value.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addDatatType(TypeNode type) throws ParseException {
|
private void addDatatType(TypeNode type, int lineNumber) throws ParseException {
|
||||||
if (this.dataTypes.stream().filter(t -> t.name().equals(type.name())).findAny().isPresent()) {
|
if (this.dataTypes.stream().filter(t -> t.name().equals(type.name())).findAny().isPresent()) {
|
||||||
throw new ParseException("Duplicate datatype '" + type.name()+ "'") ;
|
throw new ParseException("Duplicate datatype '" + type.name()+ "' at line " + lineNumber) ;
|
||||||
} else {
|
} else {
|
||||||
this.dataTypes.add(type);
|
this.dataTypes.add(type);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
{ some: configvalue, someother:value }
|
{ some: configvalue, someother:value,
|
||||||
|
package: se.rutdev.senash
|
||||||
AType(data: List[String])
|
}
|
||||||
|
|
||||||
/some/endpoint <- SomeType(foo:String)
|
/some/endpoint <- SomeType(foo:String)
|
||||||
|
Embedded(foo:Bar)
|
||||||
/some/other/endpoint <- (bar:Seq[Embedded])
|
/some/other/endpoint <- (bar:Seq[Embedded])
|
||||||
/yet/other/endpoint2 <- (bar2:Seq[AType])
|
/yet/other/endpoint2 <- (bar2:Seq[AType])
|
||||||
|
AType(data: java.util.List<String>)
|
||||||
|
|
Loading…
Add table
Reference in a new issue