Skip to content

Commit

Permalink
use apiversions enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto committed Jan 6, 2025
1 parent 1d01453 commit 269f7e7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.fern.java.utils;

public final class ApiVersionConstants {
public static final String CURRENT_API_VERSION = "CURRENT";
}
45 changes: 43 additions & 2 deletions generators/java/sdk/src/main/java/com/fern/java/client/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
import com.fern.ir.model.auth.AuthScheme;
import com.fern.ir.model.auth.OAuthScheme;
import com.fern.ir.model.commons.ErrorId;
import com.fern.ir.model.commons.Name;
import com.fern.ir.model.commons.NameAndWireValue;
import com.fern.ir.model.commons.SafeAndUnsafeString;
import com.fern.ir.model.ir.HeaderApiVersionScheme;
import com.fern.ir.model.ir.IntermediateRepresentation;
import com.fern.ir.model.types.EnumTypeDeclaration;
import com.fern.ir.model.types.EnumValue;
import com.fern.java.AbstractGeneratorCli;
import com.fern.java.AbstractPoetClassNameFactory;
import com.fern.java.DefaultGeneratorExecClient;
Expand Down Expand Up @@ -42,6 +47,7 @@
import com.fern.java.output.gradle.GradleDependency;
import com.fern.java.output.gradle.GradleDependencyType;
import com.fern.java.output.gradle.ParsedGradleDependency;
import com.fern.java.utils.ApiVersionConstants;
import com.palantir.common.streams.KeyedStream;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -182,9 +188,44 @@ public GeneratedRootClient generateClient(

ir.getApiVersion()
.flatMap(apiVersion -> apiVersion.getHeader().map(HeaderApiVersionScheme::getValue))
.ifPresent(enumTypeDeclaration -> {
.ifPresent(irDeclaration -> {
EnumTypeDeclaration.Builder enumTypeDeclaration =
EnumTypeDeclaration.builder().from(irDeclaration);

irDeclaration.getDefault().ifPresent(defaultValue -> {
Name current = Name.builder()
.originalName(ApiVersionConstants.CURRENT_API_VERSION)
.camelCase(SafeAndUnsafeString.builder()
.unsafeName(ApiVersionConstants.CURRENT_API_VERSION)
.safeName(ApiVersionConstants.CURRENT_API_VERSION)
.build())
.pascalCase(SafeAndUnsafeString.builder()
.unsafeName(ApiVersionConstants.CURRENT_API_VERSION)
.safeName(ApiVersionConstants.CURRENT_API_VERSION)
.build())
.snakeCase(SafeAndUnsafeString.builder()
.unsafeName(ApiVersionConstants.CURRENT_API_VERSION)
.safeName(ApiVersionConstants.CURRENT_API_VERSION)
.build())
.screamingSnakeCase(SafeAndUnsafeString.builder()
.unsafeName(ApiVersionConstants.CURRENT_API_VERSION)
.safeName(ApiVersionConstants.CURRENT_API_VERSION)
.build())
.build();

enumTypeDeclaration.addValues(EnumValue.builder()
.from(defaultValue)
.name(NameAndWireValue.builder()
.from(defaultValue.getName())
.name(current)
.build())
.build());
});

EnumGenerator apiVersionsGenerator = new EnumGenerator(
context.getPoetClassNameFactory().getApiVersionsClassName(), context, enumTypeDeclaration);
context.getPoetClassNameFactory().getApiVersionsClassName(),
context,
enumTypeDeclaration.build());
GeneratedJavaFile generatedApiVersions = apiVersionsGenerator.generateFile();
this.addGeneratedFile(generatedApiVersions);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.fern.java.client.GeneratedEnvironmentsClass;
import com.fern.java.generators.AbstractFileGenerator;
import com.fern.java.output.GeneratedJavaFile;
import com.fern.java.utils.ApiVersionConstants;
import com.google.common.collect.ImmutableList;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
Expand Down Expand Up @@ -71,9 +72,6 @@ public final class ClientOptionsGenerator extends AbstractFileGenerator {
Modifier.PRIVATE,
Modifier.FINAL)
.build();
private static final FieldSpec API_VERSIONS_FIELD = FieldSpec.builder(
String.class, "version", Modifier.PRIVATE, Modifier.FINAL)
.build();
private static final FieldSpec OKHTTP_CLIENT_FIELD = FieldSpec.builder(
OkHttpClient.class, "httpClient", Modifier.PRIVATE, Modifier.FINAL)
.build();
Expand All @@ -86,6 +84,7 @@ public final class ClientOptionsGenerator extends AbstractFileGenerator {
private final FieldSpec environmentField;
private final GeneratedJavaFile requestOptionsFile;
private final ClientGeneratorContext clientGeneratorContext;
private final FieldSpec apiVersionsField;

public ClientOptionsGenerator(
ClientGeneratorContext clientGeneratorContext,
Expand All @@ -101,13 +100,19 @@ public ClientOptionsGenerator(
.build();
this.requestOptionsFile = requestOptionsFile;
this.clientGeneratorContext = clientGeneratorContext;
this.apiVersionsField = FieldSpec.builder(
clientGeneratorContext.getPoetClassNameFactory().getApiVersionsClassName(),
"version",
Modifier.PRIVATE,
Modifier.FINAL)
.build();
}

@Override
public GeneratedClientOptions generateFile() {
MethodSpec environmentGetter = createGetter(environmentField);
MethodSpec headersFromRequestOptions = headersFromRequestOptions();
Optional<MethodSpec> versionsGetter = Optional.empty();
Optional<MethodSpec> versionsGetter;
Optional<MethodSpec> headersFromIdempotentRequestOptions = headersFromIdempotentRequestOptions();
MethodSpec httpClientGetter = createGetter(OKHTTP_CLIENT_FIELD);
Map<VariableId, FieldSpec> variableFields = getVariableFields();
Expand Down Expand Up @@ -161,31 +166,33 @@ public GeneratedClientOptions generateFile() {
public Void visitHeader(HeaderApiVersionScheme headerApiVersionScheme) {
constructorBuilder.addParameter(ParameterSpec.builder(
ParameterizedTypeName.get(
ClassName.get(Optional.class), ClassName.get(String.class)),
API_VERSIONS_FIELD.name)
ClassName.get(Optional.class),
clientGeneratorContext
.getPoetClassNameFactory()
.getApiVersionsClassName()),
apiVersionsField.name)
.build());

if (headerApiVersionScheme.getValue().getDefault().isPresent()) {
constructorBuilder.addStatement(
"this.$L = $L.orElse($S)",
API_VERSIONS_FIELD.name,
API_VERSIONS_FIELD.name,
headerApiVersionScheme
.getValue()
.getDefault()
.get()
.getName()
.getWireValue());
"this.$L = $L.orElse($L)",
apiVersionsField.name,
apiVersionsField.name,
CodeBlock.of(
"$T.$L",
clientGeneratorContext
.getPoetClassNameFactory()
.getApiVersionsClassName(),
ApiVersionConstants.CURRENT_API_VERSION));
} else {
constructorBuilder.addStatement(
"this.$L = $L", API_VERSIONS_FIELD.name, API_VERSIONS_FIELD.name);
constructorBuilder.addStatement("this.$L = $L", apiVersionsField.name, apiVersionsField.name);
}

constructorBuilder.addStatement(
"this.$L.put($S,$L)",
HEADERS_FIELD.name,
headerApiVersionScheme.getHeader().getName().getWireValue(),
CodeBlock.of("this.$L", API_VERSIONS_FIELD.name));
CodeBlock.of("this.$L.toString()", apiVersionsField.name));

return null;
}
Expand Down Expand Up @@ -214,8 +221,8 @@ public Void _visitUnknown(Object _o) {
.addMethod(headersFromRequestOptions);

if (clientGeneratorContext.getIr().getApiVersion().isPresent()) {
clientOptionsBuilder.addField(API_VERSIONS_FIELD);
versionsGetter = Optional.of(createGetter(API_VERSIONS_FIELD));
clientOptionsBuilder.addField(apiVersionsField);
versionsGetter = Optional.of(createGetter(apiVersionsField));
clientOptionsBuilder.addMethod(versionsGetter.get());
}

Expand Down Expand Up @@ -358,8 +365,10 @@ private TypeSpec createBuilder(ClientGeneratorContext context, Map<VariableId, F

if (context.getIr().getApiVersion().isPresent()) {
builder.addField(FieldSpec.builder(
ParameterizedTypeName.get(ClassName.get(Optional.class), ClassName.get(String.class)),
API_VERSIONS_FIELD.name,
ParameterizedTypeName.get(
ClassName.get(Optional.class),
context.getPoetClassNameFactory().getApiVersionsClassName()),
apiVersionsField.name,
Modifier.PRIVATE)
.build());
builder.addMethod(getVersionBuilder());
Expand Down Expand Up @@ -392,11 +401,13 @@ private MethodSpec getHeaderBuilder() {
}

private MethodSpec getVersionBuilder() {
return MethodSpec.methodBuilder(API_VERSIONS_FIELD.name)
return MethodSpec.methodBuilder(apiVersionsField.name)
.addModifiers(Modifier.PUBLIC)
.returns(builderClassName)
.addParameter(String.class, API_VERSIONS_FIELD.name)
.addStatement("this.$L = $T.of($L)", API_VERSIONS_FIELD.name, Optional.class, API_VERSIONS_FIELD.name)
.addParameter(
clientGeneratorContext.getPoetClassNameFactory().getApiVersionsClassName(),
apiVersionsField.name)
.addStatement("this.$L = $T.of($L)", apiVersionsField.name, Optional.class, apiVersionsField.name)
.addStatement("return this")
.build();
}
Expand Down Expand Up @@ -469,7 +480,7 @@ private MethodSpec getBuildMethod(ClientGeneratorContext context, Map<VariableId
String returnString = "return new $T($L, $L, $L, $L, this.timeout";

if (context.getIr().getApiVersion().isPresent()) {
argsBuilder.add(API_VERSIONS_FIELD.name);
argsBuilder.add(apiVersionsField.name);
returnString = "return new $T($L, $L, $L, $L, this.timeout, $L";
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 269f7e7

Please sign in to comment.