Skip to content

Commit

Permalink
stub for accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Nov 7, 2024
1 parent 3080a8b commit bbce2db
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ protected Proxy processClientOperations(ServiceClient.Builder builder, List<Oper
String baseName) {
JavaSettings settings = JavaSettings.getInstance();

if (operations.isEmpty()) {
builder.clientMethods(Collections.emptyList());
return null;
}

// TODO: Assume all operations share the same base url
String baseUrl = operations.isEmpty() ? "" : getBaseUrl(operations.iterator().next());
String baseUrl = getBaseUrl(operations.iterator().next());
Proxy.Builder proxyBuilder
= getProxyBuilder().name(baseName + "Service").clientTypeName(baseName).baseURL(baseUrl);
List<ProxyMethod> restAPIMethods = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings;
import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil;
import java.util.Collections;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -80,6 +81,8 @@ public class ServiceClient {

private PipelinePolicyDetails pipelinePolicyDetails;

private List<ServiceClient> subClients;

/**
* Create a new ServiceClient with the provided properties.
*
Expand All @@ -104,7 +107,7 @@ protected ServiceClient(String packageName, String className, String interfaceNa
ClientMethodParameter httpPipelineParameter, ClientMethodParameter serializerAdapterParameter,
ClientMethodParameter defaultPollIntervalParameter, String defaultCredentialScopes, boolean builderDisabled,
String builderPackageName, SecurityInfo securityInfo, String baseUrl,
PipelinePolicyDetails pipelinePolicyDetails, String crossLanguageDefinitionId) {
PipelinePolicyDetails pipelinePolicyDetails, List<ServiceClient> subClients, String crossLanguageDefinitionId) {
this.packageName = packageName;
this.className = className;
this.interfaceName = interfaceName;
Expand All @@ -125,6 +128,7 @@ protected ServiceClient(String packageName, String className, String interfaceNa
this.securityInfo = securityInfo;
this.baseUrl = baseUrl;
this.pipelinePolicyDetails = pipelinePolicyDetails;
this.subClients = subClients;
this.crossLanguageDefinitionId = crossLanguageDefinitionId;
}

Expand Down Expand Up @@ -222,6 +226,10 @@ public PipelinePolicyDetails getPipelinePolicyDetails() {
return pipelinePolicyDetails;
}

public List<ServiceClient> getSubClients() {
return subClients;
}

public String getCrossLanguageDefinitionId() {
return crossLanguageDefinitionId;
}
Expand Down Expand Up @@ -332,6 +340,7 @@ public static class Builder {
protected SecurityInfo securityInfo;
protected String baseUrl;
protected PipelinePolicyDetails pipelinePolicyDetails;
protected List<ServiceClient> subClients = Collections.emptyList();
private String crossLanguageDefinitionId;

/**
Expand Down Expand Up @@ -543,16 +552,22 @@ public Builder pipelinePolicyDetails(PipelinePolicyDetails pipelinePolicyDetails
return this;
}

public ServiceClient build() {
return new ServiceClient(packageName, className, interfaceName, proxy, methodGroupClients, properties,
constructors, clientMethods, azureEnvironmentParameter, tokenCredentialParameter, httpPipelineParameter,
serializerAdapterParameter, defaultPollIntervalParameter, defaultCredentialScopes, builderDisabled,
builderPackageName, securityInfo, baseUrl, pipelinePolicyDetails, crossLanguageDefinitionId);
public Builder subClients(List<ServiceClient> subClients) {
this.subClients = subClients;
return this;
}

public Builder crossLanguageDefinitionId(String crossLanguageDefinitionId) {
this.crossLanguageDefinitionId = crossLanguageDefinitionId;
return this;
}

public ServiceClient build() {
return new ServiceClient(packageName, className, interfaceName, proxy, methodGroupClients, properties,
constructors, clientMethods, azureEnvironmentParameter, tokenCredentialParameter, httpPipelineParameter,
serializerAdapterParameter, defaultPollIntervalParameter, defaultCredentialScopes, builderDisabled,
builderPackageName, securityInfo, baseUrl, pipelinePolicyDetails, subClients,
crossLanguageDefinitionId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.SecurityInfo;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClient;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClientProperty;
import java.util.Collections;
import java.util.List;
import java.util.Set;

Expand All @@ -26,7 +27,7 @@ protected AndroidServiceClient(String packageName, String className, String inte
super(packageName, className, interfaceName, proxy, methodGroupClients, properties, constructors, clientMethods,
azureEnvironmentParameter, tokenCredentialParameter, httpPipelineParameter, serializerAdapterParameter,
defaultPollIntervalParameter, defaultCredentialScopes, builderDisabled, null, securityInfo, baseUrl, null,
null);
Collections.emptyList(), null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.GenericType;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodGroupClient;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClient;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClientProperty;
import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaClass;
import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaContext;
import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaFile;
Expand Down Expand Up @@ -67,6 +68,8 @@ public final void write(AsyncSyncClient asyncClient, JavaFile javaFile) {
imports.add(builderPackageName + "." + builderClassName);
addServiceClientAnnotationImports(imports);

imports.addAll(getSubClientImports(serviceClient, true));

Templates.getConvenienceAsyncMethodTemplate().addImports(imports, asyncClient.getConvenienceMethods());

javaFile.declareImport(imports);
Expand Down Expand Up @@ -133,6 +136,8 @@ public final void write(AsyncSyncClient asyncClient, JavaFile javaFile) {
});
}

writeSubClientAccessors(serviceClient, classBlock, true);

writeConvenienceMethods(asyncClient.getConvenienceMethods(), classBlock);

ServiceAsyncClientTemplate.addEndpointMethod(classBlock, asyncClient.getClientBuilder(), serviceClient,
Expand Down Expand Up @@ -207,6 +212,48 @@ static void addEndpointMethod(JavaClass classBlock, ClientBuilder clientBuilder,
}
}

static Set<String> getSubClientImports(ServiceClient serviceClient, boolean isAsync) {
Set<String> imports = new HashSet<>();
for (ServiceClient subClient : serviceClient.getSubClients()) {
final String subClientClassName = isAsync
? ClientModelUtil.clientNameToAsyncClientName(subClient.getClientBaseName())
: subClient.getClientBaseName();

final String packageName = ClientModelUtil.getAsyncSyncClientPackageName(serviceClient);
imports.add(packageName + "." + subClientClassName);
}
return imports;
}

static void writeSubClientAccessors(ServiceClient serviceClient, JavaClass classBlock, boolean isAsync) {
// Add accessors to sub clients
for (ServiceClient subClient : serviceClient.getSubClients()) {
final String subClientClassName = isAsync
? ClientModelUtil.clientNameToAsyncClientName(subClient.getClientBaseName())
: subClient.getClientBaseName();

List<ServiceClientProperty> additionalProperties
= ClientModelUtil.getAdditionalPropertiesInSubClient(serviceClient, subClient);

// TODO: do we overload the getter, if there is optional property?
classBlock.javadocComment(comment -> {
comment.description("Gets the " + subClientClassName + ".");
for (ServiceClientProperty property : additionalProperties) {
comment.param(property.getName(), property.getDescription());
}
comment.methodReturns("the " + subClientClassName);
});
String methodSignature = subClientClassName + " get" + subClientClassName + "( "
+ additionalProperties.stream()
.map(p -> p.getType() + " " + p.getName())
.collect(Collectors.joining(", "))
+ ")";
classBlock.publicMethod(methodSignature, method -> {
method.methodReturn("null");
});
}
}

private void writeConvenienceMethods(List<ConvenienceMethod> convenienceMethods, JavaClass classBlock) {
Set<GenericType> typeReferenceStaticClasses = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public final void write(AsyncSyncClient syncClient, JavaFile javaFile) {
imports.add(builderPackageName + "." + builderClassName);
addServiceClientAnnotationImport(imports);

imports.addAll(ServiceAsyncClientTemplate.getSubClientImports(serviceClient, false));

Templates.getConvenienceSyncMethodTemplate().addImports(imports, syncClient.getConvenienceMethods());

javaFile.declareImport(imports);
Expand Down Expand Up @@ -117,6 +119,8 @@ protected void writeClass(AsyncSyncClient syncClient, JavaClass classBlock, Java
});
}

ServiceAsyncClientTemplate.writeSubClientAccessors(serviceClient, classBlock, false);

writeMethods(syncClient, classBlock);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ImplementationDetails;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodGroupClient;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClient;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClientProperty;
import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaVisibility;
import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -79,7 +80,7 @@ public static void getAsyncSyncClients(Client client, ServiceClient serviceClien
boolean generateAsyncMethods = JavaSettings.getInstance().isGenerateAsyncMethods();
boolean generateSyncMethods = JavaSettings.getInstance().isGenerateSyncMethods();

if (serviceClient.getProxy() != null) {
if (serviceClient.getProxy() != null || CoreUtils.isNullOrEmpty(serviceClient.getMethodGroupClients())) {
AsyncSyncClient.Builder builder = new AsyncSyncClient.Builder().packageName(packageName)
.serviceClient(serviceClient)
.crossLanguageDefinitionId(client.getCrossLanguageDefinitionId());
Expand Down Expand Up @@ -308,6 +309,18 @@ public static String getServiceClientInterfacePackageName() {
}
}

public static List<ServiceClientProperty> getAdditionalPropertiesInSubClient(ServiceClient client,
ServiceClient subClient) {
List<ServiceClientProperty> additionalProperties = new ArrayList<>();
for (ServiceClientProperty property : subClient.getProperties()) {
String name = property.getName();
if (client.getProperties().stream().noneMatch(p -> name.equals(p.getName()))) {
additionalProperties.add(property);
}
}
return additionalProperties;
}

public static String getClientDefaultValueOrConstantValue(Parameter parameter) {
String clientDefaultValueOrConstantValue = parameter.getClientDefaultValue();
if (clientDefaultValueOrConstantValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ public ServiceClient map(Client client, CodeModel codeModel) {
.forEach(og -> methodGroupClients.add(Mappers.getMethodGroupMapper().map(og, properties)));
builder.methodGroupClients(methodGroupClients);

if (proxy == null && CoreUtils.isNullOrEmpty(methodGroupClients)) {
// No operation in this client, and no operation group as well. Abort the processing.
if (proxy == null
&& CoreUtils.isNullOrEmpty(methodGroupClients)
&& CoreUtils.isNullOrEmpty(client.getSubClients())) {
// No operation in this client, no operation group, no sub client as well. Abort the processing.
return null;
}

if (proxy == null) {
if (proxy == null && !CoreUtils.isNullOrEmpty(methodGroupClients)) {
proxy = methodGroupClients.iterator().next().getProxy();
}

Expand Down

0 comments on commit bbce2db

Please sign in to comment.