Repository: opentofu/opentofu
File: internal/command/jsonplan/values.go (Go)

Code before:
			Sensitive: oc.Sensitive,
		}
	}

	return ret, nil

}

func marshalPlannedValues(changes *plans.Changes, schemas *tofu.Schemas) (Module, error) {
	var ret Module

	// build three maps:
	// 		module name -> [resource addresses]
	// 		module -> [children modules]
	// 		resource addr -> change src
	moduleResourceMap := make(map[string][]addrs.AbsResourceInstance)
	moduleMap := make(map[string][]addrs.ModuleInstance)
	changeMap := make(map[string]*plans.ResourceInstanceChangeSrc)
	seenModules := make(map[string]bool)

	for _, resource := range changes.Resources {
		// If the resource is being deleted, skip over it.
		// Deposed instances are always conceptually a destroy, but if they
		// were gone during refresh then the change becomes a noop.
		if resource.Action != plans.Delete && resource.DeposedKey == states.NotDeposed {
			changeMap[resource.Addr.String()] = resource
			containingModule := resource.Addr.Module.String()
			moduleResourceMap[containingModule] = append(moduleResourceMap[containingModule], resource.Addr)

			// the root module has no parents
			if !resource.Addr.Module.IsRoot() {
				parent := resource.Addr.Module.Parent().String()
				// we expect to see multiple resources in one module, so we
				// only need to report the "parent" module for each child module
				// once.
				if !seenModules[containingModule] {
					moduleMap[parent] = append(moduleMap[parent], resource.Addr.Module)
					seenModules[containingModule] = true
				}

				// If any given parent module has no resources, it needs to be
				// added to the moduleMap. This walks through the current
				// resources' modules' ancestors, taking advantage of the fact
				// that Ancestors() returns an ordered slice, and verifies that
				// each one is in the map.
				ancestors := resource.Addr.Module.Ancestors()
				for i, ancestor := range ancestors[:len(ancestors)-1] {
					aStr := ancestor.String()

					// childStr here is the immediate child of the current step
					childStr := ancestors[i+1].String()

Review comment (performance):
nit: I would prefer `changeMap` content to be equal to what `plans.Changes.ResourceInstance` returns, i.e. all `NotDeposed` changes. Then, it makes sense to rename `changeMap` to what reflects its content better, e.g. `notDeposedChangeMap`. This way we keep the original behaviour and make the retrieval more efficient. WDYT?

Code after:
			Sensitive: oc.Sensitive,
		}
	}

	return ret, nil

}

func marshalPlannedValues(changes *plans.Changes, schemas *tofu.Schemas) (Module, error) {
	var ret Module

	// build three maps:
	// 		module name -> [resource addresses]
	// 		module -> [children modules]
	// 		resource addr -> change src
	moduleResourceMap := make(map[string][]addrs.AbsResourceInstance)
	moduleMap := make(map[string][]addrs.ModuleInstance)
	changeMap := make(map[string]*plans.ResourceInstanceChangeSrc)
	seenModules := make(map[string]bool)

	for _, resource := range changes.Resources {
		// Deposed instances are always conceptually a destroy, but if they
		// were gone during refresh then the change becomes a noop.
		if resource.DeposedKey != states.NotDeposed {
			continue
		}

		changeMap[resource.Addr.String()] = resource

		// If the resource is being deleted, skip over it.
		if resource.Action == plans.Delete {
			continue
		}

		containingModule := resource.Addr.Module.String()
		moduleResourceMap[containingModule] = append(moduleResourceMap[containingModule], resource.Addr)

		// the root module has no parents
		if resource.Addr.Module.IsRoot() {
			continue
		}

		parent := resource.Addr.Module.Parent().String()

		// we expect to see multiple resources in one module, so we
		// only need to report the "parent" module for each child module
		// once.
		if !seenModules[containingModule] {
			moduleMap[parent] = append(moduleMap[parent], resource.Addr.Module)
			seenModules[containingModule] = true
		}