Use ASL license and started on documentation

This commit is contained in:
Johan Maasing 2025-04-10 21:35:49 +02:00
parent 3952cf1888
commit 19e2d46dd0
19 changed files with 553 additions and 18 deletions

View file

@ -1,4 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
grammar Endpoints;
document : generatorconfig? (namedTypeDeclaration|endpoint)* ;
generatorconfig : '{' (configitem)? (',' configitem)* '}';
@ -25,7 +38,7 @@ fragment DOT : '.' ;
fragment COMMENT_BEGIN : '/*' ;
fragment COMMENT_END : '*/' ;
WS : [ \t\n\r]+ -> skip;
COMMENT : COMMENT_BEGIN ~[/]* COMMENT_END -> skip;
COMMENT : COMMENT_BEGIN .*? COMMENT_END -> skip;
REQUEST_PREFIX : '<-' ;
RESPONSE_PREFIX : '->' ;
SLASH : '/' ;

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.ast;
import java.util.List;

View file

@ -1,6 +1,22 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.ast;
import java.util.Optional;
public record EndpointNode(
PathsNode paths,
String inputType) {
String inputType,
Optional<String> outputType) {
}

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.ast;
import nu.zoom.dsl.parser.EndpointsBaseVisitor;
@ -55,7 +68,16 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
}
private void extractEndpoint(EndpointsParser.EndpointContext ctx, List<String> segments) {
EndpointsParser.RequestBodyContext requestBodyContext = ctx.requestBody() ;
var endpoint = new EndpointNode(
new PathsNode(segments),
getRequestTypeName(ctx, segments),
getRepsonseTypeName(ctx, segments)
);
this.endpoints.add(endpoint);
}
private String getRequestTypeName(EndpointsParser.EndpointContext ctx, List<String> segments) {
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) {
@ -72,8 +94,25 @@ public class EndpointsVisitorTransformer extends EndpointsBaseVisitor<EndpointsP
if (requestTypeName == null) {
throw new ParseException("Unable to create the request body data type for " + String.join("", segments)) ;
}
var endpoint = new EndpointNode(new PathsNode(segments), requestTypeName);
this.endpoints.add(endpoint);
return requestTypeName;
}
private Optional<String> getRepsonseTypeName(EndpointsParser.EndpointContext ctx, List<String> segments) {
var responseBodyContext = ctx.responseBody();
if (responseBodyContext != null) {
if (responseBodyContext.namedTypeDeclaration() != null) {
var compoundTypeNode = extractCompoundTypeNode(responseBodyContext.namedTypeDeclaration());
return Optional.of(compoundTypeNode.name());
} else if (responseBodyContext.typeDeclaration() != null) {
var typeFields = extractTypeFields(responseBodyContext.typeDeclaration().typeField());
String responseTypeName = segments.getLast() + "Response";
addDatatType(new TypeNode(responseTypeName, typeFields), ctx.start.getLine());
return Optional.of(responseTypeName);
} else if (responseBodyContext.IDENTIFIER() != null) {
return Optional.of(responseBodyContext.IDENTIFIER().getText());
}
}
return Optional.empty();
}
private TypeNode extractCompoundTypeNode(EndpointsParser.NamedTypeDeclarationContext ctx) {

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.ast;
public record FieldNode(String name, String type) {

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.ast;
public class ParseException extends RuntimeException {

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.ast;
import nu.zoom.dsl.parser.EndpointsLexer;

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.ast;
import java.util.List;

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.ast;
import java.util.List;

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.cli;
import nu.zoom.dsl.ast.DocumentNode;

View file

@ -1,3 +1,16 @@
// Copyright 2025 "Johan Maasing" <johan@zoom.nu>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nu.zoom.dsl.freemarker;
import freemarker.template.Configuration;
@ -39,8 +52,9 @@ public class Generator {
List<Path> templates = files.filter(p -> p.toString().endsWith(".ftl")).toList() ;
ArrayList<Path> out = new ArrayList<>();
for (Path template : templates) {
// TODO file ending
Path outpath = outputDir.resolve(outputFilenameFromTemplate(template.getFileName()));
String configEnding = this.data.config().get("ending") ;
String ending = configEnding != null ? configEnding.trim() : "";
Path outpath = outputDir.resolve(outputFilenameFromTemplate(template.getFileName(), ending));
Template ftl = this.cfg.getTemplate(template.getFileName().toString()) ;
try (var outw = Files.newBufferedWriter(outpath, StandardCharsets.UTF_8)) {
ftl.process(this.data, outw);
@ -51,7 +65,7 @@ public class Generator {
}
}
private String outputFilenameFromTemplate(Path template) {
return template.getFileName().toString().replace(".ftl", ".scala");
private String outputFilenameFromTemplate(Path template, String ending) {
return template.getFileName().toString().replace(".ftl", ending);
}
}