Skip to content

Commit

Permalink
slight refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alicejli committed Jan 25, 2024
1 parent 2c7ea7e commit 06e956c
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Generated;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -325,8 +326,7 @@ protected Expr createTransportSettingsInitExpr(
.build();
}

if (method.hasAutoPopulatedFields()
&& Boolean.TRUE.equals(shouldAutoPopulateFields(method, messageTypes))) {
if (method.hasAutoPopulatedFields() && shouldGenerateRequestMutator(method, messageTypes)) {
callSettingsBuilderExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(callSettingsBuilderExpr)
Expand Down Expand Up @@ -1315,33 +1315,27 @@ static List<Statement> createRequestMutatorBody(
List<Statement> bodyStatements,
VariableExpr returnBuilderVarExpr) {

if (method.inputType().reference() == null
|| method.inputType().reference().fullName() == null) {
return bodyStatements;
}
String methodRequestName = method.inputType().reference().fullName();
if (Strings.isNullOrEmpty(methodRequestName)) {
return bodyStatements;
}
Message methodRequestMessage = messageTypes.get(methodRequestName);
if (methodRequestMessage == null || methodRequestMessage.fields() == null) {
if (!shouldGenerateRequestMutator(method, messageTypes)) {
return bodyStatements;
}
for (String field : method.autoPopulatedFields()) {
Optional<Field> matchingField =
methodRequestMessage.fields().stream()
.filter(field1 -> field1.name().equals(field))
.findFirst();
if (!matchingField.isPresent()) {
continue;
}
Field matchedField = matchingField.get();
if (matchedField.shouldAutoPopulate()) {
bodyStatements.add(
// Chain If statements based on number of autopopulated fields
createAutoPopulatedRequestStatement(method, matchedField.name(), returnBuilderVarExpr));
}
}
Message methodRequestMessage = messageTypes.get(method.inputType().reference().fullName());
method.autoPopulatedFields().stream()
// Map each field name to its corresponding Field object, if present
.map(
fieldName ->
methodRequestMessage.fields().stream()
.filter(field -> field.name().equals(fieldName))
.findFirst())
.filter(Optional::isPresent) // Keep only the existing Fields
.map(Optional::get) // Extract the Field from the Optional
.filter(Field::canBeAutoPopulated) // Filter fields that can be autopopulated
.forEach(
matchedField -> {
// Create statements for each autopopulated Field
bodyStatements.add(
createAutoPopulatedRequestStatement(
method, matchedField.name(), returnBuilderVarExpr));
});
return bodyStatements;
}

Expand Down Expand Up @@ -1418,36 +1412,29 @@ static Statement createAutoPopulatedRequestStatement(
}

@VisibleForTesting
static Boolean shouldAutoPopulateFields(
static Boolean shouldGenerateRequestMutator(
Method method, ImmutableMap<String, Message> messageTypes) {

boolean shouldAutoPopulate = false;
if (method.inputType().reference() == null
|| method.inputType().reference().fullName() == null) {
return shouldAutoPopulate;
return false;
}
String methodRequestName = method.inputType().reference().fullName();
if (Strings.isNullOrEmpty(methodRequestName)) {
return shouldAutoPopulate;
}

Message methodRequestMessage = messageTypes.get(methodRequestName);
if (methodRequestMessage == null || methodRequestMessage.fields() == null) {
return shouldAutoPopulate;
return false;
}
for (String field : method.autoPopulatedFields()) {
Optional<Field> matchingField =
methodRequestMessage.fields().stream()
.filter(field1 -> field1.name().equals(field))
.findFirst();
if (!matchingField.isPresent()) {
continue;
}
Field matchedField = matchingField.get();
if (matchedField.shouldAutoPopulate()) {
shouldAutoPopulate = true;
}
}
return shouldAutoPopulate;
return method.autoPopulatedFields().stream().anyMatch(shouldAutoPopulate(methodRequestMessage));
}

/**
* The field has to exist in the Message and properly configured in the Message(see {@link
* Field#canBeAutoPopulated()})
*/
private static Predicate<String> shouldAutoPopulate(Message methodRequestMessage) {
return fieldName ->
methodRequestMessage.fields().stream()
.anyMatch(field -> field.name().equals(fieldName) && field.canBeAutoPopulated());
}

protected LambdaExpr createRequestParamsExtractorClassInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean hasResourceReference() {
// those three conditions are met, do not autopopulate the field.
// In the future, if additional formats are supported for autopopulation, this will need to be
// refactored to support those formats.
public boolean shouldAutoPopulate() {
public boolean canBeAutoPopulated() {
return Format.UUID4.equals(fieldInfoFormat())
&& !isRequired()
&& TypeNode.STRING.equals(type());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@

package com.google.api.generator.gapic.composer.common;

import static com.google.api.generator.gapic.composer.common.AbstractTransportServiceStubClassComposer.shouldGenerateRequestMutator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.api.FieldInfo.Format;
import com.google.api.generator.engine.ast.LambdaExpr;
Expand Down Expand Up @@ -59,6 +62,91 @@ public void setUp() {
writerVisitor = new JavaWriterVisitor();
}

@Test
public void shouldGenerateRequestMutator_fieldConfiguredCorrectly() {
String ECHO_PACKAGE = "com.google.showcase.v1beta1";
List<String> autoPopulatedFieldList = new ArrayList<>();
autoPopulatedFieldList.add("TestField");

Method METHOD =
Method.builder()
.setName("TestMethod")
.setInputType(
TypeNode.withReference(
VaporReference.builder()
.setName("SampleRequest")
.setPakkage(ECHO_PACKAGE)
.build()))
.setOutputType(TypeNode.STRING)
.setAutoPopulatedFields(autoPopulatedFieldList)
.build();

Field FIELD =
Field.builder()
.setName("TestField")
.setFieldInfoFormat(Format.UUID4)
.setType(TypeNode.STRING)
.build();
List<Field> fieldList = new ArrayList<>();
fieldList.add(FIELD);

Message MESSAGE =
Message.builder()
.setFullProtoName("com.google.showcase.v1beta1.SampleRequest")
.setName("SampleRequest")
.setType(TypeNode.STRING)
.setFields(fieldList)
.build();

ImmutableMap<String, Message> messageTypes =
ImmutableMap.of("com.google.showcase.v1beta1.SampleRequest", MESSAGE);

assertTrue(shouldGenerateRequestMutator(METHOD, messageTypes));
}

@Test
public void shouldNotGenerateRequestMutator_fieldConfiguredIncorrectly() {
String ECHO_PACKAGE = "com.google.showcase.v1beta1";
List<String> autoPopulatedFieldList = new ArrayList<>();
autoPopulatedFieldList.add("TestField");

Method METHOD =
Method.builder()
.setName("TestMethod")
.setInputType(
TypeNode.withReference(
VaporReference.builder()
.setName("SampleRequest")
.setPakkage(ECHO_PACKAGE)
.build()))
.setOutputType(TypeNode.STRING)
.setAutoPopulatedFields(autoPopulatedFieldList)
.build();

Field FIELD =
Field.builder()
.setName("TestField")
.setFieldInfoFormat(Format.IPV6)
.setType(TypeNode.STRING)
.build();
List<Field> fieldList = new ArrayList<>();
fieldList.add(FIELD);

Message MESSAGE =
Message.builder()
.setFullProtoName("com.google.showcase.v1beta1.SampleRequest")
.setName("SampleRequest")
.setType(TypeNode.STRING)
.setFields(fieldList)
.build();

ImmutableMap<String, Message> messageTypes =
ImmutableMap.of("com.google.showcase.v1beta1.SampleRequest", MESSAGE);

assertFalse(shouldGenerateRequestMutator(METHOD, messageTypes));
}

// TODO: add unit tests where the field is not found in the messageTypes map
@Test
public void createAutoPopulatedRequestStatement_sampleField() {
Reference RequestBuilderRef =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void shouldAutoPopulate() {
.setType(TypeNode.STRING)
.build();

assertEquals(true, FIELD.shouldAutoPopulate());
assertEquals(true, FIELD.canBeAutoPopulated());
}

@Test
Expand All @@ -45,7 +45,7 @@ public void isRequired_shouldNotAutoPopulate() {
.setType(TypeNode.STRING)
.build();

assertEquals(false, FIELD.shouldAutoPopulate());
assertEquals(false, FIELD.canBeAutoPopulated());
}

@Test
Expand All @@ -58,7 +58,7 @@ public void fieldInfoFormatNotUUID4_shouldNotAutoPopulate() {
.setType(TypeNode.STRING)
.build();

assertEquals(false, FIELD.shouldAutoPopulate());
assertEquals(false, FIELD.canBeAutoPopulated());
}

@Test
Expand All @@ -71,6 +71,6 @@ public void typeNotString_shouldNotAutoPopulate() {
.setType(TypeNode.BOOLEAN)
.build();

assertEquals(false, FIELD.shouldAutoPopulate());
assertEquals(false, FIELD.canBeAutoPopulated());
}
}

0 comments on commit 06e956c

Please sign in to comment.