Make config values into a map

This commit is contained in:
Johan Maasing 2025-04-09 20:00:23 +02:00
parent 57088238c4
commit 2558a52b42
8 changed files with 24 additions and 27 deletions

View file

@ -1,4 +1,4 @@
package se.senashdev.projekt.api
package ${config.package}
object Codecs:
<#list typeDefinitions as type>

View file

@ -1,4 +1,4 @@
package se.senashdev.projekt.api
package ${config.package}
import se.rutdev.projekt.api.HttpProtocol.VersionedResponse
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]
<#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>
apiV1Endpoint
.post

View file

@ -1,7 +1,7 @@
package se.senashdev.projekt.api
package ${config.package}
object Protocol:
<#list typeDefinitions as type>
<#list typeDefinitions?sort as type>
case class ${type.name?cap_first}(
<#list type.fields as field>
${field.name} : ${field.type},

View file

@ -21,6 +21,7 @@ fragment DIGIT : [0-9] ;
fragment LOWERCASE : [a-z] ;
fragment UPPERCASE : [A-Z] ;
fragment GENERICS : '['|']'|'<'|'>' ;
fragment DOT : '.' ;
fragment COMMENT_BEGIN : '/*' ;
fragment COMMENT_END : '*/' ;
WS : [ \t\n\r]+ -> skip;
@ -28,5 +29,5 @@ COMMENT : COMMENT_BEGIN ~[/]* COMMENT_END -> skip;
REQUEST_PREFIX : '<-' ;
RESPONSE_PREFIX : '->' ;
SLASH : '/' ;
IDENTIFIER : (LOWERCASE | UPPERCASE) (LOWERCASE | UPPERCASE | DIGIT | GENERICS)* ;
IDENTIFIER : (LOWERCASE | UPPERCASE) (LOWERCASE | UPPERCASE | DIGIT | GENERICS | DOT)* ;
VALUE : ~[ ,{}:()/="#';*\n\r\t]+ ;

View file

@ -1,4 +0,0 @@
package nu.zoom.dsl.ast;
public record ConfigItemNode(String key, String value) {
}

View file

@ -1,9 +1,10 @@
package nu.zoom.dsl.ast;
import java.util.List;
import java.util.Map;
public record DocumentNode(
List<ConfigItemNode> configItems,
Map<String, String> config,
List<TypeNode> typeDefinitions,
List<EndpointNode> endpoints) {
}

View file

@ -4,15 +4,12 @@ import nu.zoom.dsl.parser.EndpointsBaseVisitor;
import nu.zoom.dsl.parser.EndpointsParser;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsParser.DocumentContext> {
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<>();
public EndpointsVisitorTransformer() {
@ -22,8 +19,8 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
return List.copyOf(endpoints);
}
public List<ConfigItemNode> getConfig() {
return List.copyOf(config);
public Map<String,String> getConfig() {
return Map.copyOf(config);
}
public List<TypeNode> getDataTypes() {
@ -34,14 +31,14 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
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));
this.config.put(configKey, configValue);
return super.visitConfigitem(ctx);
}
@Override
public EndpointsParser.DocumentContext visitNamedTypeDeclaration(EndpointsParser.NamedTypeDeclarationContext ctx) {
var type = extractCompoundTypeNode(ctx);
addDatatType(type);
addDatatType(type, ctx.start.getLine());
return super.visitChildren(ctx);
}
@ -69,7 +66,7 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
} else if (requestBodyContext.typeDeclaration() != null) {
var typeFields = extractTypeFields(requestBodyContext.typeDeclaration().typeField()) ;
requestTypeName = segments.getLast() + "Request" ;
addDatatType(new TypeNode(requestTypeName, typeFields));
addDatatType(new TypeNode(requestTypeName, typeFields), ctx.start.getLine());
} else if (requestBodyContext.IDENTIFIER() != null) {
requestTypeName = requestBodyContext.IDENTIFIER().getText() ;
}
@ -103,9 +100,9 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
((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()) {
throw new ParseException("Duplicate datatype '" + type.name()+ "'") ;
throw new ParseException("Duplicate datatype '" + type.name()+ "' at line " + lineNumber) ;
} else {
this.dataTypes.add(type);
}

View file

@ -1,7 +1,9 @@
{ some: configvalue, someother:value }
AType(data: List[String])
{ some: configvalue, someother:value,
package: se.rutdev.senash
}
/some/endpoint <- SomeType(foo:String)
Embedded(foo:Bar)
/some/other/endpoint <- (bar:Seq[Embedded])
/yet/other/endpoint2 <- (bar2:Seq[AType])
/yet/other/endpoint2 <- (bar2:Seq[AType])
AType(data: java.util.List<String>)