github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/internal/plans/objchange/action.go (about)

     1  package objchange
     2  
     3  import (
     4  	"github.com/zclconf/go-cty/cty"
     5  
     6  	"github.com/eliastor/durgaform/internal/plans"
     7  )
     8  
     9  // ActionForChange determines which plans.Action value best describes a
    10  // change from the value given in before to the value given in after.
    11  //
    12  // Because it has no context aside from the values, it can only return the
    13  // basic actions NoOp, Create, Update, and Delete. Other codepaths with
    14  // additional information might make this decision differently, such as by
    15  // using the Replace action instead of the Update action where that makes
    16  // sense.
    17  //
    18  // If the after value is unknown then the action can't be properly decided, and
    19  // so ActionForChange will conservatively return either Create or Update
    20  // depending on whether the before value is null. The before value must always
    21  // be fully known; ActionForChange will panic if it contains any unknown values.
    22  func ActionForChange(before, after cty.Value) plans.Action {
    23  	switch {
    24  	case !after.IsKnown():
    25  		if before.IsNull() {
    26  			return plans.Create
    27  		}
    28  		return plans.Update
    29  	case after.IsNull() && before.IsNull():
    30  		return plans.NoOp
    31  	case after.IsNull() && !before.IsNull():
    32  		return plans.Delete
    33  	case before.IsNull() && !after.IsNull():
    34  		return plans.Create
    35  	case after.RawEquals(before):
    36  		return plans.NoOp
    37  	default:
    38  		return plans.Update
    39  	}
    40  }