Skip to content

Commit

Permalink
Begin generating client implementations (opensearch-project#1052)
Browse files Browse the repository at this point in the history
* Tweak client generation to extend from existing classes

Signed-off-by: Thomas Farr <[email protected]>

* Modify existing client classes to be abstract base classes

Signed-off-by: Thomas Farr <[email protected]>

* Re-run generator

Signed-off-by: Thomas Farr <[email protected]>

* ./gradlew spotlessApply

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia authored Jun 26, 2024
1 parent edcf6c1 commit dbde609
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

//----------------------------------------------------
// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST.
//----------------------------------------------------

package org.opensearch.client.opensearch;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import org.opensearch.client.opensearch._types.OpenSearchException;
import org.opensearch.client.opensearch.core.InfoRequest;
import org.opensearch.client.opensearch.core.InfoResponse;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.TransportOptions;

@Generated("org.opensearch.client.codegen.CodeGenerator")
public class OpenSearchAsyncClient extends OpenSearchAsyncClientBase<OpenSearchAsyncClient> {
public OpenSearchAsyncClient(OpenSearchTransport transport) {
super(transport, null);
}

public OpenSearchAsyncClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
super(transport, transportOptions);
}

@Override
public OpenSearchAsyncClient withTransportOptions(@Nullable TransportOptions transportOptions) {
return new OpenSearchAsyncClient(this.transport, transportOptions);
}

// ----- Endpoint: info

/**
* Returns basic information about the cluster.
*/
public CompletableFuture<InfoResponse> info() throws IOException, OpenSearchException {
return this.transport.performRequestAsync(InfoRequest._INSTANCE, InfoRequest._ENDPOINT, this.transportOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

//----------------------------------------------------
// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST.
//----------------------------------------------------

package org.opensearch.client.opensearch;

import java.io.IOException;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import org.opensearch.client.opensearch._types.OpenSearchException;
import org.opensearch.client.opensearch.core.InfoRequest;
import org.opensearch.client.opensearch.core.InfoResponse;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.TransportOptions;

@Generated("org.opensearch.client.codegen.CodeGenerator")
public class OpenSearchClient extends OpenSearchClientBase<OpenSearchClient> {
public OpenSearchClient(OpenSearchTransport transport) {
super(transport, null);
}

public OpenSearchClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
super(transport, transportOptions);
}

@Override
public OpenSearchClient withTransportOptions(@Nullable TransportOptions transportOptions) {
return new OpenSearchClient(this.transport, transportOptions);
}

// ----- Endpoint: info

/**
* Returns basic information about the cluster.
*/
public InfoResponse info() throws IOException, OpenSearchException {
return this.transport.performRequest(InfoRequest._INSTANCE, InfoRequest._ENDPOINT, this.transportOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.opensearch.client.transport.Endpoint;
import org.opensearch.client.transport.endpoints.SimpleEndpoint;

// typedef: info.Request
// typedef: _global.info.Request

/**
* Returns basic information about the cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@
import org.opensearch.client.opensearch.core.GetSourceResponse;
import org.opensearch.client.opensearch.core.IndexRequest;
import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.InfoRequest;
import org.opensearch.client.opensearch.core.InfoResponse;
import org.opensearch.client.opensearch.core.MgetRequest;
import org.opensearch.client.opensearch.core.MgetResponse;
import org.opensearch.client.opensearch.core.MsearchRequest;
Expand Down Expand Up @@ -140,21 +138,12 @@
/**
* Client for the namespace.
*/
public class OpenSearchAsyncClient extends ApiClient<OpenSearchTransport, OpenSearchAsyncClient> {
public abstract class OpenSearchAsyncClientBase<Self extends OpenSearchAsyncClientBase<Self>> extends ApiClient<OpenSearchTransport, Self> {

public OpenSearchAsyncClient(OpenSearchTransport transport) {
super(transport, null);
}

public OpenSearchAsyncClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
public OpenSearchAsyncClientBase(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
super(transport, transportOptions);
}

@Override
public OpenSearchAsyncClient withTransportOptions(@Nullable TransportOptions transportOptions) {
return new OpenSearchAsyncClient(this.transport, transportOptions);
}

// ----- Child clients

public OpenSearchCatAsyncClient cat() {
Expand Down Expand Up @@ -897,17 +886,6 @@ public final <TDocument> CompletableFuture<IndexResponse> index(
return index(fn.apply(new IndexRequest.Builder<TDocument>()).build());
}

// ----- Endpoint: info

/**
* Returns basic information about the cluster.
*
*
*/
public CompletableFuture<InfoResponse> info() throws IOException, OpenSearchException {
return this.transport.performRequestAsync(InfoRequest._INSTANCE, InfoRequest._ENDPOINT, this.transportOptions);
}

// ----- Endpoint: list_point_in_time

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@
import org.opensearch.client.opensearch.core.GetSourceResponse;
import org.opensearch.client.opensearch.core.IndexRequest;
import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.InfoRequest;
import org.opensearch.client.opensearch.core.InfoResponse;
import org.opensearch.client.opensearch.core.MgetRequest;
import org.opensearch.client.opensearch.core.MgetResponse;
import org.opensearch.client.opensearch.core.MsearchRequest;
Expand Down Expand Up @@ -140,21 +138,12 @@
/**
* Client for the namespace.
*/
public class OpenSearchClient extends ApiClient<OpenSearchTransport, OpenSearchClient> {
public abstract class OpenSearchClientBase<Self extends OpenSearchClientBase<Self>> extends ApiClient<OpenSearchTransport, Self> {

public OpenSearchClient(OpenSearchTransport transport) {
super(transport, null);
}

public OpenSearchClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
public OpenSearchClientBase(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) {
super(transport, transportOptions);
}

@Override
public OpenSearchClient withTransportOptions(@Nullable TransportOptions transportOptions) {
return new OpenSearchClient(this.transport, transportOptions);
}

// ----- Child clients
public OpenSearchGenericClient generic() {
return new OpenSearchGenericClient(this.transport, this.transportOptions);
Expand Down Expand Up @@ -883,17 +872,6 @@ public final <TDocument> IndexResponse index(Function<IndexRequest.Builder<TDocu
return index(fn.apply(new IndexRequest.Builder<TDocument>()).build());
}

// ----- Endpoint: info

/**
* Returns basic information about the cluster.
*
*
*/
public InfoResponse info() throws IOException, OpenSearchException {
return this.transport.performRequest(InfoRequest._INSTANCE, InfoRequest._ENDPOINT, this.transportOptions);
}

// ----- Endpoint: list_point_in_time

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -75,40 +76,83 @@ public void render(ShapeRenderingContext ctx) throws RenderException {
shape.render(ctx);
}

var operations = getOperationsForClient();

if (operations.isEmpty()) return;

// TODO: Render clients when won't be partial and conflict with non-generated code
// new Client(this, false).render(outputDir, formatter);
// new Client(this, true).render(outputDir, formatter);
new Client(this, false, operations).render(ctx);
new Client(this, true, operations).render(ctx);
}

private Collection<RequestShape> getOperationsForClient() {
if ("core".equals(name)) return Collections.emptyList();
return ("".equals(name) ? child("core") : this).operations.values();
}

private String getClientClassName(boolean async, boolean base) {
return "OpenSearch" + Strings.toPascalCase(name) + (async ? "Async" : "") + "Client" + (base ? "Base" : "");
}

private Type getClientType(boolean async, boolean base) {
var type = Type.builder().pkg(getPackageName()).name(getClientClassName(async, base));
if (base) {
type.genericArgs(getClientType(async, false));
}
return type.build();
}

private static class Client extends Shape {
private final boolean async;
private final Collection<RequestShape> operations;

private Client(Namespace parent, boolean async) {
super(parent, "OpenSearch" + Strings.toPascalCase(parent.name) + (async ? "Async" : "") + "Client", null, null);
private Client(Namespace parent, boolean async, Collection<RequestShape> operations) {
super(parent, parent.getClientClassName(async, false), null, null);
this.async = async;
this.operations = operations;
}

@Override
public Type getExtendsType() {
return Types.Client.ApiClient(Types.Client.Transport.OpenSearchTransport, getType());
switch (parent.name) {
case "":
return parent.getClientType(async, true);
default:
return Types.Client.ApiClient(Types.Client.Transport.OpenSearchTransport, getType());
}
}

public String getName() {
return parent.name;
}

public Collection<Client> getChildren() {
return Lists.filterMap(parent.children.values(), n -> !n.operations.isEmpty(), n -> new Client(n, async));
public Collection<ClientRef> getChildren() {
return Lists.filterMap(parent.children.values(), n -> !n.getOperationsForClient().isEmpty(), n -> new ClientRef(n, async));
}

public Collection<RequestShape> getOperations() {
return parent.operations.values();
return operations;
}

public boolean isAsync() {
return this.async;
}

private static class ClientRef {
private final Type type;
private final String name;

public ClientRef(Namespace namespace, boolean async) {
this.type = namespace.getClientType(async, false);
this.name = namespace.name;
}

public Type getType() {
return type;
}

public String getName() {
return name;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static OperationGroup from(@Nonnull String operationGroup) {
return new OperationGroup(operationGroup.substring(0, index), operationGroup.substring(index + 1));
}

private OperationGroup(@Nullable String namespace, @Nonnull String name) {
public OperationGroup(@Nullable String namespace, @Nonnull String name) {
this.namespace = namespace;
this.name = Strings.requireNonBlank(name, "name must not be blank");
}
Expand All @@ -51,6 +51,11 @@ public String getName() {
return name;
}

@Nonnull
public String asTypedefPrefix() {
return (namespace == null ? "_global" : namespace) + "." + name;
}

@Override
public String toString() {
return namespace == null ? name : namespace + "." + name;
Expand Down
Loading

0 comments on commit dbde609

Please sign in to comment.