Skip to content

Commit

Permalink
validateBuilder and validateClient
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed May 29, 2024
1 parent 1bc3958 commit 8cec837
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,15 @@ public final void write(ClientBuilder clientBuilder, JavaFile javaFile) {
JavaVisibility visibility = settings.isGenerateSyncAsyncClients() ? JavaVisibility.Private : JavaVisibility.Public;

// build method
classBlock.javadocComment(comment ->
{
classBlock.javadocComment(comment -> {
comment.description(String.format("Builds an instance of %1$s with the provided parameters", buildReturnType));
comment.methodReturns(String.format("an instance of %1$s", buildReturnType));
});
addGeneratedAnnotation(classBlock);
classBlock.method(visibility, null, String.format("%1$s %2$s()", buildReturnType, buildMethodName), function ->
{
classBlock.method(visibility, null, String.format("%1$s %2$s()", buildReturnType, buildMethodName), function -> {
List<ServiceClientProperty> allProperties = mergeClientPropertiesWithTraits(
clientProperties,
settings.isAzureOrFluent() ? null : clientBuilder.getBuilderTraits());
clientProperties,
settings.isAzureOrFluent() ? null : clientBuilder.getBuilderTraits());

for (ServiceClientProperty serviceClientProperty : allProperties) {
if (serviceClientProperty.getDefaultValueExpression() != null
Expand All @@ -275,6 +273,10 @@ public final void write(ClientBuilder clientBuilder, JavaFile javaFile) {
}
}

if (!settings.isAzureOrFluent()) {
function.line("this.validateClient();");
}

// additional service client properties in constructor arguments
String constructorArgs = serviceClient.getProperties().stream()
.filter(p -> !p.isReadOnly())
Expand Down Expand Up @@ -318,7 +320,8 @@ public final void write(ClientBuilder clientBuilder, JavaFile javaFile) {

if (!settings.isAzureOrFluent()) {
List<ServiceClientProperty> allProperties = mergeClientPropertiesWithTraits(clientProperties, clientBuilder.getBuilderTraits());
addValidateBuilderMethod(classBlock, allProperties);
addValidateClientMethod(classBlock, allProperties);
addValidatePipelineMethod(classBlock);

addCreateHttpPipelineMethod(settings, classBlock, serviceClient.getDefaultCredentialScopes(), serviceClient.getSecurityInfo(), serviceClient.getPipelinePolicyDetails());
}
Expand Down Expand Up @@ -529,14 +532,21 @@ protected void addCreateHttpPipelineMethod(JavaSettings settings, JavaClass clas
});
}

private void addValidateBuilderMethod(JavaClass classBlock, List<ServiceClientProperty> properties) {
private void addValidatePipelineMethod(JavaClass classBlock) {
addGeneratedAnnotation(classBlock);
classBlock.privateMethod("void validatePipeline()", methodBlock -> {
methodBlock.line("// This method is invoked from 'createHttpPipeline' method.");
methodBlock.line("// Developer can customize this method, to validate that the necessary conditions are met for the new HTTP pipeline.");
});
}

private void addValidateClientMethod(JavaClass classBlock, List<ServiceClientProperty> properties) {
addGeneratedAnnotation(classBlock);
classBlock.privateMethod("void validateBuilder()", methodBlock -> {
methodBlock.line("// This method is invoked from 'createHttpPipeline' when preparing the HTTP pipeline for the new client.");
classBlock.privateMethod("void validateClient()", methodBlock -> {
methodBlock.line("// This method is invoked from 'buildInnerClient'/'buildClient' method.");
methodBlock.line("// Developer can customize this method, to validate that the necessary conditions are met for the new client.");
for (ServiceClientProperty property : properties) {
// ServiceVersion is also provided with a defaultValueExpression
if (property.isRequired() && property.getDefaultValueExpression() == null) {
if (property.isRequired()) {
methodBlock.line("Objects.requireNonNull(" + property.getName() + ", \"'" + property.getName() + "' cannot be null.\");");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static String getByteCloneExpression(String propertyName) {
}

public static void createHttpPipelineMethod(JavaSettings settings, String defaultCredentialScopes, SecurityInfo securityInfo, PipelinePolicyDetails pipelinePolicyDetails, JavaBlock function) {
function.line("this.validateBuilder();");
function.line("this.validatePipeline();");
if (!settings.isBranded()) {
createGenericHttpPipelineMethod(settings, defaultCredentialScopes, securityInfo, pipelinePolicyDetails, function);
} else {
Expand Down

0 comments on commit 8cec837

Please sign in to comment.