From b8d4106c88cafb56cf156bf07ca261754a2b7848 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Thu, 19 Dec 2024 10:28:50 +0800 Subject: [PATCH] unbranded Context.none() --- .../core/model/clientmodel/ClassType.java | 4 +- .../core/template/ClientMethodTemplate.java | 40 ++++++++++--------- .../example/ProtocolExampleWriter.java | 3 +- .../generator/core/util/TemplateUtil.java | 4 ++ 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java index 37e8ae191e..12c4aa605a 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClassType.java @@ -451,7 +451,9 @@ private static ClassType.Builder getClassTypeBuilder(Class classKey) { public static final ClassType INPUT_STREAM = new ClassType.Builder(false).knownClass(InputStream.class).build(); public static final ClassType CONTEXT = ClassType.getClassTypeBuilder(Context.class) - .defaultValueExpressionConverter(epr -> "com.azure.core.util.Context.NONE") + .defaultValueExpressionConverter(epr -> JavaSettings.getInstance().isBranded() + ? "com.azure.core.util.Context.NONE" + : "io.clientcore.core.util.Context.none()") .build(); public static final ClassType ANDROID_CONTEXT diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ClientMethodTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ClientMethodTemplate.java index 28cbeb60d4..b47a895ce0 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ClientMethodTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ClientMethodTemplate.java @@ -699,7 +699,8 @@ protected void generateProtocolPagingPlainSync(ClientMethod clientMethod, JavaTy function.line("RequestOptions requestOptionsForNextPage = new RequestOptions();"); function.line( - "requestOptionsForNextPage.setContext(requestOptions != null && requestOptions.getContext() != null ? requestOptions.getContext() : Context.NONE);"); + "requestOptionsForNextPage.setContext(requestOptions != null && requestOptions.getContext() != null ? requestOptions.getContext() : " + + TemplateUtil.getContextNone() + ");"); function.line("return new PagedIterable<>("); function.indent(() -> { @@ -833,7 +834,8 @@ protected void generatePagingPlainSync(ClientMethod clientMethod, JavaType typeB if (settings.isDataPlaneClient()) { function.line("RequestOptions requestOptionsForNextPage = new RequestOptions();"); function.line( - "requestOptionsForNextPage.setContext(requestOptions != null && requestOptions.getContext() != null ? requestOptions.getContext() : Context.NONE);"); + "requestOptionsForNextPage.setContext(requestOptions != null && requestOptions.getContext() != null ? requestOptions.getContext() : " + + TemplateUtil.getContextNone() + ");"); } function.line("return new PagedIterable<>("); @@ -843,12 +845,12 @@ protected void generatePagingPlainSync(ClientMethod clientMethod, JavaType typeB .replace("requestOptions", "requestOptionsForNextPage"); String firstPageArgs = clientMethod.getArgumentList(); if (clientMethod.getParameters().stream().noneMatch(p -> p.getClientType() == ClassType.CONTEXT)) { - nextMethodArgs = nextMethodArgs.replace("context", "Context.NONE"); + nextMethodArgs = nextMethodArgs.replace("context", TemplateUtil.getContextNone()); if (!CoreUtils.isNullOrEmpty(firstPageArgs)) { - firstPageArgs = firstPageArgs + ", Context.NONE"; + firstPageArgs = firstPageArgs + ", " + TemplateUtil.getContextNone(); } else { // If there are no first page arguments don't include a leading comma. - firstPageArgs = "Context.NONE"; + firstPageArgs = TemplateUtil.getContextNone(); } } String effectiveNextMethodArgs = nextMethodArgs; @@ -873,10 +875,10 @@ protected void generatePagingPlainSync(ClientMethod clientMethod, JavaType typeB String firstPageArgs = clientMethod.getArgumentList(); if (clientMethod.getParameters().stream().noneMatch(p -> p.getClientType() == ClassType.CONTEXT)) { if (!CoreUtils.isNullOrEmpty(firstPageArgs)) { - firstPageArgs = firstPageArgs + ", Context.NONE"; + firstPageArgs = firstPageArgs + ", " + TemplateUtil.getContextNone(); } else { // If there are no first page arguments don't include a leading comma. - firstPageArgs = "Context.NONE"; + firstPageArgs = TemplateUtil.getContextNone(); } } String effectiveFirstPageArgs = firstPageArgs; @@ -898,7 +900,8 @@ protected void generatePagingAsync(ClientMethod clientMethod, JavaType typeBlock if (settings.isDataPlaneClient()) { function.line("RequestOptions requestOptionsForNextPage = new RequestOptions();"); function.line( - "requestOptionsForNextPage.setContext(requestOptions != null && requestOptions.getContext() != null ? requestOptions.getContext() : Context.NONE);"); + "requestOptionsForNextPage.setContext(requestOptions != null && requestOptions.getContext() != null ? requestOptions.getContext() : " + + TemplateUtil.getContextNone() + ");"); } function.line("return new PagedFlux<>("); function.indent(() -> { @@ -972,10 +975,10 @@ private void generateSimpleSyncMethod(ClientMethod clientMethod, JavaType typeBl String argumentList = clientMethod.getArgumentList(); if (CoreUtils.isNullOrEmpty(argumentList)) { // If there are no arguments the argument is Context.NONE - argumentList = "Context.NONE"; + argumentList = TemplateUtil.getContextNone(); } else if (clientMethod.getParameters().stream().noneMatch(p -> p.getClientType() == ClassType.CONTEXT)) { // If the arguments don't contain Context append Context.NONE - argumentList += ", Context.NONE"; + argumentList += ", " + TemplateUtil.getContextNone(); } if (ClassType.STREAM_RESPONSE.equals(clientMethod.getReturnValue().getType())) { @@ -998,10 +1001,10 @@ private void generateSimplePlainSyncMethod(ClientMethod clientMethod, JavaType t String argumentList = clientMethod.getArgumentList(); if (CoreUtils.isNullOrEmpty(argumentList)) { // If there are no arguments the argument is Context.NONE - argumentList = "Context.NONE"; + argumentList = TemplateUtil.getContextNone(); } else if (clientMethod.getParameters().stream().noneMatch(p -> p.getClientType() == ClassType.CONTEXT)) { // If the arguments don't contain Context append Context.NONE - argumentList += ", Context.NONE"; + argumentList += ", " + TemplateUtil.getContextNone(); } if (clientMethod.getReturnValue().getType().equals(PrimitiveType.VOID)) { @@ -1336,7 +1339,7 @@ private static String checkAndReplaceParamNameCollision(ClientMethod clientMetho // which isn't 'Context'. This can be done by looking for the 'ProxyMethodParameter' with the // matching name and checking if it's the 'Context' parameter. parameterName = (parameter == null && "context".equals(proxyMethodArgument)) - ? "Context.NONE" + ? TemplateUtil.getContextNone() : proxyMethodArgument; } } @@ -1439,7 +1442,7 @@ protected void generateLongRunningBeginAsync(ClientMethod clientMethod, JavaType if (clientMethod.getParameters().stream().anyMatch(p -> p.getClientType().equals(ClassType.CONTEXT))) { contextParam = "context"; } else { - contextParam = "Context.NONE"; + contextParam = TemplateUtil.getContextNone(); } String pollingStrategy = getPollingStrategy(clientMethod, contextParam); typeBlock.annotation("ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)"); @@ -1488,17 +1491,17 @@ protected void generateLongRunningBeginSync(ClientMethod clientMethod, JavaType if (clientMethod.getParameters().stream().anyMatch(p -> p.getClientType().equals(ClassType.CONTEXT))) { contextParam = "context"; } else { - contextParam = "Context.NONE"; + contextParam = TemplateUtil.getContextNone(); } String pollingStrategy = getSyncPollingStrategy(clientMethod, contextParam); String argumentList = clientMethod.getArgumentList(); if (CoreUtils.isNullOrEmpty(argumentList)) { // If there are no arguments the argument is Context.NONE - argumentList = "Context.NONE"; + argumentList = TemplateUtil.getContextNone(); } else if (clientMethod.getParameters().stream().noneMatch(p -> p.getClientType() == ClassType.CONTEXT)) { // If the arguments don't contain Context append Context.NONE - argumentList += ", Context.NONE"; + argumentList += ", " + TemplateUtil.getContextNone(); } String effectiveArgumentList = argumentList; @@ -1519,7 +1522,8 @@ protected void generateLongRunningBeginSync(ClientMethod clientMethod, JavaType private void generateProtocolLongRunningBeginSync(ClientMethod clientMethod, JavaType typeBlock) { String contextParam - = "requestOptions != null && requestOptions.getContext() != null ? requestOptions.getContext() : Context.NONE"; + = "requestOptions != null && requestOptions.getContext() != null ? requestOptions.getContext() : " + + TemplateUtil.getContextNone(); String pollingStrategy = getSyncPollingStrategy(clientMethod, contextParam); typeBlock.annotation("ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)"); writeMethod(typeBlock, clientMethod.getMethodVisibility(), clientMethod.getDeclaration(), function -> { diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/example/ProtocolExampleWriter.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/example/ProtocolExampleWriter.java index 71b94a70c0..0d4264d73b 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/example/ProtocolExampleWriter.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/example/ProtocolExampleWriter.java @@ -30,6 +30,7 @@ import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaBlock; import com.microsoft.typespec.http.client.generator.core.util.CodeNamer; import com.microsoft.typespec.http.client.generator.core.util.ModelExampleUtil; +import com.microsoft.typespec.http.client.generator.core.util.TemplateUtil; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -222,7 +223,7 @@ public ProtocolExampleWriter(ProtocolExample protocolExample) { if (parameter.getClientType() == ClassType.REQUEST_OPTIONS) { params.set(i, "requestOptions"); } else if (parameter.getClientType() == ClassType.CONTEXT) { - params.set(i, "Context.NONE"); + params.set(i, TemplateUtil.getContextNone()); } } String methodCall = String.format("%s.%s(%s)", clientVarName, method.getName(), String.join(", ", params)); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/TemplateUtil.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/TemplateUtil.java index 55b008078e..d511f7e3d9 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/TemplateUtil.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/TemplateUtil.java @@ -370,4 +370,8 @@ public static String escapeString(String str) { builder.append(str, last, str.length()); return builder.toString(); } + + public static String getContextNone() { + return JavaSettings.getInstance().isBranded() ? "Context.NONE" : "Context.none()"; + } }