From cc1c01916635a18876cab2ee5a7887a6c640daf0 Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 6 Mar 2019 13:02:01 -0800 Subject: [PATCH] Safely access maps in storage transfer (#3185) /cc @rileykarson --- google/resource_storage_transfer_job.go | 47 +++++++++++++++++++------ 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/google/resource_storage_transfer_job.go b/google/resource_storage_transfer_job.go index 5e945553..e2b07115 100644 --- a/google/resource_storage_transfer_job.go +++ b/google/resource_storage_transfer_job.go @@ -491,12 +491,23 @@ func expandDates(dates []interface{}) *storagetransfer.Date { return nil } - date := dates[0].([]interface{}) - return &storagetransfer.Date{ - Day: int64(extractFirstMapConfig(date)["day"].(int)), - Month: int64(extractFirstMapConfig(date)["month"].(int)), - Year: int64(extractFirstMapConfig(date)["year"].(int)), + dateElem := dates[0].([]interface{}) + date := &storagetransfer.Date{} + + dateMap := extractFirstMapConfig(dateElem) + if v, ok := dateMap["day"]; ok { + date.Day = int64(v.(int)) } + + if v, ok := dateMap["month"]; ok { + date.Month = int64(v.(int)) + } + + if v, ok := dateMap["year"]; ok { + date.Year = int64(v.(int)) + } + + return date } func flattenDate(date *storagetransfer.Date) []map[string]interface{} { @@ -514,13 +525,27 @@ func expandTimeOfDays(times []interface{}) *storagetransfer.TimeOfDay { return nil } - time := times[0].([]interface{}) - return &storagetransfer.TimeOfDay{ - Hours: int64(extractFirstMapConfig(time)["hours"].(int)), - Minutes: int64(extractFirstMapConfig(time)["minutes"].(int)), - Seconds: int64(extractFirstMapConfig(time)["seconds"].(int)), - Nanos: int64(extractFirstMapConfig(time)["nanos"].(int)), + timeElem := times[0].([]interface{}) + time := &storagetransfer.TimeOfDay{} + + timeMap := extractFirstMapConfig(timeElem) + if v, ok := timeMap["hours"]; ok { + time.Hours = int64(v.(int)) } + + if v, ok := timeMap["minutes"]; ok { + time.Minutes = int64(v.(int)) + } + + if v, ok := timeMap["seconds"]; ok { + time.Seconds = int64(v.(int)) + } + + if v, ok := timeMap["nanos"]; ok { + time.Nanos = int64(v.(int)) + } + + return time } func flattenTimeOfDay(timeOfDay *storagetransfer.TimeOfDay) []map[string]interface{} {