Skip to content

Commit

Permalink
unbranded Context.none()
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Dec 19, 2024
1 parent 8e66c0b commit b8d4106
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(() -> {
Expand Down Expand Up @@ -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<>(");

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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(() -> {
Expand Down Expand Up @@ -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())) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)");
Expand Down Expand Up @@ -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;
Expand All @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()";
}
}

0 comments on commit b8d4106

Please sign in to comment.