4 "github.com/hashicorp/terraform/addrs"
5 "github.com/zclconf/go-cty/cty"
8 // Taking deep copies of states is an important operation because state is
9 // otherwise a mutable data structure that is challenging to share across
10 // many separate callers. It is important that the DeepCopy implementations
11 // in this file comprehensively copy all parts of the state data structure
12 // that could be mutated via pointers.
14 // DeepCopy returns a new state that contains equivalent data to the reciever
15 // but shares no backing memory in common.
17 // As with all methods on State, this method is not safe to use concurrently
18 // with writing to any portion of the recieving data structure. It is the
19 // caller's responsibility to ensure mutual exclusion for the duration of the
20 // operation, but may then freely modify the receiver and the returned copy
21 // independently once this method returns.
22 func (s *State) DeepCopy() *State {
27 modules := make(map[string]*Module, len(s.Modules))
28 for k, m := range s.Modules {
29 modules[k] = m.DeepCopy()
36 // DeepCopy returns a new module state that contains equivalent data to the
37 // receiver but shares no backing memory in common.
39 // As with all methods on Module, this method is not safe to use concurrently
40 // with writing to any portion of the recieving data structure. It is the
41 // caller's responsibility to ensure mutual exclusion for the duration of the
42 // operation, but may then freely modify the receiver and the returned copy
43 // independently once this method returns.
44 func (ms *Module) DeepCopy() *Module {
49 resources := make(map[string]*Resource, len(ms.Resources))
50 for k, r := range ms.Resources {
51 resources[k] = r.DeepCopy()
53 outputValues := make(map[string]*OutputValue, len(ms.OutputValues))
54 for k, v := range ms.OutputValues {
55 outputValues[k] = v.DeepCopy()
57 localValues := make(map[string]cty.Value, len(ms.LocalValues))
58 for k, v := range ms.LocalValues {
59 // cty.Value is immutable, so we don't need to copy these.
64 Addr: ms.Addr, // technically mutable, but immutable by convention
66 OutputValues: outputValues,
67 LocalValues: localValues,
71 // DeepCopy returns a new resource state that contains equivalent data to the
72 // receiver but shares no backing memory in common.
74 // As with all methods on Resource, this method is not safe to use concurrently
75 // with writing to any portion of the recieving data structure. It is the
76 // caller's responsibility to ensure mutual exclusion for the duration of the
77 // operation, but may then freely modify the receiver and the returned copy
78 // independently once this method returns.
79 func (rs *Resource) DeepCopy() *Resource {
84 instances := make(map[addrs.InstanceKey]*ResourceInstance, len(rs.Instances))
85 for k, i := range rs.Instances {
86 instances[k] = i.DeepCopy()
91 EachMode: rs.EachMode,
93 ProviderConfig: rs.ProviderConfig, // technically mutable, but immutable by convention
97 // DeepCopy returns a new resource instance state that contains equivalent data
98 // to the receiver but shares no backing memory in common.
100 // As with all methods on ResourceInstance, this method is not safe to use
101 // concurrently with writing to any portion of the recieving data structure. It
102 // is the caller's responsibility to ensure mutual exclusion for the duration
103 // of the operation, but may then freely modify the receiver and the returned
104 // copy independently once this method returns.
105 func (is *ResourceInstance) DeepCopy() *ResourceInstance {
110 deposed := make(map[DeposedKey]*ResourceInstanceObjectSrc, len(is.Deposed))
111 for k, obj := range is.Deposed {
112 deposed[k] = obj.DeepCopy()
115 return &ResourceInstance{
116 Current: is.Current.DeepCopy(),
121 // DeepCopy returns a new resource instance object that contains equivalent data
122 // to the receiver but shares no backing memory in common.
124 // As with all methods on ResourceInstanceObjectSrc, this method is not safe to
125 // use concurrently with writing to any portion of the recieving data structure.
126 // It is the caller's responsibility to ensure mutual exclusion for the duration
127 // of the operation, but may then freely modify the receiver and the returned
128 // copy independently once this method returns.
129 func (obj *ResourceInstanceObjectSrc) DeepCopy() *ResourceInstanceObjectSrc {
134 var attrsFlat map[string]string
135 if obj.AttrsFlat != nil {
136 attrsFlat = make(map[string]string, len(obj.AttrsFlat))
137 for k, v := range obj.AttrsFlat {
143 if obj.AttrsJSON != nil {
144 attrsJSON = make([]byte, len(obj.AttrsJSON))
145 copy(attrsJSON, obj.AttrsJSON)
149 if obj.Private != nil {
150 private := make([]byte, len(obj.Private))
151 copy(private, obj.Private)
154 // Some addrs.Referencable implementations are technically mutable, but
155 // we treat them as immutable by convention and so we don't deep-copy here.
156 dependencies := make([]addrs.Referenceable, len(obj.Dependencies))
157 copy(dependencies, obj.Dependencies)
159 return &ResourceInstanceObjectSrc{
161 SchemaVersion: obj.SchemaVersion,
163 AttrsFlat: attrsFlat,
164 AttrsJSON: attrsJSON,
165 Dependencies: dependencies,
169 // DeepCopy returns a new resource instance object that contains equivalent data
170 // to the receiver but shares no backing memory in common.
172 // As with all methods on ResourceInstanceObject, this method is not safe to use
173 // concurrently with writing to any portion of the recieving data structure. It
174 // is the caller's responsibility to ensure mutual exclusion for the duration
175 // of the operation, but may then freely modify the receiver and the returned
176 // copy independently once this method returns.
177 func (obj *ResourceInstanceObject) DeepCopy() *ResourceInstanceObject {
183 if obj.Private != nil {
184 private := make([]byte, len(obj.Private))
185 copy(private, obj.Private)
188 // Some addrs.Referencable implementations are technically mutable, but
189 // we treat them as immutable by convention and so we don't deep-copy here.
190 dependencies := make([]addrs.Referenceable, len(obj.Dependencies))
191 copy(dependencies, obj.Dependencies)
193 return &ResourceInstanceObject{
197 Dependencies: dependencies,
201 // DeepCopy returns a new output value state that contains equivalent data
202 // to the receiver but shares no backing memory in common.
204 // As with all methods on OutputValue, this method is not safe to use
205 // concurrently with writing to any portion of the recieving data structure. It
206 // is the caller's responsibility to ensure mutual exclusion for the duration
207 // of the operation, but may then freely modify the receiver and the returned
208 // copy independently once this method returns.
209 func (os *OutputValue) DeepCopy() *OutputValue {
216 Sensitive: os.Sensitive,