From f075bae757df398108f57f52954f4d091578032f Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Wed, 2 Nov 2022 15:19:13 -0400 Subject: [PATCH 1/6] feat(SpringGen): adding extra annotations at the `SpringComposer` step for all Spring classes generated (#1062) this pr: - adds a @BetaApi annotation for each class composed in SpringComposer - also moves the @Generated("by gapic-generator-java") annotation to SpringComposer for consistency. (this annotation was missing for the properties class composer.) - Updating annotations in SpringComposer level for the ones that applies to all classes makes it easier for future changes when needed. --- .../SpringAutoConfigClassComposer.java | 20 ++-- .../spring/composer/SpringComposer.java | 51 +++++++++- .../SpringPropertiesClassComposer.java | 3 + .../composer/comment/CommentComposer.java | 77 +++++++++++++++ .../SpringAutoconfigCommentComposer.java | 97 +++++++++++++++++++ .../SpringPropertiesCommentComposer.java | 26 +++++ .../EchoSpringAutoConfiguration.golden | 34 ++++++- .../EchoSpringAutoConfigurationFull.golden | 34 +++++++ .../goldens/EchoSpringProperties.golden | 6 ++ .../goldens/EchoSpringPropertiesFull.golden | 10 ++ 10 files changed, 347 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/google/api/generator/spring/composer/comment/CommentComposer.java create mode 100644 src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java create mode 100644 src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java index cec37f1b75..db7cb83fdd 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java @@ -50,6 +50,7 @@ import com.google.api.generator.gapic.model.Service; import com.google.api.generator.spring.utils.LoggerUtils; import com.google.api.generator.spring.utils.Utils; +import com.google.api.generator.spring.composer.comment.SpringAutoconfigCommentComposer; import com.google.common.base.CaseFormat; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; @@ -59,7 +60,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import javax.annotation.Generated; public class SpringAutoConfigClassComposer implements ClassComposer { private static final String CLASS_NAME_PATTERN = "%sSpringAutoConfiguration"; @@ -101,6 +101,7 @@ public GapicClass generate(GapicContext context, Service service) { .setPackageString(packageName) .setName(className) .setScope(ScopeNode.PUBLIC) + .setHeaderCommentStatements(SpringAutoconfigCommentComposer.createClassHeaderComments(className)) .setStatements(createMemberVariables(service, packageName, types, gapicServiceConfig)) .setMethods( Arrays.asList( @@ -224,7 +225,6 @@ private static MethodDefinition createConstructor( private static List createClassAnnotations( Service service, Map types) { - // @Generated("by gapic-generator-java") // @AutoConfiguration // @ConditionalOnClass(LanguageServiceClient.class) // @ConditionalOnProperty(value = @@ -295,10 +295,6 @@ private static List createClassAnnotations( .build(); return Arrays.asList( - AnnotationNode.builder() - .setType(STATIC_TYPES.get("Generated")) - .setDescription("by gapic-generator-java") - .build(), configurationNode, conditionalOnClassNode, conditionalOnPropertyNode, @@ -337,6 +333,7 @@ private static MethodDefinition createCredentialsProviderBeanMethod( return MethodDefinition.builder() .setName(methodName) + .setHeaderCommentStatements(SpringAutoconfigCommentComposer.createCredentialsProviderBeanComment()) .setScope(ScopeNode.PUBLIC) .setReturnType(types.get("CredentialsProvider")) .setAnnotations( @@ -365,6 +362,7 @@ private static MethodDefinition createTransportChannelProviderBeanMethod( .build(); return MethodDefinition.builder() + .setHeaderCommentStatements(SpringAutoconfigCommentComposer.createTransportChannelProviderComment()) .setName(methodName) .setScope(ScopeNode.PUBLIC) .setReturnType(STATIC_TYPES.get("TransportChannelProvider")) @@ -786,8 +784,15 @@ private static MethodDefinition createClientBeanMethod( .setArguments(serviceSettingsBuilt) .build(); + String methodName = + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, service.name()) + "Client"; + String propertiesClassName = service.name() + "Properties"; + return MethodDefinition.builder() - .setName(clientName) + .setHeaderCommentStatements(SpringAutoconfigCommentComposer.createClientBeanComment( + service.name(), propertiesClassName, transportChannelProviderName + )) + .setName(methodName) .setScope(ScopeNode.PUBLIC) .setReturnType(types.get("ServiceClient")) .setArguments( @@ -825,7 +830,6 @@ private static MethodDefinition createClientBeanMethod( private static Map createStaticTypes() { List concreteClazzes = Arrays.asList( - Generated.class, RetrySettings.class, RetrySettings.Builder .class, // name will be just Builder. consider change of more than one builder here. diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringComposer.java index ad8c581841..b9f92c1f52 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringComposer.java @@ -14,21 +14,31 @@ package com.google.api.generator.spring.composer; +import com.google.api.core.BetaApi; +import com.google.api.generator.engine.ast.AnnotationNode; import com.google.api.generator.engine.ast.ClassDefinition; import com.google.api.generator.gapic.composer.comment.CommentComposer; +import com.google.api.generator.gapic.composer.store.TypeStore; import com.google.api.generator.gapic.model.GapicClass; import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.GapicPackageInfo; import com.google.api.generator.gapic.model.Transport; +import com.google.common.collect.ImmutableList; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import javax.annotation.Generated; public class SpringComposer { + + private static final TypeStore FIXED_TYPESTORE = createStaticTypes(); + public static List composeServiceAutoConfigClasses(GapicContext context) { List clazzes = new ArrayList<>(); clazzes.addAll(generatePerServiceClasses(context)); - return addApacheLicense(clazzes); + List clazzesWithHeader = addApacheLicense(clazzes); + return addExtraClassAnnotations(clazzesWithHeader); } public static GapicPackageInfo composePackageInfo(GapicContext context) { @@ -65,6 +75,40 @@ protected static List addApacheLicense(List gapicClassLi .collect(Collectors.toList()); } + protected static List addExtraClassAnnotations(List gapicClassList) { + AnnotationNode generatedAnnotation = + AnnotationNode.builder() + .setType(FIXED_TYPESTORE.get("Generated")) + .setDescription("by gapic-generator-java") + .build(); + AnnotationNode betaAnnotation = + AnnotationNode.builder() + .setType(FIXED_TYPESTORE.get("BetaApi")) + .setDescription("Autogenerated Spring autoconfiguration is not yet stable") + .build(); + return gapicClassList.stream() + .map( + gapicClass -> { + ImmutableList classAnnotations = + gapicClass.classDefinition().annotations(); + ImmutableList updatedAnnotations = + ImmutableList.builder() + .add(generatedAnnotation) + .add(betaAnnotation) + .addAll(classAnnotations) + .build(); + ClassDefinition classWithUpdatedAnnotations = + gapicClass + .classDefinition() + .toBuilder() + .setAnnotations(updatedAnnotations) + .build(); + + return GapicClass.create(gapicClass.kind(), classWithUpdatedAnnotations); + }) + .collect(Collectors.toList()); + } + private static GapicPackageInfo addApacheLicense(GapicPackageInfo gapicPackageInfo) { return GapicPackageInfo.with( gapicPackageInfo @@ -73,4 +117,9 @@ private static GapicPackageInfo addApacheLicense(GapicPackageInfo gapicPackageIn .setFileHeader(CommentComposer.APACHE_LICENSE_COMMENT) .build()); } + + private static TypeStore createStaticTypes() { + List> concreteClazzes = Arrays.asList(BetaApi.class, Generated.class); + return new TypeStore(concreteClazzes); + } } diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java index 00c1c3557f..620042b6bc 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java @@ -42,6 +42,7 @@ import com.google.api.generator.gapic.model.GapicServiceConfig; import com.google.api.generator.gapic.model.Service; import com.google.api.generator.spring.utils.Utils; +import com.google.api.generator.spring.composer.comment.SpringPropertiesCommentComposer; import com.google.common.base.CaseFormat; import com.google.common.base.Joiner; import java.util.ArrayList; @@ -80,6 +81,8 @@ public GapicClass generate(GapicContext context, Service service) { ClassDefinition classDef = ClassDefinition.builder() + .setHeaderCommentStatements(SpringPropertiesCommentComposer.createClassHeaderComments( + className, service.name())) .setPackageString(packageName) .setName(className) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/CommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/CommentComposer.java new file mode 100644 index 0000000000..439333a01f --- /dev/null +++ b/src/main/java/com/google/api/generator/spring/composer/comment/CommentComposer.java @@ -0,0 +1,77 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.api.generator.spring.composer.comment; + +import com.google.api.generator.engine.ast.BlockComment; +import com.google.api.generator.engine.ast.CommentStatement; +import com.google.api.generator.engine.ast.LineComment; +import com.google.api.generator.engine.ast.Statement; +import java.util.Arrays; +import java.util.List; + +public class CommentComposer { + protected static final String CLASS_HEADER_SUMMARY_PATTERN = + "Settings class to configure an instance of {@link %s}."; + private static final String APACHE_LICENSE_STRING = + "Copyright 2022 Google LLC\n\n" + + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "you may not use this file except in compliance with the License.\n" + + "You may obtain a copy of the License at\n\n" + + " https://www.apache.org/licenses/LICENSE-2.0\n\n" + + "Unless required by applicable law or agreed to in writing, software\n" + + "distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "See the License for the specific language governing permissions and\n" + + "limitations under the License."; + + private static final String AUTO_GENERATED_CLASS_DISCLAIMER_STRING = + "AUTO-GENERATED DOCUMENTATION AND CLASS."; + + private static final String AUTO_GENERATED_METHOD_DISCLAIMER_STRING = + "AUTO-GENERATED DOCUMENTATION AND METHOD."; + + static final String DEPRECATED_CLASS_STRING = + "This class is deprecated and will be removed in the next major version update."; + + static final String DEPRECATED_METHOD_STRING = + "This method is deprecated and will be removed in the next major version update."; + + public static final CommentStatement APACHE_LICENSE_COMMENT = + CommentStatement.withComment(BlockComment.withComment(APACHE_LICENSE_STRING)); + + public static final CommentStatement AUTO_GENERATED_CLASS_COMMENT = + CommentStatement.withComment(LineComment.withComment(AUTO_GENERATED_CLASS_DISCLAIMER_STRING)); + + public static final CommentStatement AUTO_GENERATED_METHOD_COMMENT = + CommentStatement.withComment( + LineComment.withComment(AUTO_GENERATED_METHOD_DISCLAIMER_STRING)); + + public static final List AUTO_GENERATED_SAMPLE_COMMENT = + Arrays.asList( + CommentStatement.withComment( + LineComment.withComment( + "This snippet has been automatically generated and should be regarded as a code template only.")), + CommentStatement.withComment( + LineComment.withComment("It will require modifications to work:")), + CommentStatement.withComment( + LineComment.withComment( + "- It may require correct/in-range values for request initialization.")), + CommentStatement.withComment( + LineComment.withComment( + "- It may require specifying regional endpoints when creating the service client as shown in")), + CommentStatement.withComment( + LineComment.withComment( + "https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library"))); +} diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java new file mode 100644 index 0000000000..a92c2eda5b --- /dev/null +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java @@ -0,0 +1,97 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.api.generator.spring.composer.comment; + +import static com.google.api.generator.spring.composer.comment.CommentComposer.CLASS_HEADER_SUMMARY_PATTERN; + +import com.google.api.generator.engine.ast.CommentStatement; +import com.google.api.generator.engine.ast.JavaDocComment; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class SpringAutoconfigCommentComposer { + + private static final String CLASS_HEADER_GENERAL_DESCRIPTION = + "Provides auto-configuration for Spring Boot"; + private static final String CLASS_HEADER_DEFAULTS_DESCRIPTION = + "The default instance has everything set to sensible defaults:"; + private static final String CLASS_HEADER_DEFAULTS_CREDENTIALS_DESCRIPTION = + "Credentials are acquired automatically through Application Default Credentials."; + private static final String CLASS_HEADER_DEFAULTS_TRANSPORT_DESCRIPTION = + "The default transport provider is used."; + private static final String CLASS_HEADER_DEFAULTS_RETRIES_DESCRIPTION = + "Retries are configured for idempotent methods but not for non-idempotent methods."; + + public static final String CREDENTIALS_PROVIDER_GENERAL_DESCRIPTION = + "Obtains the default credentials provider. The used key will be obtained from application.properties"; + + public static final String TRANSPORT_CHANNEL_PROVIDER_GENERAL_DESCRIPTION = + "Returns the default channel provider. The default is gRPC and will default to it unless the " + + "useRest option is provided to use HTTP transport instead"; + public static final String CLIENT_BEAN_GENERAL_DESCRIPTION = "Provides a %s client configured to " + + "use the default credentials provider (obtained with googleCredentials()) and its default" + + "transport channel provider (%s()). It also configures the quota project ID if provided. It " + + "will configure an executor provider in case there is more than one thread configured " + + "in the client "; + + public static final String CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION = "Individual retry settings " + + "are configured as well. It will use the default retry settings obtained from %s when they " + + "are not specified"; + + public SpringAutoconfigCommentComposer() {} + + public static List createClassHeaderComments( + String configuredClassName) { + + JavaDocComment.Builder javaDocCommentBuilder = + JavaDocComment.builder() + .addUnescapedComment(String.format(CLASS_HEADER_SUMMARY_PATTERN, configuredClassName)) + .addParagraph(CLASS_HEADER_GENERAL_DESCRIPTION) + .addParagraph(CLASS_HEADER_DEFAULTS_DESCRIPTION) + .addUnorderedList( + Arrays.asList( + CLASS_HEADER_DEFAULTS_TRANSPORT_DESCRIPTION, + CLASS_HEADER_DEFAULTS_CREDENTIALS_DESCRIPTION, + CLASS_HEADER_DEFAULTS_RETRIES_DESCRIPTION)); + + return Arrays.asList( + CommentComposer.AUTO_GENERATED_CLASS_COMMENT, + CommentStatement.withComment(javaDocCommentBuilder.build())); + } + public static CommentStatement createCredentialsProviderBeanComment() { + return CommentStatement.withComment(JavaDocComment.builder() + .addParagraph(CREDENTIALS_PROVIDER_GENERAL_DESCRIPTION) + .build() + ); + } + + public static CommentStatement createTransportChannelProviderComment() { + return CommentStatement.withComment(JavaDocComment.builder() + .addParagraph(TRANSPORT_CHANNEL_PROVIDER_GENERAL_DESCRIPTION) + .build() + ); + } + public static CommentStatement createClientBeanComment( + String serviceName, + String propertiesClazzName, + String channelProviderName) { + return CommentStatement.withComment(JavaDocComment.builder() + .addParagraph(String.format(CLIENT_BEAN_GENERAL_DESCRIPTION, serviceName, channelProviderName)) + .addParagraph(String.format(CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION, propertiesClazzName)) + .build() + ); + } +} diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java new file mode 100644 index 0000000000..71e6e1ab8a --- /dev/null +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java @@ -0,0 +1,26 @@ +package com.google.api.generator.spring.composer.comment; + +import static com.google.api.generator.spring.composer.comment.CommentComposer.CLASS_HEADER_SUMMARY_PATTERN; + +import com.google.api.generator.engine.ast.CommentStatement; +import com.google.api.generator.engine.ast.JavaDocComment; +import java.util.Arrays; +import java.util.List; + +public class SpringPropertiesCommentComposer { + private static final String CLASS_HEADER_GENERAL_DESCRIPTION = + "Provides default configuration values for %s client"; + + public static List createClassHeaderComments( + String configuredClassName, + String serviceName) { + + JavaDocComment.Builder javaDocCommentBuilder = + JavaDocComment.builder() + .addUnescapedComment(String.format(CLASS_HEADER_SUMMARY_PATTERN, configuredClassName)) + .addParagraph(String.format(CLASS_HEADER_GENERAL_DESCRIPTION, serviceName)); + return Arrays.asList( + CommentComposer.AUTO_GENERATED_CLASS_COMMENT, + CommentStatement.withComment(javaDocCommentBuilder.build())); + } +} diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden index be91d0f036..f8e145f976 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden @@ -9,7 +9,6 @@ import com.google.cloud.spring.core.DefaultCredentialsProvider; import com.google.showcase.v1beta1.EchoClient; import com.google.showcase.v1beta1.EchoSettings; import java.io.IOException; -import javax.annotation.Generated; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; @@ -20,7 +19,20 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.threeten.bp.Duration; -@Generated("by gapic-generator-java") +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * Settings class to configure an instance of {@link EchoSpringAutoConfiguration}. + * + *

Provides auto-configuration for Spring Boot + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default transport provider is used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ */ @AutoConfiguration @ConditionalOnClass(EchoClient.class) @ConditionalOnProperty( @@ -35,18 +47,36 @@ public class EchoSpringAutoConfiguration { this.clientProperties = clientProperties; } + /** + * Obtains the default credentials provider. The used key will be obtained from + * application.properties + */ @Bean @ConditionalOnMissingBean public CredentialsProvider echoCredentials() throws IOException { return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); } + /** + * Returns the default channel provider. The default is gRPC and will default to it unless the + * useRest option is provided to use HTTP transport instead + */ @Bean @ConditionalOnMissingBean public TransportChannelProvider defaultEchoTransportChannelProvider() { return EchoSettings.defaultTransportChannelProvider(); } + /** + * Provides a Echo client configured to use the default credentials provider (obtained with + * googleCredentials()) and its defaulttransport channel provider + * (defaultEchoTransportChannelProvider()). It also configures the quota project ID if provided. + * It will configure an executor provider in case there is more than one thread configured in the + * client + * + *

Individual retry settings are configured as well. It will use the default retry settings + * obtained from EchoProperties when they are not specified + */ @Bean @ConditionalOnMissingBean public EchoClient echoClient( diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden index 7018ac2e55..0a9b17e3da 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden @@ -16,6 +16,7 @@ package com.google.showcase.v1beta1.spring; +import com.google.api.core.BetaApi; import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.core.ExecutorProvider; import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider; @@ -36,7 +37,22 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.threeten.bp.Duration; +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * Settings class to configure an instance of {@link EchoSpringAutoConfiguration}. + * + *

Provides auto-configuration for Spring Boot + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default transport provider is used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ */ @Generated("by gapic-generator-java") +@BetaApi("Autogenerated Spring autoconfiguration is not yet stable") @AutoConfiguration @ConditionalOnClass(EchoClient.class) @ConditionalOnProperty( @@ -51,18 +67,36 @@ public class EchoSpringAutoConfiguration { this.clientProperties = clientProperties; } + /** + * Obtains the default credentials provider. The used key will be obtained from + * application.properties + */ @Bean @ConditionalOnMissingBean public CredentialsProvider echoCredentials() throws IOException { return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); } + /** + * Returns the default channel provider. The default is gRPC and will default to it unless the + * useRest option is provided to use HTTP transport instead + */ @Bean @ConditionalOnMissingBean public TransportChannelProvider defaultEchoTransportChannelProvider() { return EchoSettings.defaultTransportChannelProvider(); } + /** + * Provides a Echo client configured to use the default credentials provider (obtained with + * googleCredentials()) and its defaulttransport channel provider + * (defaultEchoTransportChannelProvider()). It also configures the quota project ID if provided. + * It will configure an executor provider in case there is more than one thread configured in the + * client + * + *

Individual retry settings are configured as well. It will use the default retry settings + * obtained from EchoProperties when they are not specified + */ @Bean @ConditionalOnMissingBean public EchoClient echoClient( diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringProperties.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringProperties.golden index 5fe2406751..63c53fd8d2 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringProperties.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringProperties.golden @@ -5,6 +5,12 @@ import com.google.cloud.spring.core.CredentialsSupplier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.threeten.bp.Duration; +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * Settings class to configure an instance of {@link EchoSpringProperties}. + * + *

Provides default configuration values for Echo client + */ @ConfigurationProperties("com.google.showcase.v1beta1.spring.auto.echo") public class EchoSpringProperties implements CredentialsSupplier { @NestedConfigurationProperty diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringPropertiesFull.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringPropertiesFull.golden index a20c370430..0ef92e78b8 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringPropertiesFull.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringPropertiesFull.golden @@ -16,11 +16,21 @@ package com.google.showcase.v1beta1.spring; +import com.google.api.core.BetaApi; import com.google.cloud.spring.core.Credentials; import com.google.cloud.spring.core.CredentialsSupplier; +import javax.annotation.Generated; import org.springframework.boot.context.properties.ConfigurationProperties; import org.threeten.bp.Duration; +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * Settings class to configure an instance of {@link EchoSpringProperties}. + * + *

Provides default configuration values for Echo client + */ +@Generated("by gapic-generator-java") +@BetaApi("Autogenerated Spring autoconfiguration is not yet stable") @ConfigurationProperties("com.google.showcase.v1beta1.spring.auto.echo") public class EchoSpringProperties implements CredentialsSupplier { @NestedConfigurationProperty From 32bb3270081fb5af3c553133d25f6059b4fdeb20 Mon Sep 17 00:00:00 2001 From: Diego Marquez Date: Wed, 12 Oct 2022 19:06:38 +0000 Subject: [PATCH 2/6] fix(test): remove wrong files from rebase action --- .../SpringAutoconfigCommentComposer.java | 2 +- .../gapic/composer/ComposerTest.java | 123 ------------------ 2 files changed, 1 insertion(+), 124 deletions(-) delete mode 100644 src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java index a92c2eda5b..1c1cfb8f22 100644 --- a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java @@ -42,7 +42,7 @@ public class SpringAutoconfigCommentComposer { "Returns the default channel provider. The default is gRPC and will default to it unless the " + "useRest option is provided to use HTTP transport instead"; public static final String CLIENT_BEAN_GENERAL_DESCRIPTION = "Provides a %s client configured to " - + "use the default credentials provider (obtained with googleCredentials()) and its default" + + "use the default credentials provider (obtained with googleCredentials()) and its default " + "transport channel provider (%s()). It also configures the quota project ID if provided. It " + "will configure an executor provider in case there is more than one thread configured " + "in the client "; diff --git a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java deleted file mode 100644 index 4bbc779dcf..0000000000 --- a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.api.generator.gapic.composer; - -import static org.junit.Assert.assertEquals; - -import com.google.api.generator.engine.ast.ClassDefinition; -import com.google.api.generator.engine.ast.ScopeNode; -import com.google.api.generator.engine.writer.JavaWriterVisitor; -import com.google.api.generator.gapic.composer.comment.CommentComposer; -import com.google.api.generator.gapic.composer.grpc.GrpcServiceCallableFactoryClassComposer; -import com.google.api.generator.gapic.composer.grpc.GrpcTestProtoLoader; -import com.google.api.generator.gapic.model.GapicClass; -import com.google.api.generator.gapic.model.GapicClass.Kind; -import com.google.api.generator.gapic.model.GapicContext; -import com.google.api.generator.gapic.model.Sample; -import com.google.api.generator.gapic.model.Service; -import com.google.api.generator.test.framework.Assert; -import com.google.api.generator.test.framework.Utils; -import com.google.common.collect.ImmutableList; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; -import org.junit.Test; - -public class ComposerTest { - private final GapicContext context = GrpcTestProtoLoader.instance().parseShowcaseEcho(); - private final Service echoProtoService = context.services().get(0); - private final List clazzes = - Arrays.asList( - GrpcServiceCallableFactoryClassComposer.instance().generate(context, echoProtoService)); - private final String protoPackage = context.gapicMetadata().getProtoPackage(); - private final List samples = clazzes.get(0).samples(); - - @Test - public void gapicClass_addApacheLicense() { - ClassDefinition classDef = - ClassDefinition.builder() - .setPackageString("com.google.showcase.v1beta1.stub") - .setName("ComposerPostProcOnFooBar") - .setScope(ScopeNode.PUBLIC) - .build(); - List gapicClassWithHeaderList = - Composer.addApacheLicense(Arrays.asList(GapicClass.create(Kind.TEST, classDef))); - JavaWriterVisitor visitor = new JavaWriterVisitor(); - gapicClassWithHeaderList.get(0).classDefinition().accept(visitor); - Utils.saveCodegenToFile(this.getClass(), "ComposerPostProcOnFooBar.golden", visitor.write()); - Path goldenFilePath = - Paths.get(Utils.getGoldenDir(this.getClass()), "ComposerPostProcOnFooBar.golden"); - Assert.assertCodeEquals(goldenFilePath, visitor.write()); - } - - @Test - public void composeSamples_showcase() { - for (Sample sample : samples) { - assertEquals( - "File header will be empty before composing samples", - sample.fileHeader(), - ImmutableList.of()); - assertEquals( - "ApiShortName will be empty before composing samples", - sample.regionTag().apiShortName(), - ""); - assertEquals( - "ApiVersion will be empty before composing samples", sample.regionTag().apiVersion(), ""); - } - - List composedSamples = - Composer.prepareExecutableSamples(clazzes, protoPackage).get(0).samples(); - - for (Sample sample : composedSamples) { - assertEquals( - "File header should be apache", - sample.fileHeader(), - Arrays.asList(CommentComposer.APACHE_LICENSE_COMMENT)); - assertEquals( - "ApiShortName should be showcase", sample.regionTag().apiShortName(), "showcase"); - assertEquals("ApiVersion should be v1beta1", sample.regionTag().apiVersion(), "v1beta1"); - } - } - - @Test - public void composeSamples_parseProtoPackage() { - String protoPack = "google.cloud.accessapproval.v1"; - List composedSamples = - Composer.prepareExecutableSamples(clazzes, protoPack).get(0).samples(); - - for (Sample sample : composedSamples) { - assertEquals( - "ApiShortName should be accessapproval", - sample.regionTag().apiShortName(), - "accessapproval"); - assertEquals("ApiVersion should be v1", sample.regionTag().apiVersion(), "v1"); - } - - protoPack = "google.cloud.vision.v1p1beta1"; - composedSamples = Composer.prepareExecutableSamples(clazzes, protoPack).get(0).samples(); - for (Sample sample : composedSamples) { - assertEquals("ApiShortName should be vision", sample.regionTag().apiShortName(), "vision"); - assertEquals("ApiVersion should be v1p1beta1", sample.regionTag().apiVersion(), "v1p1beta1"); - } - - protoPack = "google.cloud.vision"; - composedSamples = Composer.prepareExecutableSamples(clazzes, protoPack).get(0).samples(); - for (Sample sample : composedSamples) { - assertEquals("ApiShortName should be vision", sample.regionTag().apiShortName(), "vision"); - assertEquals("ApiVersion should be empty", sample.regionTag().apiVersion(), ""); - } - } -} From a6b0fd3700f703f870399fda4a38f56a286ae1d8 Mon Sep 17 00:00:00 2001 From: Diego Marquez Date: Wed, 12 Oct 2022 19:12:19 +0000 Subject: [PATCH 3/6] fix(format): linting --- .../SpringAutoConfigClassComposer.java | 15 +++-- .../SpringPropertiesClassComposer.java | 5 +- .../SpringAutoconfigCommentComposer.java | 56 +++++++++---------- .../SpringPropertiesCommentComposer.java | 3 +- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java index db7cb83fdd..2855b20c79 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java @@ -101,7 +101,8 @@ public GapicClass generate(GapicContext context, Service service) { .setPackageString(packageName) .setName(className) .setScope(ScopeNode.PUBLIC) - .setHeaderCommentStatements(SpringAutoconfigCommentComposer.createClassHeaderComments(className)) + .setHeaderCommentStatements( + SpringAutoconfigCommentComposer.createClassHeaderComments(className)) .setStatements(createMemberVariables(service, packageName, types, gapicServiceConfig)) .setMethods( Arrays.asList( @@ -333,7 +334,8 @@ private static MethodDefinition createCredentialsProviderBeanMethod( return MethodDefinition.builder() .setName(methodName) - .setHeaderCommentStatements(SpringAutoconfigCommentComposer.createCredentialsProviderBeanComment()) + .setHeaderCommentStatements( + SpringAutoconfigCommentComposer.createCredentialsProviderBeanComment()) .setScope(ScopeNode.PUBLIC) .setReturnType(types.get("CredentialsProvider")) .setAnnotations( @@ -362,7 +364,8 @@ private static MethodDefinition createTransportChannelProviderBeanMethod( .build(); return MethodDefinition.builder() - .setHeaderCommentStatements(SpringAutoconfigCommentComposer.createTransportChannelProviderComment()) + .setHeaderCommentStatements( + SpringAutoconfigCommentComposer.createTransportChannelProviderComment()) .setName(methodName) .setScope(ScopeNode.PUBLIC) .setReturnType(STATIC_TYPES.get("TransportChannelProvider")) @@ -789,9 +792,9 @@ private static MethodDefinition createClientBeanMethod( String propertiesClassName = service.name() + "Properties"; return MethodDefinition.builder() - .setHeaderCommentStatements(SpringAutoconfigCommentComposer.createClientBeanComment( - service.name(), propertiesClassName, transportChannelProviderName - )) + .setHeaderCommentStatements( + SpringAutoconfigCommentComposer.createClientBeanComment( + service.name(), propertiesClassName, transportChannelProviderName)) .setName(methodName) .setScope(ScopeNode.PUBLIC) .setReturnType(types.get("ServiceClient")) diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java index 620042b6bc..90d09448e6 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java @@ -81,8 +81,9 @@ public GapicClass generate(GapicContext context, Service service) { ClassDefinition classDef = ClassDefinition.builder() - .setHeaderCommentStatements(SpringPropertiesCommentComposer.createClassHeaderComments( - className, service.name())) + .setHeaderCommentStatements( + SpringPropertiesCommentComposer.createClassHeaderComments( + className, service.name())) .setPackageString(packageName) .setName(className) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java index 1c1cfb8f22..b781176367 100644 --- a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java @@ -20,12 +20,11 @@ import com.google.api.generator.engine.ast.JavaDocComment; import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; public class SpringAutoconfigCommentComposer { private static final String CLASS_HEADER_GENERAL_DESCRIPTION = - "Provides auto-configuration for Spring Boot"; + "Provides auto-configuration for Spring Boot"; private static final String CLASS_HEADER_DEFAULTS_DESCRIPTION = "The default instance has everything set to sensible defaults:"; private static final String CLASS_HEADER_DEFAULTS_CREDENTIALS_DESCRIPTION = @@ -41,20 +40,21 @@ public class SpringAutoconfigCommentComposer { public static final String TRANSPORT_CHANNEL_PROVIDER_GENERAL_DESCRIPTION = "Returns the default channel provider. The default is gRPC and will default to it unless the " + "useRest option is provided to use HTTP transport instead"; - public static final String CLIENT_BEAN_GENERAL_DESCRIPTION = "Provides a %s client configured to " - + "use the default credentials provider (obtained with googleCredentials()) and its default " - + "transport channel provider (%s()). It also configures the quota project ID if provided. It " - + "will configure an executor provider in case there is more than one thread configured " - + "in the client "; + public static final String CLIENT_BEAN_GENERAL_DESCRIPTION = + "Provides a %s client configured to " + + "use the default credentials provider (obtained with googleCredentials()) and its default " + + "transport channel provider (%s()). It also configures the quota project ID if provided. It " + + "will configure an executor provider in case there is more than one thread configured " + + "in the client "; - public static final String CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION = "Individual retry settings " - + "are configured as well. It will use the default retry settings obtained from %s when they " - + "are not specified"; + public static final String CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION = + "Individual retry settings " + + "are configured as well. It will use the default retry settings obtained from %s when they " + + "are not specified"; public SpringAutoconfigCommentComposer() {} - public static List createClassHeaderComments( - String configuredClassName) { + public static List createClassHeaderComments(String configuredClassName) { JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder() @@ -71,27 +71,27 @@ public static List createClassHeaderComments( CommentComposer.AUTO_GENERATED_CLASS_COMMENT, CommentStatement.withComment(javaDocCommentBuilder.build())); } + public static CommentStatement createCredentialsProviderBeanComment() { - return CommentStatement.withComment(JavaDocComment.builder() - .addParagraph(CREDENTIALS_PROVIDER_GENERAL_DESCRIPTION) - .build() - ); + return CommentStatement.withComment( + JavaDocComment.builder().addParagraph(CREDENTIALS_PROVIDER_GENERAL_DESCRIPTION).build()); } public static CommentStatement createTransportChannelProviderComment() { - return CommentStatement.withComment(JavaDocComment.builder() - .addParagraph(TRANSPORT_CHANNEL_PROVIDER_GENERAL_DESCRIPTION) - .build() - ); + return CommentStatement.withComment( + JavaDocComment.builder() + .addParagraph(TRANSPORT_CHANNEL_PROVIDER_GENERAL_DESCRIPTION) + .build()); } + public static CommentStatement createClientBeanComment( - String serviceName, - String propertiesClazzName, - String channelProviderName) { - return CommentStatement.withComment(JavaDocComment.builder() - .addParagraph(String.format(CLIENT_BEAN_GENERAL_DESCRIPTION, serviceName, channelProviderName)) - .addParagraph(String.format(CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION, propertiesClazzName)) - .build() - ); + String serviceName, String propertiesClazzName, String channelProviderName) { + return CommentStatement.withComment( + JavaDocComment.builder() + .addParagraph( + String.format(CLIENT_BEAN_GENERAL_DESCRIPTION, serviceName, channelProviderName)) + .addParagraph( + String.format(CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION, propertiesClazzName)) + .build()); } } diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java index 71e6e1ab8a..499b28e90e 100644 --- a/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java @@ -12,8 +12,7 @@ public class SpringPropertiesCommentComposer { "Provides default configuration values for %s client"; public static List createClassHeaderComments( - String configuredClassName, - String serviceName) { + String configuredClassName, String serviceName) { JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder() From 0e43c976af6f2de1af66b63e02af416e9b09d18b Mon Sep 17 00:00:00 2001 From: Diego Marquez Date: Wed, 12 Oct 2022 19:13:33 +0000 Subject: [PATCH 4/6] fix(license): add license header to prop comm comp --- .../comment/SpringAutoconfigCommentComposer.java | 2 +- .../comment/SpringPropertiesCommentComposer.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java index b781176367..d1d871a714 100644 --- a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java index 499b28e90e..4e897c2d7e 100644 --- a/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java @@ -1,3 +1,17 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.api.generator.spring.composer.comment; import static com.google.api.generator.spring.composer.comment.CommentComposer.CLASS_HEADER_SUMMARY_PATTERN; From 03a5fe35f0bd26f8e15413f11a2740a173f851e0 Mon Sep 17 00:00:00 2001 From: Diego Marquez Date: Wed, 12 Oct 2022 19:27:51 +0000 Subject: [PATCH 5/6] fix(test): restore ComposerTest --- .../SpringAutoConfigClassComposer.java | 2 +- .../composer/comment/CommentComposer.java | 77 ----------- .../SpringAutoconfigCommentComposer.java | 26 ++-- .../SpringPropertiesCommentComposer.java | 6 +- .../gapic/composer/ComposerTest.java | 123 ++++++++++++++++++ .../EchoSpringAutoConfiguration.golden | 14 +- .../EchoSpringAutoConfigurationFull.golden | 14 +- .../goldens/EchoSpringProperties.golden | 6 +- .../goldens/EchoSpringPropertiesFull.golden | 6 +- 9 files changed, 159 insertions(+), 115 deletions(-) delete mode 100644 src/main/java/com/google/api/generator/spring/composer/comment/CommentComposer.java create mode 100644 src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java index 2855b20c79..05041e958a 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java @@ -102,7 +102,7 @@ public GapicClass generate(GapicContext context, Service service) { .setName(className) .setScope(ScopeNode.PUBLIC) .setHeaderCommentStatements( - SpringAutoconfigCommentComposer.createClassHeaderComments(className)) + SpringAutoconfigCommentComposer.createClassHeaderComments(className, serviceName)) .setStatements(createMemberVariables(service, packageName, types, gapicServiceConfig)) .setMethods( Arrays.asList( diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/CommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/CommentComposer.java deleted file mode 100644 index 439333a01f..0000000000 --- a/src/main/java/com/google/api/generator/spring/composer/comment/CommentComposer.java +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.api.generator.spring.composer.comment; - -import com.google.api.generator.engine.ast.BlockComment; -import com.google.api.generator.engine.ast.CommentStatement; -import com.google.api.generator.engine.ast.LineComment; -import com.google.api.generator.engine.ast.Statement; -import java.util.Arrays; -import java.util.List; - -public class CommentComposer { - protected static final String CLASS_HEADER_SUMMARY_PATTERN = - "Settings class to configure an instance of {@link %s}."; - private static final String APACHE_LICENSE_STRING = - "Copyright 2022 Google LLC\n\n" - + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "you may not use this file except in compliance with the License.\n" - + "You may obtain a copy of the License at\n\n" - + " https://www.apache.org/licenses/LICENSE-2.0\n\n" - + "Unless required by applicable law or agreed to in writing, software\n" - + "distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "See the License for the specific language governing permissions and\n" - + "limitations under the License."; - - private static final String AUTO_GENERATED_CLASS_DISCLAIMER_STRING = - "AUTO-GENERATED DOCUMENTATION AND CLASS."; - - private static final String AUTO_GENERATED_METHOD_DISCLAIMER_STRING = - "AUTO-GENERATED DOCUMENTATION AND METHOD."; - - static final String DEPRECATED_CLASS_STRING = - "This class is deprecated and will be removed in the next major version update."; - - static final String DEPRECATED_METHOD_STRING = - "This method is deprecated and will be removed in the next major version update."; - - public static final CommentStatement APACHE_LICENSE_COMMENT = - CommentStatement.withComment(BlockComment.withComment(APACHE_LICENSE_STRING)); - - public static final CommentStatement AUTO_GENERATED_CLASS_COMMENT = - CommentStatement.withComment(LineComment.withComment(AUTO_GENERATED_CLASS_DISCLAIMER_STRING)); - - public static final CommentStatement AUTO_GENERATED_METHOD_COMMENT = - CommentStatement.withComment( - LineComment.withComment(AUTO_GENERATED_METHOD_DISCLAIMER_STRING)); - - public static final List AUTO_GENERATED_SAMPLE_COMMENT = - Arrays.asList( - CommentStatement.withComment( - LineComment.withComment( - "This snippet has been automatically generated and should be regarded as a code template only.")), - CommentStatement.withComment( - LineComment.withComment("It will require modifications to work:")), - CommentStatement.withComment( - LineComment.withComment( - "- It may require correct/in-range values for request initialization.")), - CommentStatement.withComment( - LineComment.withComment( - "- It may require specifying regional endpoints when creating the service client as shown in")), - CommentStatement.withComment( - LineComment.withComment( - "https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library"))); -} diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java index d1d871a714..6d6717182b 100644 --- a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java @@ -14,15 +14,19 @@ package com.google.api.generator.spring.composer.comment; -import static com.google.api.generator.spring.composer.comment.CommentComposer.CLASS_HEADER_SUMMARY_PATTERN; import com.google.api.generator.engine.ast.CommentStatement; import com.google.api.generator.engine.ast.JavaDocComment; +import com.google.api.generator.gapic.composer.comment.CommentComposer; +import com.google.common.base.CaseFormat; +import com.google.common.base.Joiner; import java.util.Arrays; import java.util.List; public class SpringAutoconfigCommentComposer { + private static final String CLASS_HEADER_SUMMARY_PATTERN = + "Auto-configuration for {@link %sClient}."; private static final String CLASS_HEADER_GENERAL_DESCRIPTION = "Provides auto-configuration for Spring Boot"; private static final String CLASS_HEADER_DEFAULTS_DESCRIPTION = @@ -35,30 +39,33 @@ public class SpringAutoconfigCommentComposer { "Retries are configured for idempotent methods but not for non-idempotent methods."; public static final String CREDENTIALS_PROVIDER_GENERAL_DESCRIPTION = - "Obtains the default credentials provider. The used key will be obtained from application.properties"; + "Obtains the default credentials provider. The used key will be obtained from Spring Boot " + + "configuration data files."; public static final String TRANSPORT_CHANNEL_PROVIDER_GENERAL_DESCRIPTION = "Returns the default channel provider. The default is gRPC and will default to it unless the " + "useRest option is provided to use HTTP transport instead"; public static final String CLIENT_BEAN_GENERAL_DESCRIPTION = - "Provides a %s client configured to " - + "use the default credentials provider (obtained with googleCredentials()) and its default " + "Provides a %sClient bean configured to " + + "use the default credentials provider (obtained with %sCredentials()) and its default " + "transport channel provider (%s()). It also configures the quota project ID if provided. It " + "will configure an executor provider in case there is more than one thread configured " + "in the client "; public static final String CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION = "Individual retry settings " - + "are configured as well. It will use the default retry settings obtained from %s when they " - + "are not specified"; + + "are configured as well. It will use the relevant client library's default retry " + + "settings when they are not specified in %s."; public SpringAutoconfigCommentComposer() {} - public static List createClassHeaderComments(String configuredClassName) { + public static List createClassHeaderComments( + String configuredClassName, + String serviceName) { JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder() - .addUnescapedComment(String.format(CLASS_HEADER_SUMMARY_PATTERN, configuredClassName)) + .addUnescapedComment(String.format(CLASS_HEADER_SUMMARY_PATTERN, serviceName)) .addParagraph(CLASS_HEADER_GENERAL_DESCRIPTION) .addParagraph(CLASS_HEADER_DEFAULTS_DESCRIPTION) .addUnorderedList( @@ -86,10 +93,11 @@ public static CommentStatement createTransportChannelProviderComment() { public static CommentStatement createClientBeanComment( String serviceName, String propertiesClazzName, String channelProviderName) { + String credentialsBaseName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, serviceName); return CommentStatement.withComment( JavaDocComment.builder() .addParagraph( - String.format(CLIENT_BEAN_GENERAL_DESCRIPTION, serviceName, channelProviderName)) + String.format(CLIENT_BEAN_GENERAL_DESCRIPTION, serviceName, credentialsBaseName, channelProviderName)) .addParagraph( String.format(CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION, propertiesClazzName)) .build()); diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java index 4e897c2d7e..6d05240eea 100644 --- a/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringPropertiesCommentComposer.java @@ -14,23 +14,21 @@ package com.google.api.generator.spring.composer.comment; -import static com.google.api.generator.spring.composer.comment.CommentComposer.CLASS_HEADER_SUMMARY_PATTERN; - import com.google.api.generator.engine.ast.CommentStatement; import com.google.api.generator.engine.ast.JavaDocComment; +import com.google.api.generator.gapic.composer.comment.CommentComposer; import java.util.Arrays; import java.util.List; public class SpringPropertiesCommentComposer { private static final String CLASS_HEADER_GENERAL_DESCRIPTION = - "Provides default configuration values for %s client"; + "Provides default property values for %s client bean"; public static List createClassHeaderComments( String configuredClassName, String serviceName) { JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder() - .addUnescapedComment(String.format(CLASS_HEADER_SUMMARY_PATTERN, configuredClassName)) .addParagraph(String.format(CLASS_HEADER_GENERAL_DESCRIPTION, serviceName)); return Arrays.asList( CommentComposer.AUTO_GENERATED_CLASS_COMMENT, diff --git a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java new file mode 100644 index 0000000000..4bbc779dcf --- /dev/null +++ b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -0,0 +1,123 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.api.generator.gapic.composer; + +import static org.junit.Assert.assertEquals; + +import com.google.api.generator.engine.ast.ClassDefinition; +import com.google.api.generator.engine.ast.ScopeNode; +import com.google.api.generator.engine.writer.JavaWriterVisitor; +import com.google.api.generator.gapic.composer.comment.CommentComposer; +import com.google.api.generator.gapic.composer.grpc.GrpcServiceCallableFactoryClassComposer; +import com.google.api.generator.gapic.composer.grpc.GrpcTestProtoLoader; +import com.google.api.generator.gapic.model.GapicClass; +import com.google.api.generator.gapic.model.GapicClass.Kind; +import com.google.api.generator.gapic.model.GapicContext; +import com.google.api.generator.gapic.model.Sample; +import com.google.api.generator.gapic.model.Service; +import com.google.api.generator.test.framework.Assert; +import com.google.api.generator.test.framework.Utils; +import com.google.common.collect.ImmutableList; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; + +public class ComposerTest { + private final GapicContext context = GrpcTestProtoLoader.instance().parseShowcaseEcho(); + private final Service echoProtoService = context.services().get(0); + private final List clazzes = + Arrays.asList( + GrpcServiceCallableFactoryClassComposer.instance().generate(context, echoProtoService)); + private final String protoPackage = context.gapicMetadata().getProtoPackage(); + private final List samples = clazzes.get(0).samples(); + + @Test + public void gapicClass_addApacheLicense() { + ClassDefinition classDef = + ClassDefinition.builder() + .setPackageString("com.google.showcase.v1beta1.stub") + .setName("ComposerPostProcOnFooBar") + .setScope(ScopeNode.PUBLIC) + .build(); + List gapicClassWithHeaderList = + Composer.addApacheLicense(Arrays.asList(GapicClass.create(Kind.TEST, classDef))); + JavaWriterVisitor visitor = new JavaWriterVisitor(); + gapicClassWithHeaderList.get(0).classDefinition().accept(visitor); + Utils.saveCodegenToFile(this.getClass(), "ComposerPostProcOnFooBar.golden", visitor.write()); + Path goldenFilePath = + Paths.get(Utils.getGoldenDir(this.getClass()), "ComposerPostProcOnFooBar.golden"); + Assert.assertCodeEquals(goldenFilePath, visitor.write()); + } + + @Test + public void composeSamples_showcase() { + for (Sample sample : samples) { + assertEquals( + "File header will be empty before composing samples", + sample.fileHeader(), + ImmutableList.of()); + assertEquals( + "ApiShortName will be empty before composing samples", + sample.regionTag().apiShortName(), + ""); + assertEquals( + "ApiVersion will be empty before composing samples", sample.regionTag().apiVersion(), ""); + } + + List composedSamples = + Composer.prepareExecutableSamples(clazzes, protoPackage).get(0).samples(); + + for (Sample sample : composedSamples) { + assertEquals( + "File header should be apache", + sample.fileHeader(), + Arrays.asList(CommentComposer.APACHE_LICENSE_COMMENT)); + assertEquals( + "ApiShortName should be showcase", sample.regionTag().apiShortName(), "showcase"); + assertEquals("ApiVersion should be v1beta1", sample.regionTag().apiVersion(), "v1beta1"); + } + } + + @Test + public void composeSamples_parseProtoPackage() { + String protoPack = "google.cloud.accessapproval.v1"; + List composedSamples = + Composer.prepareExecutableSamples(clazzes, protoPack).get(0).samples(); + + for (Sample sample : composedSamples) { + assertEquals( + "ApiShortName should be accessapproval", + sample.regionTag().apiShortName(), + "accessapproval"); + assertEquals("ApiVersion should be v1", sample.regionTag().apiVersion(), "v1"); + } + + protoPack = "google.cloud.vision.v1p1beta1"; + composedSamples = Composer.prepareExecutableSamples(clazzes, protoPack).get(0).samples(); + for (Sample sample : composedSamples) { + assertEquals("ApiShortName should be vision", sample.regionTag().apiShortName(), "vision"); + assertEquals("ApiVersion should be v1p1beta1", sample.regionTag().apiVersion(), "v1p1beta1"); + } + + protoPack = "google.cloud.vision"; + composedSamples = Composer.prepareExecutableSamples(clazzes, protoPack).get(0).samples(); + for (Sample sample : composedSamples) { + assertEquals("ApiShortName should be vision", sample.regionTag().apiShortName(), "vision"); + assertEquals("ApiVersion should be empty", sample.regionTag().apiVersion(), ""); + } + } +} diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden index f8e145f976..95cfa7bff9 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfiguration.golden @@ -21,7 +21,7 @@ import org.threeten.bp.Duration; // AUTO-GENERATED DOCUMENTATION AND CLASS. /** - * Settings class to configure an instance of {@link EchoSpringAutoConfiguration}. + * Auto-configuration for {@link EchoClient}. * *

Provides auto-configuration for Spring Boot * @@ -48,8 +48,8 @@ public class EchoSpringAutoConfiguration { } /** - * Obtains the default credentials provider. The used key will be obtained from - * application.properties + * Obtains the default credentials provider. The used key will be obtained from Spring Boot + * configuration data files. */ @Bean @ConditionalOnMissingBean @@ -68,14 +68,14 @@ public class EchoSpringAutoConfiguration { } /** - * Provides a Echo client configured to use the default credentials provider (obtained with - * googleCredentials()) and its defaulttransport channel provider + * Provides a EchoClient bean configured to use the default credentials provider (obtained with + * echoCredentials()) and its default transport channel provider * (defaultEchoTransportChannelProvider()). It also configures the quota project ID if provided. * It will configure an executor provider in case there is more than one thread configured in the * client * - *

Individual retry settings are configured as well. It will use the default retry settings - * obtained from EchoProperties when they are not specified + *

Individual retry settings are configured as well. It will use the relevant client library's + * default retry settings when they are not specified in EchoProperties. */ @Bean @ConditionalOnMissingBean diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden index 0a9b17e3da..fea5f436ba 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringAutoConfigurationFull.golden @@ -39,7 +39,7 @@ import org.threeten.bp.Duration; // AUTO-GENERATED DOCUMENTATION AND CLASS. /** - * Settings class to configure an instance of {@link EchoSpringAutoConfiguration}. + * Auto-configuration for {@link EchoClient}. * *

Provides auto-configuration for Spring Boot * @@ -68,8 +68,8 @@ public class EchoSpringAutoConfiguration { } /** - * Obtains the default credentials provider. The used key will be obtained from - * application.properties + * Obtains the default credentials provider. The used key will be obtained from Spring Boot + * configuration data files. */ @Bean @ConditionalOnMissingBean @@ -88,14 +88,14 @@ public class EchoSpringAutoConfiguration { } /** - * Provides a Echo client configured to use the default credentials provider (obtained with - * googleCredentials()) and its defaulttransport channel provider + * Provides a EchoClient bean configured to use the default credentials provider (obtained with + * echoCredentials()) and its default transport channel provider * (defaultEchoTransportChannelProvider()). It also configures the quota project ID if provided. * It will configure an executor provider in case there is more than one thread configured in the * client * - *

Individual retry settings are configured as well. It will use the default retry settings - * obtained from EchoProperties when they are not specified + *

Individual retry settings are configured as well. It will use the relevant client library's + * default retry settings when they are not specified in EchoProperties. */ @Bean @ConditionalOnMissingBean diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringProperties.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringProperties.golden index 63c53fd8d2..28f0e8612d 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringProperties.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringProperties.golden @@ -6,11 +6,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.threeten.bp.Duration; // AUTO-GENERATED DOCUMENTATION AND CLASS. -/** - * Settings class to configure an instance of {@link EchoSpringProperties}. - * - *

Provides default configuration values for Echo client - */ +/** Provides default property values for Echo client bean */ @ConfigurationProperties("com.google.showcase.v1beta1.spring.auto.echo") public class EchoSpringProperties implements CredentialsSupplier { @NestedConfigurationProperty diff --git a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringPropertiesFull.golden b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringPropertiesFull.golden index 0ef92e78b8..fe1ab2fb5f 100644 --- a/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringPropertiesFull.golden +++ b/src/test/java/com/google/api/generator/spring/composer/goldens/EchoSpringPropertiesFull.golden @@ -24,11 +24,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.threeten.bp.Duration; // AUTO-GENERATED DOCUMENTATION AND CLASS. -/** - * Settings class to configure an instance of {@link EchoSpringProperties}. - * - *

Provides default configuration values for Echo client - */ +/** Provides default property values for Echo client bean */ @Generated("by gapic-generator-java") @BetaApi("Autogenerated Spring autoconfiguration is not yet stable") @ConfigurationProperties("com.google.showcase.v1beta1.spring.auto.echo") From ea84a81a284830b5a51ff28bccec8abd660f007a Mon Sep 17 00:00:00 2001 From: Diego Marquez Date: Wed, 2 Nov 2022 19:08:40 +0000 Subject: [PATCH 6/6] fix: format files --- .../composer/SpringAutoConfigClassComposer.java | 2 +- .../composer/SpringPropertiesClassComposer.java | 2 +- .../comment/SpringAutoconfigCommentComposer.java | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java index 05041e958a..952d79335e 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java @@ -48,9 +48,9 @@ import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.GapicServiceConfig; import com.google.api.generator.gapic.model.Service; +import com.google.api.generator.spring.composer.comment.SpringAutoconfigCommentComposer; import com.google.api.generator.spring.utils.LoggerUtils; import com.google.api.generator.spring.utils.Utils; -import com.google.api.generator.spring.composer.comment.SpringAutoconfigCommentComposer; import com.google.common.base.CaseFormat; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java index 90d09448e6..618f6f933b 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringPropertiesClassComposer.java @@ -41,8 +41,8 @@ import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.GapicServiceConfig; import com.google.api.generator.gapic.model.Service; -import com.google.api.generator.spring.utils.Utils; import com.google.api.generator.spring.composer.comment.SpringPropertiesCommentComposer; +import com.google.api.generator.spring.utils.Utils; import com.google.common.base.CaseFormat; import com.google.common.base.Joiner; import java.util.ArrayList; diff --git a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java index 6d6717182b..171ae79909 100644 --- a/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/comment/SpringAutoconfigCommentComposer.java @@ -14,12 +14,10 @@ package com.google.api.generator.spring.composer.comment; - import com.google.api.generator.engine.ast.CommentStatement; import com.google.api.generator.engine.ast.JavaDocComment; import com.google.api.generator.gapic.composer.comment.CommentComposer; import com.google.common.base.CaseFormat; -import com.google.common.base.Joiner; import java.util.Arrays; import java.util.List; @@ -60,8 +58,7 @@ public class SpringAutoconfigCommentComposer { public SpringAutoconfigCommentComposer() {} public static List createClassHeaderComments( - String configuredClassName, - String serviceName) { + String configuredClassName, String serviceName) { JavaDocComment.Builder javaDocCommentBuilder = JavaDocComment.builder() @@ -97,7 +94,11 @@ public static CommentStatement createClientBeanComment( return CommentStatement.withComment( JavaDocComment.builder() .addParagraph( - String.format(CLIENT_BEAN_GENERAL_DESCRIPTION, serviceName, credentialsBaseName, channelProviderName)) + String.format( + CLIENT_BEAN_GENERAL_DESCRIPTION, + serviceName, + credentialsBaseName, + channelProviderName)) .addParagraph( String.format(CLIENT_BEAN_RETRY_SETTINGS_DESCRIPTION, propertiesClazzName)) .build());