use of com.google.api.generator.gapic.model.HttpBindings.HttpBinding 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.gapic.model.HttpBindings.HttpBinding in project gapic-generator-java by googleapis.
the class HttpJsonServiceStubClassComposer method createBodyFieldsExtractorClassInstance.
private Expr createBodyFieldsExtractorClassInstance(Method method, TypeNode extractorReturnType, Set<HttpBinding> httpBindingFieldNames, String serializerMethodName, boolean asteriskBody) {
List<Statement> bodyStatements = new ArrayList<>();
Expr returnExpr = null;
Expr serializerExpr = MethodInvocationExpr.builder().setMethodName("create").setStaticReferenceType(FIXED_REST_TYPESTORE.get(ProtoRestSerializer.class.getSimpleName())).build();
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
Expr bodyRequestExpr = requestVarExpr;
String requestMethodPrefix = "get";
String bodyParamName = null;
if (asteriskBody) {
bodyRequestExpr = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr).setMethodName("toBuilder").build();
// In case of `body: "*"` case we send the whole request message as a body, minus the fields
// in the path, therefore the "clear" prefix here.
requestMethodPrefix = "clear";
}
Expr prevExpr = bodyRequestExpr;
for (HttpBinding httpBindingFieldName : httpBindingFieldNames) {
// Handle foo.bar cases by descending into the subfields.
MethodInvocationExpr.Builder requestFieldMethodExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(prevExpr);
bodyParamName = JavaStyle.toLowerCamelCase(httpBindingFieldName.name());
String[] descendantFields = httpBindingFieldName.name().split("\\.");
if (asteriskBody && descendantFields.length > 1) {
// well-defined case, and it is generally safer to send more than less in such case.
continue;
}
for (int i = 0; i < descendantFields.length; i++) {
String currFieldName = descendantFields[i];
String bindingFieldMethodName = String.format("%s%s", requestMethodPrefix, JavaStyle.toUpperCamelCase(currFieldName));
requestFieldMethodExprBuilder = requestFieldMethodExprBuilder.setMethodName(bindingFieldMethodName);
if (i < descendantFields.length - 1) {
requestFieldMethodExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestFieldMethodExprBuilder.build());
}
}
prevExpr = requestFieldMethodExprBuilder.build();
}
if (httpBindingFieldNames.isEmpty() && !asteriskBody) {
returnExpr = ValueExpr.createNullExpr();
} else {
ImmutableList.Builder<Expr> paramsPutArgs = ImmutableList.builder();
if (asteriskBody) {
prevExpr = MethodInvocationExpr.builder().setExprReferenceExpr(prevExpr).setMethodName("build").build();
bodyParamName = "*";
}
paramsPutArgs.add(ValueExpr.withValue(StringObjectValue.withValue(bodyParamName)));
paramsPutArgs.add(prevExpr);
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serializerExpr).setMethodName(serializerMethodName).setArguments(paramsPutArgs.build()).setReturnType(extractorReturnType).build();
}
// (https://github.com/googleapis/gax-java/blob/12b18ee255d3fabe13bb3969df40753b29f830d5/gax-httpjson/src/main/java/com/google/api/gax/httpjson/FieldsExtractor.java).
return LambdaExpr.builder().setArguments(requestVarExpr.toBuilder().setIsDecl(true).build()).setBody(bodyStatements).setReturnExpr(returnExpr).build();
}
use of com.google.api.generator.gapic.model.HttpBindings.HttpBinding in project gapic-generator-java by googleapis.
the class HttpJsonServiceStubClassComposer method createFieldsExtractorClassInstance.
private Expr createFieldsExtractorClassInstance(Method method, TypeNode extractorReturnType, Set<HttpBinding> httpBindingFieldNames, String serializerMethodName) {
List<Statement> bodyStatements = new ArrayList<>();
VariableExpr fieldsVarExpr = VariableExpr.withVariable(Variable.builder().setName("fields").setType(extractorReturnType).build());
Expr fieldsAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_REST_TYPESTORE.get(HashMap.class.getSimpleName())).setIsGeneric(true).build()).build();
bodyStatements.add(ExprStatement.withExpr(fieldsAssignExpr));
TypeNode serializerVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(ProtoRestSerializer.class).setGenerics(method.inputType().reference()).build());
VariableExpr serializerVarExpr = VariableExpr.withVariable(Variable.builder().setName("serializer").setType(serializerVarType).build());
Expr serializerAssignExpr = AssignmentExpr.builder().setVariableExpr(serializerVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_REST_TYPESTORE.get(ProtoRestSerializer.class.getSimpleName())).setMethodName("create").setReturnType(serializerVarType).build()).build();
bodyStatements.add(ExprStatement.withExpr(serializerAssignExpr));
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
for (HttpBinding httpBindingFieldName : httpBindingFieldNames) {
// Handle foo.bar cases by descending into the subfields.
MethodInvocationExpr.Builder requestFieldGetterExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr);
MethodInvocationExpr.Builder requestFieldHasExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr);
String[] descendantFields = httpBindingFieldName.name().split("\\.");
for (int i = 0; i < descendantFields.length; i++) {
String currFieldName = descendantFields[i];
String bindingFieldMethodName = String.format("get%s", JavaStyle.toUpperCamelCase(currFieldName));
requestFieldGetterExprBuilder = requestFieldGetterExprBuilder.setMethodName(bindingFieldMethodName);
String bindingFieldHasMethodName = (i < descendantFields.length - 1) ? bindingFieldMethodName : String.format("has%s", JavaStyle.toUpperCamelCase(currFieldName));
requestFieldHasExprBuilder = requestFieldHasExprBuilder.setMethodName(bindingFieldHasMethodName).setReturnType(TypeNode.BOOLEAN);
if (i < descendantFields.length - 1) {
requestFieldGetterExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestFieldGetterExprBuilder.build());
requestFieldHasExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestFieldHasExprBuilder.build());
}
}
MethodInvocationExpr requestBuilderExpr = requestFieldGetterExprBuilder.build();
MethodInvocationExpr requestHasExpr = requestFieldHasExprBuilder.build();
ImmutableList.Builder<Expr> paramsPutArgs = ImmutableList.builder();
paramsPutArgs.add(fieldsVarExpr);
paramsPutArgs.add(ValueExpr.withValue(StringObjectValue.withValue(JavaStyle.toLowerCamelCase(httpBindingFieldName.name()))));
paramsPutArgs.add(requestBuilderExpr);
Expr paramsPutExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serializerVarExpr).setMethodName(serializerMethodName).setArguments(paramsPutArgs.build()).setReturnType(extractorReturnType).build();
if (httpBindingFieldName.isOptional()) {
bodyStatements.add(IfStatement.builder().setConditionExpr(requestHasExpr).setBody(Arrays.asList(ExprStatement.withExpr(paramsPutExpr))).build());
} else {
bodyStatements.add(ExprStatement.withExpr(paramsPutExpr));
}
}
// (https://github.com/googleapis/gax-java/blob/12b18ee255d3fabe13bb3969df40753b29f830d5/gax-httpjson/src/main/java/com/google/api/gax/httpjson/FieldsExtractor.java).
return LambdaExpr.builder().setArguments(requestVarExpr.toBuilder().setIsDecl(true).build()).setBody(bodyStatements).setReturnExpr(fieldsVarExpr).build();
}
use of com.google.api.generator.gapic.model.HttpBindings.HttpBinding in project gapic-generator-java by googleapis.
the class HttpRuleParser method validateAndConstructHttpBindings.
private static Set<HttpBinding> validateAndConstructHttpBindings(Set<String> paramNames, Message inputMessage, Map<String, Message> messageTypes, Map<String, String> patternSampleValues) {
ImmutableSortedSet.Builder<HttpBinding> httpBindings = ImmutableSortedSet.naturalOrder();
for (String paramName : paramNames) {
// Handle foo.bar cases by descending into the subfields.
String patternSampleValue = patternSampleValues != null ? patternSampleValues.get(paramName) : null;
String[] subFields = paramName.split("\\.");
if (inputMessage == null) {
httpBindings.add(HttpBinding.create(paramName, false, patternSampleValue));
continue;
}
Message nestedMessage = inputMessage;
for (int i = 0; i < subFields.length; i++) {
String subFieldName = subFields[i];
if (i < subFields.length - 1) {
Field field = nestedMessage.fieldMap().get(subFieldName);
nestedMessage = messageTypes.get(field.type().reference().fullName());
Preconditions.checkNotNull(nestedMessage, String.format("No containing message found for field %s with type %s", field.name(), field.type().reference().simpleName()));
} else {
if (patternSampleValues != null) {
checkHttpFieldIsValid(subFieldName, nestedMessage, false);
patternSampleValue = patternSampleValues.get(paramName);
}
Field field = nestedMessage.fieldMap().get(subFieldName);
httpBindings.add(HttpBinding.create(paramName, field.isProto3Optional(), patternSampleValue));
}
}
}
return httpBindings.build();
}
Aggregations