use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class DefaultValueComposer method createResourceHelperValue.
@VisibleForTesting
static Expr createResourceHelperValue(ResourceName resourceName, boolean isChildType, List<ResourceName> resnames, String fieldOrMessageName, boolean allowAnonResourceNameClass) {
if (isChildType) {
resourceName = findParentResource(resourceName, resnames).orElse(resourceName);
}
if (resourceName.isOnlyWildcard()) {
List<ResourceName> unexaminedResnames = new ArrayList<>(resnames);
for (ResourceName resname : resnames) {
unexaminedResnames.remove(resname);
if (!resname.isOnlyWildcard()) {
return createResourceHelperValue(resname, false, unexaminedResnames, fieldOrMessageName);
}
}
return allowAnonResourceNameClass ? createAnonymousResourceNameClassValue(fieldOrMessageName) : ValueExpr.withValue(StringObjectValue.withValue(String.format("%s%s", fieldOrMessageName, fieldOrMessageName.hashCode())));
}
// The cost tradeoffs of new ctors versus distinct() don't really matter here, since this list
// will usually have a very small number of elements.
List<String> patterns = new ArrayList<>(new HashSet<>(resourceName.patterns()));
boolean containsOnlyDeletedTopic = patterns.size() == 1 && patterns.get(0).equals(ResourceNameConstants.DELETED_TOPIC_LITERAL);
String ofMethodName = "of";
List<String> patternPlaceholderTokens = new ArrayList<>();
if (containsOnlyDeletedTopic) {
ofMethodName = "ofDeletedTopic";
} else {
for (String pattern : resourceName.patterns()) {
if (pattern.equals(ResourceNameConstants.WILDCARD_PATTERN) || pattern.equals(ResourceNameConstants.DELETED_TOPIC_LITERAL)) {
continue;
}
patternPlaceholderTokens.addAll(ResourceNameTokenizer.parseTokenHierarchy(Arrays.asList(pattern)).get(0));
break;
}
}
boolean hasOnePattern = resourceName.patterns().size() == 1;
if (!hasOnePattern) {
ofMethodName = String.format("of%sName", String.join("", patternPlaceholderTokens.stream().map(s -> JavaStyle.toUpperCamelCase(s)).collect(Collectors.toList())));
}
TypeNode resourceNameJavaType = resourceName.type();
List<Expr> argExprs = patternPlaceholderTokens.stream().map(s -> ValueExpr.withValue(StringObjectValue.withValue(String.format("[%s]", s.toUpperCase())))).collect(Collectors.toList());
return MethodInvocationExpr.builder().setStaticReferenceType(resourceNameJavaType).setMethodName(ofMethodName).setArguments(argExprs).setReturnType(resourceNameJavaType).build();
}
use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class GrpcServiceStubClassComposer method createRequestParamsExtractorBodyForHttpBindings.
private void createRequestParamsExtractorBodyForHttpBindings(Method method, VariableExpr requestVarExpr, List<Statement> bodyStatements, MethodInvocationExpr.Builder returnExprBuilder) {
TypeNode paramsVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(TypeNode.STRING.reference(), TypeNode.STRING.reference()).build());
VariableExpr paramsVarExpr = VariableExpr.withVariable(Variable.builder().setName("params").setType(paramsVarType).build());
Expr paramsAssignExpr = AssignmentExpr.builder().setVariableExpr(paramsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(paramsVarType).build()).build();
bodyStatements.add(ExprStatement.withExpr(paramsAssignExpr));
for (HttpBinding httpBindingFieldBinding : method.httpBindings().pathParameters()) {
MethodInvocationExpr requestBuilderExpr = createRequestFieldGetterExpr(requestVarExpr, httpBindingFieldBinding.name());
Expr valueOfExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.STRING).setMethodName("valueOf").setArguments(requestBuilderExpr).build();
Expr paramsPutExpr = MethodInvocationExpr.builder().setExprReferenceExpr(paramsVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(StringObjectValue.withValue(httpBindingFieldBinding.name())), valueOfExpr).build();
bodyStatements.add(ExprStatement.withExpr(paramsPutExpr));
}
returnExprBuilder.setExprReferenceExpr(paramsVarExpr).setMethodName("build");
}
use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class GrpcServiceStubClassComposer method createMethodDescriptorVariableDecl.
@Override
protected Statement createMethodDescriptorVariableDecl(Service service, Method protoMethod, VariableExpr methodDescriptorVarExpr, Map<String, Message> messageTypes) {
MethodInvocationExpr methodDescriptorMaker = MethodInvocationExpr.builder().setMethodName("newBuilder").setStaticReferenceType(FIXED_GRPC_TYPE_STORE.get("MethodDescriptor")).setGenerics(methodDescriptorVarExpr.variable().type().reference().generics()).build();
BiFunction<String, Expr, Function<MethodInvocationExpr, MethodInvocationExpr>> methodMakerFn = (mName, argExpr) -> m -> MethodInvocationExpr.builder().setMethodName(mName).setArguments(Arrays.asList(argExpr)).setExprReferenceExpr(m).build();
methodDescriptorMaker = methodMakerFn.apply("setType", getMethodDescriptorMethodTypeExpr(protoMethod)).apply(methodDescriptorMaker);
String codeMethodNameArg = getProtoRpcFullMethodName(service, protoMethod);
methodDescriptorMaker = methodMakerFn.apply("setFullMethodName", ValueExpr.withValue(StringObjectValue.withValue(codeMethodNameArg))).apply(methodDescriptorMaker);
Function<MethodInvocationExpr, MethodInvocationExpr> protoUtilsMarshallerFn = m -> MethodInvocationExpr.builder().setStaticReferenceType(FIXED_GRPC_TYPE_STORE.get("ProtoUtils")).setMethodName("marshaller").setArguments(Arrays.asList(m)).build();
MethodInvocationExpr methodInvocationArg = MethodInvocationExpr.builder().setMethodName("getDefaultInstance").setStaticReferenceType(protoMethod.inputType()).build();
methodDescriptorMaker = methodMakerFn.apply("setRequestMarshaller", protoUtilsMarshallerFn.apply(methodInvocationArg)).apply(methodDescriptorMaker);
methodInvocationArg = MethodInvocationExpr.builder().setMethodName("getDefaultInstance").setStaticReferenceType(protoMethod.outputType()).build();
methodDescriptorMaker = methodMakerFn.apply("setResponseMarshaller", protoUtilsMarshallerFn.apply(methodInvocationArg)).apply(methodDescriptorMaker);
methodDescriptorMaker = MethodInvocationExpr.builder().setMethodName("build").setExprReferenceExpr(methodDescriptorMaker).setReturnType(methodDescriptorVarExpr.type()).build();
return ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(methodDescriptorVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(methodDescriptorMaker).build());
}
use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class MockServiceClassComposer method createGetRequestsMethod.
private static MethodDefinition createGetRequestsMethod(VariableExpr serviceImplVarExpr, TypeStore typeStore) {
TypeNode returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(typeStore.get("AbstractMessage").reference())).build());
String methodName = "getRequests";
Expr returnExpr = MethodInvocationExpr.builder().setMethodName(methodName).setReturnType(returnType).setExprReferenceExpr(serviceImplVarExpr).build();
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(returnType).setName(methodName).setReturnExpr(returnExpr).build();
}
use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class MockServiceClassComposer method createConstructor.
private static MethodDefinition createConstructor(Service service, VariableExpr serviceImplVarExpr, TypeStore typeStore) {
String mockImplName = ClassNames.getMockServiceImplClassName(service);
Expr serviceImplAssignExpr = AssignmentExpr.builder().setVariableExpr(serviceImplVarExpr).setValueExpr(NewObjectExpr.builder().setType(typeStore.get(mockImplName)).build()).build();
return MethodDefinition.constructorBuilder().setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(serviceImplAssignExpr))).setReturnType(typeStore.get(ClassNames.getMockServiceClassName(service))).build();
}
Aggregations