Skip to content

Commit 42e2928

Browse files
authored
Invert abstractness of generated vs "handwritten" client classes (#1170)
* Invert abstractness of generated vs "handwritten" client classes Signed-off-by: Thomas Farr <[email protected]> * Address review comments Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]>
1 parent eb5ba5f commit 42e2928

23 files changed

+439
-169
lines changed

java-client/src/generated/java/org/opensearch/client/opensearch/OpenSearchAsyncClient.java java-client/src/generated/java/org/opensearch/client/opensearch/OpenSearchAsyncClientBase.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.concurrent.CompletableFuture;
4141
import javax.annotation.Generated;
4242
import javax.annotation.Nullable;
43+
import org.opensearch.client.ApiClient;
4344
import org.opensearch.client.opensearch._types.OpenSearchException;
4445
import org.opensearch.client.opensearch.core.InfoRequest;
4546
import org.opensearch.client.opensearch.core.InfoResponse;
@@ -52,20 +53,11 @@
5253
* Client for the namespace.
5354
*/
5455
@Generated("org.opensearch.client.codegen.CodeGenerator")
55-
public class OpenSearchAsyncClient extends OpenSearchAsyncClientBase<OpenSearchAsyncClient> {
56-
public OpenSearchAsyncClient(OpenSearchTransport transport) {
57-
super(transport, null);
58-
}
59-
60-
public OpenSearchAsyncClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
56+
public abstract class OpenSearchAsyncClientBase<Self extends OpenSearchAsyncClientBase<Self>> extends ApiClient<OpenSearchTransport, Self> {
57+
public OpenSearchAsyncClientBase(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
6158
super(transport, transportOptions);
6259
}
6360

64-
@Override
65-
public OpenSearchAsyncClient withTransportOptions(@Nullable TransportOptions transportOptions) {
66-
return new OpenSearchAsyncClient(this.transport, transportOptions);
67-
}
68-
6961
// ----- Child clients
7062

7163
public OpenSearchDanglingIndicesAsyncClient danglingIndices() {

java-client/src/generated/java/org/opensearch/client/opensearch/OpenSearchClient.java java-client/src/generated/java/org/opensearch/client/opensearch/OpenSearchClientBase.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.io.IOException;
4040
import javax.annotation.Generated;
4141
import javax.annotation.Nullable;
42+
import org.opensearch.client.ApiClient;
4243
import org.opensearch.client.opensearch._types.OpenSearchException;
4344
import org.opensearch.client.opensearch.core.InfoRequest;
4445
import org.opensearch.client.opensearch.core.InfoResponse;
@@ -51,20 +52,11 @@
5152
* Client for the namespace.
5253
*/
5354
@Generated("org.opensearch.client.codegen.CodeGenerator")
54-
public class OpenSearchClient extends OpenSearchClientBase<OpenSearchClient> {
55-
public OpenSearchClient(OpenSearchTransport transport) {
56-
super(transport, null);
57-
}
58-
59-
public OpenSearchClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
55+
public abstract class OpenSearchClientBase<Self extends OpenSearchClientBase<Self>> extends ApiClient<OpenSearchTransport, Self> {
56+
public OpenSearchClientBase(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
6057
super(transport, transportOptions);
6158
}
6259

63-
@Override
64-
public OpenSearchClient withTransportOptions(@Nullable TransportOptions transportOptions) {
65-
return new OpenSearchClient(this.transport, transportOptions);
66-
}
67-
6860
// ----- Child clients
6961

7062
public OpenSearchDanglingIndicesClient danglingIndices() {

java-client/src/main/java/org/opensearch/client/opensearch/OpenSearchAsyncClientBase.java java-client/src/main/java/org/opensearch/client/opensearch/OpenSearchAsyncClient.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.concurrent.CompletableFuture;
3737
import java.util.function.Function;
3838
import javax.annotation.Nullable;
39-
import org.opensearch.client.ApiClient;
4039
import org.opensearch.client.opensearch._types.ErrorResponse;
4140
import org.opensearch.client.opensearch._types.OpenSearchException;
4241
import org.opensearch.client.opensearch.cat.OpenSearchCatAsyncClient;
@@ -137,12 +136,20 @@
137136
/**
138137
* Client for the namespace.
139138
*/
140-
public abstract class OpenSearchAsyncClientBase<Self extends OpenSearchAsyncClientBase<Self>> extends ApiClient<OpenSearchTransport, Self> {
139+
public class OpenSearchAsyncClient extends OpenSearchAsyncClientBase<OpenSearchAsyncClient> {
140+
public OpenSearchAsyncClient(OpenSearchTransport transport) {
141+
super(transport, null);
142+
}
141143

142-
public OpenSearchAsyncClientBase(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
144+
public OpenSearchAsyncClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
143145
super(transport, transportOptions);
144146
}
145147

148+
@Override
149+
public OpenSearchAsyncClient withTransportOptions(@Nullable TransportOptions transportOptions) {
150+
return new OpenSearchAsyncClient(this.transport, transportOptions);
151+
}
152+
146153
// ----- Child clients
147154

148155
public OpenSearchCatAsyncClient cat() {

java-client/src/main/java/org/opensearch/client/opensearch/OpenSearchClientBase.java java-client/src/main/java/org/opensearch/client/opensearch/OpenSearchClient.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.io.IOException;
3636
import java.util.function.Function;
3737
import javax.annotation.Nullable;
38-
import org.opensearch.client.ApiClient;
3938
import org.opensearch.client.opensearch._types.ErrorResponse;
4039
import org.opensearch.client.opensearch._types.OpenSearchException;
4140
import org.opensearch.client.opensearch.cat.OpenSearchCatClient;
@@ -137,12 +136,20 @@
137136
/**
138137
* Client for the namespace.
139138
*/
140-
public abstract class OpenSearchClientBase<Self extends OpenSearchClientBase<Self>> extends ApiClient<OpenSearchTransport, Self> {
139+
public class OpenSearchClient extends OpenSearchClientBase<OpenSearchClient> {
140+
public OpenSearchClient(OpenSearchTransport transport) {
141+
super(transport, null);
142+
}
141143

142-
public OpenSearchClientBase(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
144+
public OpenSearchClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
143145
super(transport, transportOptions);
144146
}
145147

148+
@Override
149+
public OpenSearchClient withTransportOptions(@Nullable TransportOptions transportOptions) {
150+
return new OpenSearchClient(this.transport, transportOptions);
151+
}
152+
146153
// ----- Child clients
147154
public OpenSearchGenericClient generic() {
148155
return new OpenSearchGenericClient(this.transport, this.transportOptions);

java-codegen/src/main/java/org/opensearch/client/codegen/model/Namespace.java

+28-17
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ public void render(ShapeRenderingContext ctx) throws RenderException {
8282

8383
if (operations.isEmpty()) return;
8484

85-
new Client(this, false, operations).render(ctx);
86-
new Client(this, true, operations).render(ctx);
85+
var asBaseClass = "".equals(name);
86+
87+
new Client(this, false, asBaseClass, operations).render(ctx);
88+
new Client(this, true, asBaseClass, operations).render(ctx);
8789
}
8890

8991
private Collection<RequestShape> getOperationsForClient() {
@@ -95,32 +97,37 @@ private String getClientClassName(boolean async, boolean base) {
9597
return "OpenSearch" + Strings.toPascalCase(name) + (async ? "Async" : "") + "Client" + (base ? "Base" : "");
9698
}
9799

98-
private Type getClientType(boolean async, boolean base) {
99-
var type = Type.builder().pkg(getPackageName()).name(getClientClassName(async, base));
100-
if (base) {
101-
type.typeParams(getClientType(async, false));
102-
}
103-
return type.build();
100+
private Type getClientType(boolean async) {
101+
return Type.builder().withPackage(getPackageName()).withName(getClientClassName(async, false)).build();
104102
}
105103

106104
private static class Client extends Shape {
107105
private final boolean async;
106+
private final boolean base;
108107
private final Collection<RequestShape> operations;
109108

110-
private Client(Namespace parent, boolean async, Collection<RequestShape> operations) {
111-
super(parent, parent.getClientClassName(async, false), null, "Client for the " + parent.name + " namespace.");
109+
private Client(Namespace parent, boolean async, boolean base, Collection<RequestShape> operations) {
110+
super(parent, parent.getClientClassName(async, base), null, "Client for the " + parent.name + " namespace.");
112111
this.async = async;
112+
this.base = base;
113113
this.operations = operations;
114114
}
115115

116+
@Override
117+
public TypeParameterDiamond getTypeParameters() {
118+
if (!base) return null;
119+
var thisType = getType().withTypeParams(Type.builder().withName("Self").build());
120+
return TypeParameterDiamond.builder()
121+
.withParams(TypeParameterDefinition.builder().withName("Self").withExtends(thisType).build())
122+
.build();
123+
}
124+
116125
@Override
117126
public Type getExtendsType() {
118-
switch (parent.name) {
119-
case "":
120-
return parent.getClientType(async, true);
121-
default:
122-
return Types.Client.ApiClient(Types.Client.Transport.OpenSearchTransport, getType());
123-
}
127+
return Types.Client.ApiClient(
128+
Types.Client.Transport.OpenSearchTransport,
129+
!base ? getType() : Type.builder().withName("Self").build()
130+
);
124131
}
125132

126133
public String getName() {
@@ -139,6 +146,10 @@ public boolean isAsync() {
139146
return this.async;
140147
}
141148

149+
public boolean isBase() {
150+
return this.base;
151+
}
152+
142153
@Override
143154
public String toString() {
144155
return new ToStringBuilder(this).append("type", getType()).toString();
@@ -149,7 +160,7 @@ private static class ClientRef {
149160
private final String name;
150161

151162
public ClientRef(Namespace namespace, boolean async) {
152-
this.type = namespace.getClientType(async, false);
163+
this.type = namespace.getClientType(async);
153164
this.name = namespace.name;
154165
}
155166

java-codegen/src/main/java/org/opensearch/client/codegen/model/RequestShape.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void addSupportedHttpMethod(String method) {
7878
}
7979

8080
public Type getResponseType() {
81-
return Type.builder().pkg(getPackageName()).name(responseClassName(operationGroup)).build();
81+
return Type.builder().withPackage(getPackageName()).withName(responseClassName(operationGroup)).build();
8282
}
8383

8484
public boolean canBeSingleton() {

java-codegen/src/main/java/org/opensearch/client/codegen/model/Shape.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public Collection<Type> getAnnotations() {
6161
return Collections.emptyList();
6262
}
6363

64+
public TypeParameterDiamond getTypeParameters() {
65+
return null;
66+
}
67+
6468
public Type getExtendsType() {
6569
return null;
6670
}
@@ -70,7 +74,7 @@ public Collection<Type> getImplementsTypes() {
7074
}
7175

7276
public Type getType() {
73-
return Type.builder().pkg(getPackageName()).name(className).build();
77+
return Type.builder().withPackage(getPackageName()).withName(className).build();
7478
}
7579

7680
public Namespace getParent() {

java-codegen/src/main/java/org/opensearch/client/codegen/model/ShapeRenderingContext.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.opensearch.client.codegen.renderer.TemplateLoader;
1717
import org.opensearch.client.codegen.renderer.TemplateRenderer;
1818
import org.opensearch.client.codegen.renderer.TemplateValueFormatter;
19+
import org.opensearch.client.codegen.utils.ObjectBuilderBase;
1920
import org.opensearch.client.codegen.utils.Strings;
2021

2122
public final class ShapeRenderingContext implements AutoCloseable {
@@ -71,12 +72,21 @@ public static Builder builder() {
7172
return new Builder();
7273
}
7374

74-
public static final class Builder {
75+
public static final class Builder extends ObjectBuilderBase<ShapeRenderingContext, Builder> {
7576
private File outputDir;
7677
private TemplateLoader templateLoader;
7778
private JavaCodeFormatter javaCodeFormatter;
7879
private boolean ownedJavaCodeFormatter;
7980

81+
private Builder() {
82+
super(ShapeRenderingContext::new);
83+
}
84+
85+
@Override
86+
protected @Nonnull Builder self() {
87+
return this;
88+
}
89+
8090
@Nonnull
8191
public Builder withOutputDir(@Nonnull File outputDir) {
8292
this.outputDir = Objects.requireNonNull(outputDir, "outputDir must not be null");
@@ -118,10 +128,5 @@ public Builder withJavaCodeFormatter(@Nonnull Function<JavaCodeFormatter.Builder
118128
true
119129
);
120130
}
121-
122-
@Nonnull
123-
public ShapeRenderingContext build() {
124-
return new ShapeRenderingContext(this);
125-
}
126131
}
127132
}

java-codegen/src/main/java/org/opensearch/client/codegen/model/SpecTransformer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ private Type mapTypeInner(OpenApiSchema schema) {
387387
visit(schema);
388388

389389
return Type.builder()
390-
.pkg(Types.Client.OpenSearch.PACKAGE + "." + schema.getNamespace().orElseThrow())
391-
.name(schema.getName().orElseThrow())
390+
.withPackage(Types.Client.OpenSearch.PACKAGE + "." + schema.getNamespace().orElseThrow())
391+
.withName(schema.getName().orElseThrow())
392392
.isEnum(schema.hasEnums())
393393
.build();
394394
}

0 commit comments

Comments
 (0)