ソースを参照

list for null

wfansh 5 ヶ月 前
コミット
f65a8fd4c6

+ 22 - 15
src/main/java/com/wechi/adweb/bridge/google/gtm/service/GTMService.java

@@ -28,6 +28,7 @@ import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 
 import javax.annotation.PostConstruct;
 
@@ -69,7 +70,9 @@ public class GTMService {
     public List<GTMAccountDTO> listAccounts() throws DataException {
         try {
             List<GTMAccountDTO> gtmAccounts = Lists.newArrayList();
-            for (Account account : tagManager.accounts().list().execute().getAccount()) {
+            for (Account account :
+                    Optional.ofNullable(tagManager.accounts().list().execute().getAccount())
+                            .orElse(List.of())) {
                 GTMAccountDTO gtmAccount =
                         GTMAccountDTO.builder()
                                 .id(account.getAccountId())
@@ -97,12 +100,14 @@ public class GTMService {
         try {
             List<GTMContainerDTO> gtmContainers = Lists.newArrayList();
             for (Container container :
-                    tagManager
-                            .accounts()
-                            .containers()
-                            .list(accountResourceName)
-                            .execute()
-                            .getContainer()) {
+                    Optional.ofNullable(
+                                    tagManager
+                                            .accounts()
+                                            .containers()
+                                            .list(accountResourceName)
+                                            .execute()
+                                            .getContainer())
+                            .orElse(List.of())) {
                 GTMContainerDTO gtmContainer =
                         GTMContainerDTO.builder()
                                 .id(container.getContainerId())
@@ -141,14 +146,16 @@ public class GTMService {
         try {
             List<GTMTagDTO> gtmTags = Lists.newArrayList();
             for (Tag tag :
-                    tagManager
-                            .accounts()
-                            .containers()
-                            .workspaces()
-                            .tags()
-                            .list(workspaceResourceName)
-                            .execute()
-                            .getTag()) {
+                    Optional.ofNullable(
+                                    tagManager
+                                            .accounts()
+                                            .containers()
+                                            .workspaces()
+                                            .tags()
+                                            .list(workspaceResourceName)
+                                            .execute()
+                                            .getTag())
+                            .orElse(List.of())) {
                 GTMTagDTO gtmTag =
                         GTMTagDTO.builder()
                                 .id(tag.getTagId())