@@ -35,11 +35,11 @@ type Loader struct {
3535// Constructs a new [Loader], with an initial empty plan.
3636func NewLoader () * Loader {
3737 ret := & Plan {
38+ Root : newStackInstance (stackaddrs .RootStackInstance ),
3839 RootInputValues : make (map [stackaddrs.InputVariable ]cty.Value ),
3940 ApplyTimeInputVariables : collections .NewSetCmp [stackaddrs.InputVariable ](),
4041 DeletedInputVariables : collections .NewSet [stackaddrs.InputVariable ](),
4142 DeletedOutputValues : collections .NewSet [stackaddrs.OutputValue ](),
42- Components : collections .NewMap [stackaddrs.AbsComponentInstance , * Component ](),
4343 DeletedComponents : collections .NewSet [stackaddrs.AbsComponentInstance ](),
4444 PrevRunStateRaw : make (map [string ]* anypb.Any ),
4545 }
@@ -220,27 +220,24 @@ func (l *Loader) AddRaw(rawMsg *anypb.Any) error {
220220 })
221221 }
222222
223- if ! l .ret .Components .HasKey (addr ) {
224- l .ret .Components .Put (addr , & Component {
225- PlannedAction : plannedAction ,
226- Mode : mode ,
227- PlanApplyable : msg .PlanApplyable ,
228- PlanComplete : msg .PlanComplete ,
229- Dependencies : dependencies ,
230- Dependents : collections .NewSet [stackaddrs.AbsComponent ](),
231- PlannedInputValues : inputVals ,
232- PlannedInputValueMarks : inputValMarks ,
233- PlannedOutputValues : outputVals ,
234- PlannedChecks : checkResults ,
235- PlannedFunctionResults : functionResults ,
236-
237- ResourceInstancePlanned : addrs .MakeMap [addrs.AbsResourceInstanceObject , * plans.ResourceInstanceChangeSrc ](),
238- ResourceInstancePriorState : addrs .MakeMap [addrs.AbsResourceInstanceObject , * states.ResourceInstanceObjectSrc ](),
239- ResourceInstanceProviderConfig : addrs .MakeMap [addrs.AbsResourceInstanceObject , addrs.AbsProviderConfig ](),
240- DeferredResourceInstanceChanges : addrs .MakeMap [addrs.AbsResourceInstanceObject , * plans.DeferredResourceInstanceChangeSrc ](),
241- })
242- }
243- c := l .ret .Components .Get (addr )
223+ c := l .ret .GetOrCreate (addr , & Component {
224+ PlannedAction : plannedAction ,
225+ Mode : mode ,
226+ PlanApplyable : msg .PlanApplyable ,
227+ PlanComplete : msg .PlanComplete ,
228+ Dependencies : dependencies ,
229+ Dependents : collections .NewSet [stackaddrs.AbsComponent ](),
230+ PlannedInputValues : inputVals ,
231+ PlannedInputValueMarks : inputValMarks ,
232+ PlannedOutputValues : outputVals ,
233+ PlannedChecks : checkResults ,
234+ PlannedFunctionResults : functionResults ,
235+
236+ ResourceInstancePlanned : addrs .MakeMap [addrs.AbsResourceInstanceObject , * plans.ResourceInstanceChangeSrc ](),
237+ ResourceInstancePriorState : addrs .MakeMap [addrs.AbsResourceInstanceObject , * states.ResourceInstanceObjectSrc ](),
238+ ResourceInstanceProviderConfig : addrs .MakeMap [addrs.AbsResourceInstanceObject , addrs.AbsProviderConfig ](),
239+ DeferredResourceInstanceChanges : addrs .MakeMap [addrs.AbsResourceInstanceObject , * plans.DeferredResourceInstanceChangeSrc ](),
240+ })
244241 err = c .PlanTimestamp .UnmarshalText ([]byte (msg .PlanTimestamp ))
245242 if err != nil {
246243 return fmt .Errorf ("invalid plan timestamp %q for %s" , msg .PlanTimestamp , addr )
@@ -329,26 +326,15 @@ func (l *Loader) Plan() (*Plan, error) {
329326
330327 // Before we return we'll calculate the reverse dependency information
331328 // based on the forward dependency information we loaded above.
332- for dependentInstAddr , dependencyInst := range l .ret .Components . All () {
329+ for dependentInstAddr , dependencyInst := range l .ret .AllComponents () {
333330 dependentAddr := stackaddrs.AbsComponent {
334331 Stack : dependentInstAddr .Stack ,
335332 Item : dependentInstAddr .Item .Component ,
336333 }
337334
338335 for dependencyAddr := range dependencyInst .Dependencies .All () {
339- // FIXME: This is very inefficient because the current data structure doesn't
340- // allow looking up all of the component instances that have a particular
341- // component. This'll be okay as long as the number of components is
342- // small, but we'll need to improve this if we ever want to support stacks
343- // with a large number of components.
344- for maybeDependencyInstAddr , dependencyInst := range l .ret .Components .All () {
345- maybeDependencyAddr := stackaddrs.AbsComponent {
346- Stack : maybeDependencyInstAddr .Stack ,
347- Item : maybeDependencyInstAddr .Item .Component ,
348- }
349- if dependencyAddr .UniqueKey () == maybeDependencyAddr .UniqueKey () {
350- dependencyInst .Dependents .Add (dependentAddr )
351- }
336+ for _ , dependencyInst := range l .ret .ComponentInstances (dependencyAddr ) {
337+ dependencyInst .Dependents .Add (dependentAddr )
352338 }
353339 }
354340 }
@@ -439,7 +425,7 @@ func LoadComponentForResourceInstance(plan *Plan, change *tfstackdata1.PlanResou
439425 DeposedKey : deposedKey ,
440426 }
441427
442- c , ok := plan .Components .GetOk (cAddr )
428+ c , ok := plan .Root .GetOk (cAddr )
443429 if ! ok {
444430 return nil , addrs.AbsResourceInstanceObject {}, addrs.AbsProviderConfig {}, fmt .Errorf ("resource instance change for unannounced component instance %s" , cAddr )
445431 }
@@ -476,7 +462,7 @@ func LoadComponentForPartialResourceInstance(plan *Plan, change *tfstackdata1.Pl
476462 DeposedKey : deposedKey ,
477463 }
478464
479- c , ok := plan .Components .GetOk (cAddr )
465+ c , ok := plan .Root .GetOk (cAddr )
480466 if ! ok {
481467 return nil , addrs.AbsResourceInstanceObject {}, addrs.AbsProviderConfig {}, fmt .Errorf ("resource instance change for unannounced component instance %s" , cAddr )
482468 }
0 commit comments