use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createFixedClassMemberVariables.
private static Map<String, VariableExpr> createFixedClassMemberVariables() {
Map<String, TypeNode> memberVars = new HashMap<>();
Reference stringRef = ConcreteReference.withClazz(String.class);
memberVars.put("fieldValuesMap", TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(stringRef, stringRef)).build()));
memberVars.put("pathTemplate", TypeNode.withReference(ConcreteReference.withClazz(PathTemplate.class)));
memberVars.put("fixedValue", TypeNode.STRING);
return memberVars.entrySet().stream().map(e -> Variable.builder().setName(e.getKey()).setType(e.getValue()).build()).collect(Collectors.toMap(v -> v.identifier().name(), v -> VariableExpr.withVariable(v)));
}
use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createGetFieldValuesMapMethod.
private static MethodDefinition createGetFieldValuesMapMethod(ResourceName resourceName, TypeNode thisClassType, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies) {
Reference strRef = TypeNode.STRING.reference();
TypeNode mapBuilderType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(Arrays.asList(strRef, strRef)).build());
VariableExpr fieldMapBuilderVarExpr = VariableExpr.withVariable(Variable.builder().setName("fieldMapBuilder").setType(mapBuilderType).build());
AssignmentExpr builderAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldMapBuilderVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(mapBuilderType).build()).build();
// Special-cased PubSub handling.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(tokens -> !tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
// Innermost if-blocks.
List<Statement> tokenIfStatements = new ArrayList<>();
for (String token : getTokenSet(processedTokenHierarchies)) {
VariableExpr tokenVarExpr = patternTokenVarExprs.get(token);
Preconditions.checkNotNull(tokenVarExpr, String.format("No variable found for %s among %s", token, patternTokenVarExprs.keySet()));
StringObjectValue tokenStrVal = StringObjectValue.withValue(token);
MethodInvocationExpr putExpr = MethodInvocationExpr.builder().setExprReferenceExpr(fieldMapBuilderVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(tokenStrVal), tokenVarExpr).build();
Expr notNullCheckExpr = RelationalOperationExpr.notEqualToWithExprs(tokenVarExpr, ValueExpr.createNullExpr());
tokenIfStatements.add(IfStatement.builder().setConditionExpr(notNullCheckExpr).setBody(Arrays.asList(ExprStatement.withExpr(putExpr))).build());
}
// Put the innermost if-statements and assignment expressions together.
VariableExpr fieldValuesMapVarExpr = FIXED_CLASS_VARS.get("fieldValuesMap");
AssignmentExpr fieldValuesMapAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldValuesMapVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(fieldMapBuilderVarExpr).setMethodName("build").setReturnType(fieldValuesMapVarExpr.type()).build()).build();
List<Statement> middleIfBlockStatements = new ArrayList<>();
middleIfBlockStatements.add(ExprStatement.withExpr(builderAssignExpr));
middleIfBlockStatements.addAll(tokenIfStatements);
middleIfBlockStatements.add(ExprStatement.withExpr(fieldValuesMapAssignExpr));
// Middle if-block, i.e. `if (fieldValuesMap == null)`.
Expr fieldValuesMapNullCheckExpr = RelationalOperationExpr.equalToWithExprs(fieldValuesMapVarExpr, ValueExpr.createNullExpr());
IfStatement fieldValuesMapIfStatement = IfStatement.builder().setConditionExpr(fieldValuesMapNullCheckExpr).setBody(middleIfBlockStatements).build();
// Outer if-block.
IfStatement outerIfStatement = IfStatement.builder().setConditionExpr(fieldValuesMapNullCheckExpr).setBody(Arrays.asList(SynchronizedStatement.builder().setLock(ThisObjectValue.withType(thisClassType)).setBody(Arrays.asList(fieldValuesMapIfStatement)).build())).build();
// Put the method together.
TypeNode mapStringType = fieldValuesMapVarExpr.type();
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(mapStringType).setName("getFieldValuesMap").setBody(Arrays.asList(outerIfStatement)).setReturnExpr(fieldValuesMapVarExpr).build();
}
use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class TypeParser method createMapType.
@VisibleForTesting
static TypeNode createMapType(FieldDescriptor field) {
Preconditions.checkState(field.isMapField(), "createMapType can only be called on map-type fields");
Descriptor type = field.getMessageType();
FieldDescriptor keyField = type.findFieldByName("key");
FieldDescriptor valueField = type.findFieldByName("value");
Reference keyReference = parseFieldReference(keyField);
Reference valueReference = parseFieldReference(valueField);
Reference mapReference = ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(keyReference, valueReference)).build();
return TypeNode.withReference(mapReference);
}
use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeMethodDefinitionImports_templatedMixedNamesAndTypes.
@Test
public void writeMethodDefinitionImports_templatedMixedNamesAndTypes() {
Reference mapRef = ConcreteReference.withClazz(Map.class);
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("K", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("T", "V")).build());
TypeNode returnType = TypeNode.withReference(mapRef);
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(returnType).setTemplateNames(Arrays.asList("T", "K", "V")).setReturnTemplateNames(Arrays.asList("K", "V")).setArguments(arguments).setReturnExpr(MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(returnType).build()).build();
methodDefinition.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import java.util.Map;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.
the class Message method findAndUnwrapPaginatedRepeatedField.
/**
* Returns the first list repeated field in a message, unwrapped from its list type.
*/
@Nullable
public Field findAndUnwrapPaginatedRepeatedField() {
for (Field field : fields()) {
if (field.isMap()) {
List<Reference> repeatedGenericMapRefs = field.type().reference().generics();
TypeNode paginatedType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.Entry.class).setGenerics(Arrays.asList(repeatedGenericMapRefs.get(0), repeatedGenericMapRefs.get(1))).build());
return field.toBuilder().setType(paginatedType).build();
}
}
for (Field field : fields()) {
if (field.isRepeated() && !field.isMap()) {
Reference repeatedGenericRef = field.type().reference().generics().get(0);
return field.toBuilder().setType(TypeNode.withReference(repeatedGenericRef)).build();
}
}
return null;
}
Aggregations