Skip to content

Commit

Permalink
WIP: clean up experimenting approaches for string to duration conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
emmileaf committed Nov 14, 2022
1 parent 6b7e7c8 commit df71cb6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import org.threeten.bp.Duration;

// AUTO-GENERATED DOCUMENTATION AND CLASS.
/** Provides default property values for LanguageService client bean */
/**
* Provides default property values for LanguageService client bean
*/
@Generated("by gapic-generator-java")
@BetaApi("Autogenerated Spring autoconfiguration is not yet stable")
@ConfigurationProperties("com.google.cloud.language.v1.spring.auto.language-service")
Expand All @@ -39,12 +41,12 @@ public class LanguageServiceSpringProperties implements CredentialsSupplier {
private String quotaProjectId;
private Integer executorThreadCount;
private boolean useRest = false;
// Option 2(a): Override setter to accept arg or type java.time.Duration
// Option 2(a): Change setter to accept arg of type java.time.Duration
private Duration analyzeSentimentInitialRetryDelay;
private Double analyzeSentimentRetryDelayMultiplier;
// Option 2(b): Switch type to java.time.Duration but override getter to return threeten.bp.Duration
// Option 2(b): Change property type to java.time.Duration, but have getter to return threeten.bp.Duration
private java.time.Duration analyzeSentimentMaxRetryDelay;
// Option 1: Try ConversionServiceBean
// Option 1 (WIP): Try defining custom converter through @ConfigurationPropertiesBinding
private Duration analyzeSentimentInitialRpcTimeout;
private Double analyzeSentimentRpcTimeoutMultiplier;
private Duration analyzeSentimentMaxRpcTimeout;
Expand Down Expand Up @@ -118,12 +120,11 @@ public Duration getAnalyzeSentimentInitialRetryDelay() {
return this.analyzeSentimentInitialRetryDelay;
}

// public void setAnalyzeSentimentInitialRetryDelay(Duration analyzeSentimentInitialRetryDelay) {
// this.analyzeSentimentInitialRetryDelay = analyzeSentimentInitialRetryDelay;
// }

public void setAnalyzeSentimentInitialRetryDelay(java.time.Duration analyzeSentimentInitialRetryDelay) {
this.analyzeSentimentInitialRetryDelay = Duration.parse(analyzeSentimentInitialRetryDelay.toString());
// Option 2(a): Override setter to accept arg of type java.time.Duration
public void setAnalyzeSentimentInitialRetryDelay(
java.time.Duration analyzeSentimentInitialRetryDelay) {
this.analyzeSentimentInitialRetryDelay = Duration.parse(
analyzeSentimentInitialRetryDelay.toString());
}

public Double getAnalyzeSentimentRetryDelayMultiplier() {
Expand All @@ -134,6 +135,7 @@ public void setAnalyzeSentimentRetryDelayMultiplier(Double analyzeSentimentRetry
this.analyzeSentimentRetryDelayMultiplier = analyzeSentimentRetryDelayMultiplier;
}

// Option 2(b): Change property type to java.time.Duration, but have getter to return threeten.bp.Duration
public Duration getAnalyzeSentimentMaxRetryDelay() {
if (this.analyzeSentimentMaxRetryDelay != null) {
return Duration.parse(this.analyzeSentimentMaxRetryDelay.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@

import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.threeten.bp.Duration;

@Component
@ConfigurationPropertiesBinding
public class SpringDurationConverter implements Converter<java.time.Duration, Duration> {
// @Override
// public Duration convert(String source) {
// return Duration.parse(source);
// }
public class StringToBackportDurationConverter implements Converter<String, Duration> {
@Override
public Duration convert(java.time.Duration source) {
return Duration.parse(source.toString());
public Duration convert(String source) {
return (source == null) ? null : Duration.parse(source);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
import com.google.auth.Credentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.language.v1.LanguageServiceClient;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.threeten.bp.Duration;

class LanguageAutoConfigurationTests {
Expand Down Expand Up @@ -138,20 +144,23 @@ void testRetrySettingsFromProperties() {
+ "PT0.5S",
"com.google.cloud.language.v1.spring.auto.language-service.analyze-sentiment-max-retry-delay="
+ "PT5S")
// "com.google.cloud.language.v1.spring.auto.language-service.analyze-sentiment-initial-rpc-timeout=" + "PT10S")
// "com.google.cloud.language.v1.spring.auto.language-service.analyze-sentiment-initial-rpc-timeout="
// + "PT5M")
.run(ctx -> {
LanguageServiceClient client = ctx.getBean(LanguageServiceClient.class);
RetrySettings retrySettings = client.getSettings().analyzeSentimentSettings()
.getRetrySettings();
// Spring handles String to Double conversion
assertThat(retrySettings.getRetryDelayMultiplier()).isEqualTo(2);
// Spring converts String to java.time.Duration, but not org.threeteh.bp.Duration
// Option 2(a) - override setter to take argument of type java.time.Duration and convert
// since explicit String -> java.time.Duration is supported by Spring
assertThat(retrySettings.getInitialRetryDelay()).isEqualTo(Duration.ofMillis(500));
// Option 2(b) - change property type to java.time.Duration but
// have getter return type org.threeten.bp.Duration
assertThat(retrySettings.getMaxRetryDelay()).isEqualTo(Duration.ofSeconds(5));
assertThat(retrySettings.getInitialRetryDelay()).isInstanceOf(Duration.class);
// Option 1 (WIP) - Add custom converter or ConversionService bean
// Option 1 (WIP) - Try defining custom converter through @ConfigurationPropertiesBinding
// assertThat(retrySettings.getInitialRpcTimeout()).isEqualTo(Duration.ofMinutes(5));
});
}
Expand Down

0 comments on commit df71cb6

Please sign in to comment.