Skip to content

Commit cc1c019

Browse files
modular-magicianrileykarson
authored andcommitted
Safely access maps in storage transfer (#3185)
<!-- This change is generated by MagicModules. --> /cc @rileykarson
1 parent 11bb321 commit cc1c019

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

google/resource_storage_transfer_job.go

+36-11
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,23 @@ func expandDates(dates []interface{}) *storagetransfer.Date {
491491
return nil
492492
}
493493

494-
date := dates[0].([]interface{})
495-
return &storagetransfer.Date{
496-
Day: int64(extractFirstMapConfig(date)["day"].(int)),
497-
Month: int64(extractFirstMapConfig(date)["month"].(int)),
498-
Year: int64(extractFirstMapConfig(date)["year"].(int)),
494+
dateElem := dates[0].([]interface{})
495+
date := &storagetransfer.Date{}
496+
497+
dateMap := extractFirstMapConfig(dateElem)
498+
if v, ok := dateMap["day"]; ok {
499+
date.Day = int64(v.(int))
500+
}
501+
502+
if v, ok := dateMap["month"]; ok {
503+
date.Month = int64(v.(int))
499504
}
505+
506+
if v, ok := dateMap["year"]; ok {
507+
date.Year = int64(v.(int))
508+
}
509+
510+
return date
500511
}
501512

502513
func flattenDate(date *storagetransfer.Date) []map[string]interface{} {
@@ -514,13 +525,27 @@ func expandTimeOfDays(times []interface{}) *storagetransfer.TimeOfDay {
514525
return nil
515526
}
516527

517-
time := times[0].([]interface{})
518-
return &storagetransfer.TimeOfDay{
519-
Hours: int64(extractFirstMapConfig(time)["hours"].(int)),
520-
Minutes: int64(extractFirstMapConfig(time)["minutes"].(int)),
521-
Seconds: int64(extractFirstMapConfig(time)["seconds"].(int)),
522-
Nanos: int64(extractFirstMapConfig(time)["nanos"].(int)),
528+
timeElem := times[0].([]interface{})
529+
time := &storagetransfer.TimeOfDay{}
530+
531+
timeMap := extractFirstMapConfig(timeElem)
532+
if v, ok := timeMap["hours"]; ok {
533+
time.Hours = int64(v.(int))
534+
}
535+
536+
if v, ok := timeMap["minutes"]; ok {
537+
time.Minutes = int64(v.(int))
538+
}
539+
540+
if v, ok := timeMap["seconds"]; ok {
541+
time.Seconds = int64(v.(int))
523542
}
543+
544+
if v, ok := timeMap["nanos"]; ok {
545+
time.Nanos = int64(v.(int))
546+
}
547+
548+
return time
524549
}
525550

526551
func flattenTimeOfDay(timeOfDay *storagetransfer.TimeOfDay) []map[string]interface{} {

0 commit comments

Comments
 (0)