-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add path parameter support.
- Loading branch information
Showing
14 changed files
with
673 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 12 additions & 2 deletions
14
modules/lily-compiler/src/main/java/io/github/tomboyo/lily/compiler/ast/AstOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
package io.github.tomboyo.lily.compiler.ast; | ||
|
||
/** An operation, such as "createNewBlogPost" corresponding to an OAS operation. */ | ||
public record AstOperation(String operationName, AstReference operationClass, String relativePath) | ||
import java.util.List; | ||
|
||
/** | ||
* An operation, such as "createNewBlogPost" corresponding to an OAS operation. | ||
* | ||
* <p>The parameters field list reflects the order of parameters as specified in the OAS. | ||
*/ | ||
public record AstOperation( | ||
String operationName, | ||
AstReference operationClass, | ||
String relativePath, | ||
List<AstParameter> parameters) | ||
implements Ast {} |
3 changes: 2 additions & 1 deletion
3
modules/lily-compiler/src/main/java/io/github/tomboyo/lily/compiler/ast/AstParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
package io.github.tomboyo.lily.compiler.ast; | ||
|
||
/** A path parameter for an operation */ | ||
public record AstParameter(String name, AstReference astReference) implements Ast {} | ||
public record AstParameter(String name, AstParameterLocation location, AstReference astReference) | ||
implements Ast {} |
18 changes: 18 additions & 0 deletions
18
...lily-compiler/src/main/java/io/github/tomboyo/lily/compiler/ast/AstParameterLocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.tomboyo.lily.compiler.ast; | ||
|
||
public enum AstParameterLocation { | ||
PATH, | ||
QUERY, | ||
COOKIE, | ||
HEADER; | ||
|
||
public static AstParameterLocation fromString(String raw) { | ||
return switch (raw) { | ||
case "path" -> PATH; | ||
case "query" -> QUERY; | ||
case "cookie" -> COOKIE; | ||
case "header" -> HEADER; | ||
default -> throw new IllegalArgumentException("Unrecognized parameter location: " + raw); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...es/lily-compiler/src/main/java/io/github/tomboyo/lily/compiler/icg/OasOperationToAst.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.github.tomboyo.lily.compiler.icg; | ||
|
||
import static io.github.tomboyo.lily.compiler.icg.Support.capitalCamelCase; | ||
import static io.github.tomboyo.lily.compiler.icg.Support.joinPackages; | ||
import static java.util.Objects.requireNonNull; | ||
import static java.util.Objects.requireNonNullElse; | ||
import static java.util.function.Function.identity; | ||
import static java.util.stream.Collectors.toMap; | ||
|
||
import io.github.tomboyo.lily.compiler.ast.Ast; | ||
import io.github.tomboyo.lily.compiler.ast.AstOperation; | ||
import io.github.tomboyo.lily.compiler.ast.AstReference; | ||
import io.swagger.v3.oas.models.Operation; | ||
import io.swagger.v3.oas.models.parameters.Parameter; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class OasOperationToAst { | ||
|
||
private final String basePackage; | ||
|
||
private OasOperationToAst(String basePackage) { | ||
this.basePackage = basePackage; | ||
} | ||
|
||
public static TagsOperationAndAst evaluateOperaton( | ||
String basePackage, | ||
String relativePath, | ||
Operation operation, | ||
List<Parameter> inheritedParameters) { | ||
return new OasOperationToAst(basePackage) | ||
.evaluateOperation(relativePath, operation, inheritedParameters); | ||
} | ||
|
||
private TagsOperationAndAst evaluateOperation( | ||
String relativePath, Operation operation, List<Parameter> inheritedParameters) { | ||
var operationName = requireNonNull(capitalCamelCase(operation.getOperationId())) + "Operation"; | ||
var subordinatePackageName = joinPackages(basePackage, operationName); | ||
var ownParameters = requireNonNullElse(operation.getParameters(), List.<Parameter>of()); | ||
|
||
var parametersAndAst = | ||
mergeParameters(inheritedParameters, ownParameters).stream() | ||
.map( | ||
parameter -> OasParameterToAst.evaluateParameter(subordinatePackageName, parameter)) | ||
.collect(Collectors.toList()); | ||
var ast = | ||
parametersAndAst.stream() | ||
.flatMap(OasParameterToAst.ParameterAndAst::ast) | ||
.collect(Collectors.toSet()); | ||
var parameters = | ||
parametersAndAst.stream() | ||
.map(OasParameterToAst.ParameterAndAst::parameter) | ||
.collect(Collectors.toList()); | ||
|
||
return new TagsOperationAndAst( | ||
getOperationTags(operation), | ||
new AstOperation( | ||
operation.getOperationId(), | ||
new AstReference(basePackage, operationName, List.of(), false), | ||
relativePath, | ||
parameters), | ||
ast); | ||
} | ||
|
||
/** Merge owned parameters with inherited parameters. Owned parameters take precedence. */ | ||
private static Collection<Parameter> mergeParameters( | ||
List<Parameter> inherited, List<Parameter> owned) { | ||
return Stream.concat(inherited.stream(), owned.stream()) | ||
.collect( | ||
toMap( | ||
param -> new ParameterId(param.getName(), param.getIn()), identity(), (a, b) -> b)) | ||
.values(); | ||
} | ||
|
||
private static Set<String> getOperationTags(Operation operation) { | ||
var set = new HashSet<>(requireNonNullElse(operation.getTags(), List.of())); | ||
if (set.isEmpty()) { | ||
set.add("other"); | ||
} | ||
set.add("all"); | ||
return set; | ||
} | ||
|
||
/** Holds the tags, AstOperation, and other Ast from evaluating an OAS Operation. */ | ||
public static record TagsOperationAndAst( | ||
Set<String> tags, AstOperation operation, Set<Ast> ast) {} | ||
|
||
private static record ParameterId(String name, String in) {} | ||
} |
27 changes: 27 additions & 0 deletions
27
...es/lily-compiler/src/main/java/io/github/tomboyo/lily/compiler/icg/OasParameterToAst.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.tomboyo.lily.compiler.icg; | ||
|
||
import static io.github.tomboyo.lily.compiler.icg.Support.capitalCamelCase; | ||
|
||
import io.github.tomboyo.lily.compiler.ast.Ast; | ||
import io.github.tomboyo.lily.compiler.ast.AstParameter; | ||
import io.github.tomboyo.lily.compiler.ast.AstParameterLocation; | ||
import io.swagger.v3.oas.models.parameters.Parameter; | ||
import java.util.stream.Stream; | ||
|
||
public class OasParameterToAst { | ||
|
||
public static ParameterAndAst evaluateParameter(String packageName, Parameter parameter) { | ||
var parameterRefAndAst = | ||
OasSchemaToAst.evaluate( | ||
packageName, capitalCamelCase(parameter.getName()), parameter.getSchema()); | ||
|
||
return new ParameterAndAst( | ||
new AstParameter( | ||
parameter.getName(), | ||
AstParameterLocation.fromString(parameter.getIn()), | ||
parameterRefAndAst.left()), | ||
parameterRefAndAst.right()); | ||
} | ||
|
||
public static record ParameterAndAst(AstParameter parameter, Stream<Ast> ast) {} | ||
} |
Oops, something went wrong.