Skip to content

Commit

Permalink
Clean up Optional usage in BigQueryConnectorModule
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Melnychuk authored and kokosing committed Aug 10, 2021
1 parent 5d63f07 commit 58832bc
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ public BigQueryClient provideBigQueryClient(BigQueryConfig config, HeaderProvide
static String calculateBillingProjectId(Optional<String> configParentProjectId, Optional<Credentials> credentials)
{
// 1. Get from configuration
if (configParentProjectId.isPresent()) {
return configParentProjectId.get();
}
// 2. Get from the provided credentials, but only ServiceAccountCredentials contains the project id.
// All other credentials types (User, AppEngine, GCE, CloudShell, etc.) take it from the environment
if (credentials.isPresent() && credentials.get() instanceof ServiceAccountCredentials) {
return ((ServiceAccountCredentials) credentials.get()).getProjectId();
}
// 3. No configuration was provided, so get the default from the environment
return BigQueryOptions.getDefaultProjectId();
return configParentProjectId
// 2. Get from the provided credentials, but only ServiceAccountCredentials contains the project id.
// All other credentials types (User, AppEngine, GCE, CloudShell, etc.) take it from the environment
.orElseGet(() -> credentials
.filter(ServiceAccountCredentials.class::isInstance)
.map(ServiceAccountCredentials.class::cast)
.map(ServiceAccountCredentials::getProjectId)
// 3. No configuration was provided, so get the default from the environment
.orElseGet(BigQueryOptions::getDefaultProjectId));
}
}

0 comments on commit 58832bc

Please sign in to comment.