Skip to content

Commit

Permalink
[ggj][resname][codegen] fix: pass all tokens to instansiate in resnam…
Browse files Browse the repository at this point in the history
…e 1-pattern case (#437)

* fix: pass all tokens to instansiate in resname 1-pattern case

* fix: update goldens

* fix: update goldens
  • Loading branch information
miraleung authored Oct 31, 2020
1 parent 96f8676 commit ae7e1b6
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1090,21 +1090,25 @@ private static MethodDefinition createToStringMethod(
List<List<String>> tokenHierarchies) {
boolean hasVariants = tokenHierarchies.size() > 1;
if (!hasVariants) {
String token = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList()).get(0);
String javaTokenVarName = JavaStyle.toLowerCamelCase(token);
Preconditions.checkNotNull(
patternTokenVarExprs.get(token),
String.format(
"No expression found for token %s amongst values %s",
javaTokenVarName, patternTokenVarExprs.toString()));

List<Expr> instantiateArgExprs = new ArrayList<>();
List<String> tokens = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList());
for (int i = 0; i < tokens.size(); i++) {
String token = tokens.get(i);
Preconditions.checkNotNull(
patternTokenVarExprs.get(token),
String.format(
"No expression found for token %s amongst values %s",
token, patternTokenVarExprs.toString()));
instantiateArgExprs.add(ValueExpr.withValue(StringObjectValue.withValue(token)));
instantiateArgExprs.add(patternTokenVarExprs.get(token));
}

MethodInvocationExpr returnInstantiateExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(templateFinalVarExprs.get(0))
.setMethodName("instantiate")
.setArguments(
ValueExpr.withValue(StringObjectValue.withValue(token)),
patternTokenVarExprs.get(token))
.setArguments(instantiateArgExprs)
.setReturnType(TypeNode.STRING)
.build();
return MethodDefinition.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@
import com.google.api.generator.gapic.protoparser.Parser;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.framework.Utils;
import com.google.logging.v2.LogEntryProto;
import com.google.logging.v2.LoggingConfigProto;
import com.google.logging.v2.LoggingMetricsProto;
import com.google.logging.v2.LoggingProto;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.showcase.v1beta1.EchoOuterClass;
import com.google.showcase.v1beta1.TestingOuterClass;
import google.cloud.CommonResources;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -95,7 +101,6 @@ public void generateResourceNameClass_echoFoobarMultiplePatterns() {
ResourceName foobarResname = resourceNames.get("showcase.googleapis.com/Foobar");
assertThat(outputResourceNames).contains(foobarResname);

Service echoProtoService = services.get(0);
GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(foobarResname);

JavaWriterVisitor visitor = new JavaWriterVisitor();
Expand All @@ -105,6 +110,55 @@ public void generateResourceNameClass_echoFoobarMultiplePatterns() {
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}

@Test
public void generateResourceNameClass_loggingOnePatternMultipleVariables() {
FileDescriptor serviceFileDescriptor = LoggingConfigProto.getDescriptor();
ServiceDescriptor serviceDescriptor = serviceFileDescriptor.getServices().get(0);
assertEquals(serviceDescriptor.getName(), "ConfigServiceV2");

List<FileDescriptor> protoFiles =
Arrays.asList(
serviceFileDescriptor,
LoggingProto.getDescriptor(),
LogEntryProto.getDescriptor(),
LoggingConfigProto.getDescriptor(),
LoggingMetricsProto.getDescriptor());

Map<String, ResourceName> resourceNames = new HashMap<>();
Map<String, Message> messageTypes = new HashMap<>();
for (FileDescriptor fileDescriptor : protoFiles) {
resourceNames.putAll(Parser.parseResourceNames(fileDescriptor));
messageTypes.putAll(Parser.parseMessages(fileDescriptor));
}

// Additional resource names.
FileDescriptor commonResourcesFileDescriptor = CommonResources.getDescriptor();
resourceNames.putAll(Parser.parseResourceNames(commonResourcesFileDescriptor));

Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor,
messageTypes,
resourceNames,
Optional.empty(),
outputResourceNames);

ResourceName billingAccountLocationResname =
resourceNames.get("logging.googleapis.com/BillingAccountLocation");
assertThat(outputResourceNames).contains(billingAccountLocationResname);

GapicClass clazz =
ResourceNameHelperClassComposer.instance().generate(billingAccountLocationResname);

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "BillingAccountLocationName.golden", visitor.write());
Path goldenFilePath =
Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "BillingAccountLocationName.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}

@Test
public void generateResourceNameClass_testingSessionOnePattern() {
FileDescriptor testingFileDescriptor = TestingOuterClass.getDescriptor();
Expand All @@ -125,7 +179,6 @@ public void generateResourceNameClass_testingSessionOnePattern() {
ResourceName sessionResname = resourceNames.get("showcase.googleapis.com/Session");
assertThat(outputResourceNames).contains(sessionResname);

Service testingProtoService = services.get(0);
GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(sessionResname);

JavaWriterVisitor visitor = new JavaWriterVisitor();
Expand All @@ -134,5 +187,4 @@ public void generateResourceNameClass_testingSessionOnePattern() {
Path goldenFilePath = Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "SessionName.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
// TODO(miraleung): Add more tests for a single pattern.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.google.logging.v2;

import com.google.api.pathtemplate.PathTemplate;
import com.google.api.resourcenames.ResourceName;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Generated;

// AUTO-GENERATED DOCUMENTATION AND CLASS.
@Generated("by gapic-generator-java")
public class BillingAccountLocationName implements ResourceName {
private static final PathTemplate BILLING_ACCOUNT_LOCATION =
PathTemplate.createWithoutUrlEncoding(
"billingAccounts/{billing_account}/locations/{location}");
private volatile Map<String, String> fieldValuesMap;
private final String billingAccount;
private final String location;

private BillingAccountLocationName(Builder builder) {
billingAccount = Preconditions.checkNotNull(builder.getBillingAccount());
location = Preconditions.checkNotNull(builder.getLocation());
}

public String getBillingAccount() {
return billingAccount;
}

public String getLocation() {
return location;
}

public static Builder newBuilder() {
return new Builder();
}

public Builder toBuilder() {
return new Builder(this);
}

public static BillingAccountLocationName of(String billingAccount, String location) {
return newBuilder().setBillingAccount(billingAccount).setLocation(location).build();
}

public static String format(String billingAccount, String location) {
return newBuilder().setBillingAccount(billingAccount).setLocation(location).build().toString();
}

public static BillingAccountLocationName parse(String formattedString) {
if (formattedString.isEmpty()) {
return null;
}
Map<String, String> matchMap =
BILLING_ACCOUNT_LOCATION.validatedMatch(
formattedString,
"BillingAccountLocationName.parse: formattedString not in valid format");
return of(matchMap.get("billing_account"), matchMap.get("location"));
}

public static List<BillingAccountLocationName> parseList(List<String> formattedStrings) {
List<BillingAccountLocationName> list = new ArrayList<>(formattedStrings.size());
for (String formattedString : formattedStrings) {
list.add(parse(formattedString));
}
return list;
}

public static List<String> toStringList(List<BillingAccountLocationName> values) {
List<String> list = new ArrayList<>(values.size());
for (BillingAccountLocationName value : values) {
if (Objects.isNull(value)) {
list.add("");
} else {
list.add(value.toString());
}
}
return list;
}

public static boolean isParsableFrom(String formattedString) {
return BILLING_ACCOUNT_LOCATION.matches(formattedString);
}

@Override
public Map<String, String> getFieldValuesMap() {
if (Objects.isNull(fieldValuesMap)) {
synchronized (this) {
if (Objects.isNull(fieldValuesMap)) {
ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
if (!Objects.isNull(billingAccount)) {
fieldMapBuilder.put("billing_account", billingAccount);
}
if (!Objects.isNull(location)) {
fieldMapBuilder.put("location", location);
}
fieldValuesMap = fieldMapBuilder.build();
}
}
}
return fieldValuesMap;
}

public String getFieldValue(String fieldName) {
return getFieldValuesMap().get(fieldName);
}

@Override
public String toString() {
return BILLING_ACCOUNT_LOCATION.instantiate(
"billing_account", billingAccount, "location", location);
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
BillingAccountLocationName that = ((BillingAccountLocationName) o);
return Objects.equals(this.billingAccount, that.billingAccount)
&& Objects.equals(this.location, that.location);
}
return false;
}

@Override
public int hashCode() {
int h = 1;
h *= 1000003;
h ^= Objects.hashCode(billingAccount);
h *= 1000003;
h ^= Objects.hashCode(location);
return h;
}

/** Builder for billingAccounts/{billing_account}/locations/{location}. */
public static class Builder {
private String billingAccount;
private String location;

private Builder() {}

public String getBillingAccount() {
return billingAccount;
}

public String getLocation() {
return location;
}

public Builder setBillingAccount(String billingAccount) {
this.billingAccount = billingAccount;
return this;
}

public Builder setLocation(String location) {
this.location = location;
return this;
}

private Builder(BillingAccountLocationName billingAccountLocationName) {
billingAccount = billingAccountLocationName.billingAccount;
location = billingAccountLocationName.location;
}

public BillingAccountLocationName build() {
return new BillingAccountLocationName(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public String getFieldValue(String fieldName) {

@Override
public String toString() {
return BILLING_ACCOUNT_LOCATION.instantiate("billing_account", billingAccount);
return BILLING_ACCOUNT_LOCATION.instantiate(
"billing_account", billingAccount, "location", location);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion test/integration/goldens/logging/FolderLocationName.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public String getFieldValue(String fieldName) {

@Override
public String toString() {
return FOLDER_LOCATION.instantiate("folder", folder);
return FOLDER_LOCATION.instantiate("folder", folder, "location", location);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion test/integration/goldens/logging/LocationName.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public String getFieldValue(String fieldName) {

@Override
public String toString() {
return PROJECT_LOCATION.instantiate("project", project);
return PROJECT_LOCATION.instantiate("project", project, "location", location);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion test/integration/goldens/logging/LogMetricName.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public String getFieldValue(String fieldName) {

@Override
public String toString() {
return PROJECT_METRIC.instantiate("project", project);
return PROJECT_METRIC.instantiate("project", project, "metric", metric);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public String getFieldValue(String fieldName) {

@Override
public String toString() {
return ORGANIZATION_LOCATION.instantiate("organization", organization);
return ORGANIZATION_LOCATION.instantiate("organization", organization, "location", location);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion test/integration/goldens/redis/InstanceName.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public String getFieldValue(String fieldName) {

@Override
public String toString() {
return PROJECT_LOCATION_INSTANCE.instantiate("project", project);
return PROJECT_LOCATION_INSTANCE.instantiate(
"project", project, "location", location, "instance", instance);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion test/integration/goldens/redis/LocationName.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public String getFieldValue(String fieldName) {

@Override
public String toString() {
return PROJECT_LOCATION.instantiate("project", project);
return PROJECT_LOCATION.instantiate("project", project, "location", location);
}

@Override
Expand Down

0 comments on commit ae7e1b6

Please sign in to comment.