Skip to content

Commit

Permalink
impl of the stub
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Nov 7, 2024
1 parent a27756c commit 4523e98
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ClientAccessorMethod {
private ServiceClient serviceClient;

/**
* Initializes the ClientAccessorMethod
* Initializes the ClientAccessorMethod.
*
* @param subClient the ServiceClient of the sub client
*/
Expand All @@ -30,14 +30,32 @@ public ClientAccessorMethod(ServiceClient subClient) {
}

/**
* Sets the ServiceClient
* Sets the ServiceClient containing this accessor method.
*
* @param serviceClient the ServiceClient containing this accessor method
*/
public void setServiceClient(ServiceClient serviceClient) {
this.serviceClient = serviceClient;
}

/**
* Gets the ServiceClient containing this accessor method.
*
* @return the ServiceClient containing this accessor method
*/
public ServiceClient getServiceClient() {
return serviceClient;
}

/**
* Gets the sub ServiceClient.
*
* @return the sub ServiceClient
*/
public ServiceClient getSubClient() {
return subClient;
}

public List<ServiceClientProperty> getAccessorProperties() {
List<ServiceClientProperty> additionalProperties = new ArrayList<>();
for (ServiceClientProperty property : subClient.getProperties()) {
Expand Down Expand Up @@ -67,8 +85,17 @@ public void addImportsTo(Set<String> imports, boolean includeImplementationImpor
}
}

public String getName() {
return "get" + subClient.getClientBaseName();
}

public String getDeclaration() {
return null;
String subClientClassName = subClient.getClassName();
List<ServiceClientProperty> additionalProperties = this.getAccessorProperties();

return subClientClassName + " " + getName() + "("
+ additionalProperties.stream().map(p -> p.getType() + " " + p.getName()).collect(Collectors.joining(", "))
+ ")";
}

public String getAsyncSyncClientName(boolean isAsync) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,20 @@ static void writeSubClientAccessors(ServiceClient serviceClient, JavaClass class
List<ServiceClientProperty> accessorProperties = clientAccessorMethod.getAccessorProperties();
String subClientClassName = clientAccessorMethod.getAsyncSyncClientName(isAsync);

String serviceClientMethodCall = "serviceClient." + clientAccessorMethod.getName() + "("
+ accessorProperties.stream().map(ServiceClientProperty::getName).collect(Collectors.joining(", "))
+ ")";

// expect all properties are required, so no overload
classBlock.javadocComment(comment -> {
comment.description("Gets the " + subClientClassName + ".");
comment.description("Gets an instance of " + subClientClassName + " class.");
for (ServiceClientProperty property : accessorProperties) {
comment.param(property.getName(), property.getDescription());
}
comment.methodReturns("the " + subClientClassName);
comment.methodReturns("an instance of " + subClientClassName + "class");
});
classBlock.publicMethod(clientAccessorMethod.getAsyncSyncClientDeclaration(isAsync), method -> {
method.methodReturn("null");
method.methodReturn("new " + subClientClassName + "(" + serviceClientMethodCall + ")");
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Annotation;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientAccessorMethod;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientMethodParameter;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Constructor;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.MethodGroupClient;
Expand Down Expand Up @@ -319,6 +320,8 @@ public final void write(ServiceClient serviceClient, JavaFile javaFile) {

this.writeAdditionalClassBlock(classBlock);

writeClientAccessorMethods(classBlock, serviceClient.getClientAccessorMethods());

if (settings.isUseClientLogger()) {
TemplateUtil.addClientLogger(classBlock, serviceClient.getClassName(), javaFile.getContents());
}
Expand Down Expand Up @@ -367,4 +370,51 @@ protected void addServiceClientAnnotationImport(Set<String> imports) {
*/
protected void writeAdditionalClassBlock(JavaClass classBlock) {
}

private static void writeClientAccessorMethods(JavaClass classBlock,
List<ClientAccessorMethod> clientAccessorMethods) {
for (ClientAccessorMethod clientAccessorMethod : clientAccessorMethods) {
final String subClientName = clientAccessorMethod.getSubClient().getClassName();
final List<ServiceClientProperty> accessorProperties = clientAccessorMethod.getAccessorProperties();
final List<String> arguments = new ArrayList<>();

// pre-defined properties like "httpPipeline"
List<Constructor> parentConstructors = clientAccessorMethod.getServiceClient().getConstructors();
// take the last, which is the maximum overload
Constructor parentConstructor
= clientAccessorMethod.getServiceClient().getConstructors().get(parentConstructors.size() - 1);
for (ClientMethodParameter parameter : parentConstructor.getParameters()) {
arguments.add("this." + parameter.getName());
}
// other properties from parent client
for (ServiceClientProperty property : clientAccessorMethod.getServiceClient().getProperties()) {
if (!property.isReadOnly()) {
arguments.add("this." + property.getName());
}
}
// properties from method
for (ServiceClientProperty property : accessorProperties) {
arguments.add(property.getName());
}

classBlock.javadocComment(comment -> {
comment.description("Gets an instance of " + subClientName + " class.");
for (ServiceClientProperty property : accessorProperties) {
comment.param(property.getName(), property.getDescription());
}
comment.methodReturns("an instance of " + subClientName + "class");
});
classBlock.publicMethod(clientAccessorMethod.getDeclaration(), method -> {
for (ServiceClientProperty property : accessorProperties) {
if (property.isRequired()) {
method.line("Objects.requireNonNull(" + property.getName() + ", \"'" + property.getName()
+ "' cannot be null.\");");
}
}

method.methodReturn(
"new " + subClientName + "(" + arguments.stream().collect(Collectors.joining(", ")) + ")");
});
}
}
}

0 comments on commit 4523e98

Please sign in to comment.