terraform: deepcopy should not alloc when nil

This commit is contained in:
Armon Dadgar 2014-09-19 16:15:52 -07:00
parent 8e421caba1
commit 1d96373a54
1 changed files with 16 additions and 10 deletions

View File

@ -94,6 +94,9 @@ func (s *State) deepcopy() *State {
// prune is used to remove any resources that are no longer required // prune is used to remove any resources that are no longer required
func (s *State) prune() { func (s *State) prune() {
if s == nil {
return
}
for _, mod := range s.Modules { for _, mod := range s.Modules {
mod.prune() mod.prune()
} }
@ -391,12 +394,14 @@ func (i *InstanceState) deepcopy() *InstanceState {
return nil return nil
} }
n := &InstanceState{ n := &InstanceState{
ID: i.ID, ID: i.ID,
Attributes: make(map[string]string, len(i.Attributes)), Ephemeral: *i.Ephemeral.deepcopy(),
Ephemeral: *i.Ephemeral.deepcopy(),
} }
for k, v := range i.Attributes { if i.Attributes != nil {
n.Attributes[k] = v n.Attributes = make(map[string]string, len(i.Attributes))
for k, v := range i.Attributes {
n.Attributes[k] = v
}
} }
return n return n
} }
@ -489,11 +494,12 @@ func (e *EphemeralState) deepcopy() *EphemeralState {
if e == nil { if e == nil {
return nil return nil
} }
n := &EphemeralState{ n := &EphemeralState{}
ConnInfo: make(map[string]string, len(e.ConnInfo)), if e.ConnInfo != nil {
} n.ConnInfo = make(map[string]string, len(e.ConnInfo))
for k, v := range e.ConnInfo { for k, v := range e.ConnInfo {
n.ConnInfo[k] = v n.ConnInfo[k] = v
}
} }
return n return n
} }