github.com/opentofu/opentofu@v1.7.1/internal/states/statemgr/transient_inmem.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package statemgr
     7  
     8  import (
     9  	"sync"
    10  
    11  	"github.com/opentofu/opentofu/internal/states"
    12  )
    13  
    14  // NewTransientInMemory returns a Transient implementation that retains
    15  // transient snapshots only in memory, as part of the object.
    16  //
    17  // The given initial state, if any, must not be modified concurrently while
    18  // this function is running, but may be freely modified once this function
    19  // returns without affecting the stored transient snapshot.
    20  func NewTransientInMemory(initial *states.State) Transient {
    21  	return &transientInMemory{
    22  		current: initial.DeepCopy(),
    23  	}
    24  }
    25  
    26  type transientInMemory struct {
    27  	lock    sync.RWMutex
    28  	current *states.State
    29  }
    30  
    31  var _ Transient = (*transientInMemory)(nil)
    32  
    33  func (m *transientInMemory) State() *states.State {
    34  	m.lock.RLock()
    35  	defer m.lock.RUnlock()
    36  
    37  	return m.current.DeepCopy()
    38  }
    39  
    40  func (m *transientInMemory) WriteState(new *states.State) error {
    41  	m.lock.Lock()
    42  	defer m.lock.Unlock()
    43  
    44  	m.current = new.DeepCopy()
    45  	return nil
    46  }