Skip to content

Commit

Permalink
don't precompute actions
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcervante authored and DanielMSchmidt committed Jan 6, 2025
1 parent 11ae302 commit ffdb679
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 47 deletions.
10 changes: 4 additions & 6 deletions internal/command/jsonformat/differ/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@
package differ

import (
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"

"github.com/hashicorp/terraform/internal/command/jsonformat/computed/renderers"
"github.com/hashicorp/terraform/internal/command/jsonformat/structured"
"github.com/hashicorp/terraform/internal/plans"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"

"github.com/hashicorp/terraform/internal/command/jsonformat/computed"

"github.com/hashicorp/terraform/internal/command/jsonprovider"
)

func ComputeDiffForAttribute(change structured.Change, attribute *jsonprovider.Attribute, blockAction plans.Action) computed.Diff {
func ComputeDiffForAttribute(change structured.Change, attribute *jsonprovider.Attribute) computed.Diff {
if attribute.AttributeNestedType != nil {
return computeDiffForNestedAttribute(change, attribute.AttributeNestedType)
}
if attribute.WriteOnly {
return computeDiffForWriteOnlyAttribute(change, blockAction)
}

return ComputeDiffForType(change, unmarshalAttribute(attribute))
}
Expand Down
13 changes: 11 additions & 2 deletions internal/command/jsonformat/differ/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ func ComputeDiffForBlock(change structured.Change, block *jsonprovider.Block) co
current := change.GetDefaultActionForIteration()

blockValue := change.AsMap()
blockAction := change.CalculateAction()

attributes := make(map[string]computed.Diff)
for key, attr := range block.Attributes {
if attr.WriteOnly {
continue
}

childValue := blockValue.GetChild(key)

if !childValue.RelevantAttributes.MatchesPartial() {
Expand All @@ -43,7 +46,7 @@ func ComputeDiffForBlock(change structured.Change, block *jsonprovider.Block) co
childValue.BeforeExplicit = false
childValue.AfterExplicit = false

childChange := ComputeDiffForAttribute(childValue, attr, blockAction)
childChange := ComputeDiffForAttribute(childValue, attr)
if childChange.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil && !attr.WriteOnly {
// Don't record nil values at all in blocks except if they are write-only.
continue
Expand Down Expand Up @@ -115,6 +118,12 @@ func ComputeDiffForBlock(change structured.Change, block *jsonprovider.Block) co
}
}

for name, attr := range block.Attributes {
if attr.WriteOnly {
attributes[name] = computeDiffForWriteOnlyAttribute(change, current)
}
}

return computed.NewDiff(renderers.Block(attributes, blocks), current, change.ReplacePaths.Matches())
}

Expand Down
68 changes: 34 additions & 34 deletions internal/command/jsonformat/differ/differ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,17 +779,17 @@ func TestValue_ObjectAttributes(t *testing.T) {
}

if tc.validateObject != nil {
tc.validateObject(t, ComputeDiffForAttribute(tc.input, attribute, plans.Update))
tc.validateObject(t, ComputeDiffForAttribute(tc.input, attribute))
return
}

if tc.validateSingleDiff != nil {
tc.validateSingleDiff(t, ComputeDiffForAttribute(tc.input, attribute, plans.Update))
tc.validateSingleDiff(t, ComputeDiffForAttribute(tc.input, attribute))
return
}

validate := renderers.ValidateObject(tc.validateDiffs, tc.validateAction, tc.validateReplace)
validate(t, ComputeDiffForAttribute(tc.input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(tc.input, attribute))
})

t.Run("map", func(t *testing.T) {
Expand All @@ -803,22 +803,22 @@ func TestValue_ObjectAttributes(t *testing.T) {
validate := renderers.ValidateMap(map[string]renderers.ValidateDiffFunction{
"element": tc.validateObject,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateSingleDiff != nil {
validate := renderers.ValidateMap(map[string]renderers.ValidateDiffFunction{
"element": tc.validateSingleDiff,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

validate := renderers.ValidateMap(map[string]renderers.ValidateDiffFunction{
"element": renderers.ValidateObject(tc.validateDiffs, tc.validateAction, tc.validateReplace),
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})

t.Run("list", func(t *testing.T) {
Expand All @@ -829,30 +829,30 @@ func TestValue_ObjectAttributes(t *testing.T) {
input := wrapChangeInSlice(tc.input)

if tc.validateList != nil {
tc.validateList(t, ComputeDiffForAttribute(input, attribute, plans.Update))
tc.validateList(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateObject != nil {
validate := renderers.ValidateList([]renderers.ValidateDiffFunction{
tc.validateObject,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateSingleDiff != nil {
validate := renderers.ValidateList([]renderers.ValidateDiffFunction{
tc.validateSingleDiff,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

validate := renderers.ValidateList([]renderers.ValidateDiffFunction{
renderers.ValidateObject(tc.validateDiffs, tc.validateAction, tc.validateReplace),
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})

t.Run("set", func(t *testing.T) {
Expand All @@ -869,30 +869,30 @@ func TestValue_ObjectAttributes(t *testing.T) {
ret = append(ret, tc.validateSetDiffs.After.Validate(renderers.ValidateObject))
return ret
}(), collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateObject != nil {
validate := renderers.ValidateSet([]renderers.ValidateDiffFunction{
tc.validateObject,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateSingleDiff != nil {
validate := renderers.ValidateSet([]renderers.ValidateDiffFunction{
tc.validateSingleDiff,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

validate := renderers.ValidateSet([]renderers.ValidateDiffFunction{
renderers.ValidateObject(tc.validateDiffs, tc.validateAction, tc.validateReplace),
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})
})

Expand All @@ -914,17 +914,17 @@ func TestValue_ObjectAttributes(t *testing.T) {
}

if tc.validateNestedObject != nil {
tc.validateNestedObject(t, ComputeDiffForAttribute(tc.input, attribute, plans.Update))
tc.validateNestedObject(t, ComputeDiffForAttribute(tc.input, attribute))
return
}

if tc.validateSingleDiff != nil {
tc.validateSingleDiff(t, ComputeDiffForAttribute(tc.input, attribute, plans.Update))
tc.validateSingleDiff(t, ComputeDiffForAttribute(tc.input, attribute))
return
}

validate := renderers.ValidateNestedObject(tc.validateDiffs, tc.validateAction, tc.validateReplace)
validate(t, ComputeDiffForAttribute(tc.input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(tc.input, attribute))
})

t.Run("map", func(t *testing.T) {
Expand All @@ -949,22 +949,22 @@ func TestValue_ObjectAttributes(t *testing.T) {
validate := renderers.ValidateMap(map[string]renderers.ValidateDiffFunction{
"element": tc.validateNestedObject,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateSingleDiff != nil {
validate := renderers.ValidateMap(map[string]renderers.ValidateDiffFunction{
"element": tc.validateSingleDiff,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

validate := renderers.ValidateMap(map[string]renderers.ValidateDiffFunction{
"element": renderers.ValidateNestedObject(tc.validateDiffs, tc.validateAction, tc.validateReplace),
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})

t.Run("list", func(t *testing.T) {
Expand All @@ -989,22 +989,22 @@ func TestValue_ObjectAttributes(t *testing.T) {
validate := renderers.ValidateNestedList([]renderers.ValidateDiffFunction{
tc.validateNestedObject,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateSingleDiff != nil {
validate := renderers.ValidateNestedList([]renderers.ValidateDiffFunction{
tc.validateSingleDiff,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

validate := renderers.ValidateNestedList([]renderers.ValidateDiffFunction{
renderers.ValidateNestedObject(tc.validateDiffs, tc.validateAction, tc.validateReplace),
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})

t.Run("set", func(t *testing.T) {
Expand Down Expand Up @@ -1032,30 +1032,30 @@ func TestValue_ObjectAttributes(t *testing.T) {
ret = append(ret, tc.validateSetDiffs.After.Validate(renderers.ValidateNestedObject))
return ret
}(), collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateNestedObject != nil {
validate := renderers.ValidateSet([]renderers.ValidateDiffFunction{
tc.validateNestedObject,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

if tc.validateSingleDiff != nil {
validate := renderers.ValidateSet([]renderers.ValidateDiffFunction{
tc.validateSingleDiff,
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

validate := renderers.ValidateSet([]renderers.ValidateDiffFunction{
renderers.ValidateNestedObject(tc.validateDiffs, tc.validateAction, tc.validateReplace),
}, collectionDefaultAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})
})
}
Expand Down Expand Up @@ -1840,7 +1840,7 @@ func TestValue_PrimitiveAttributes(t *testing.T) {
t.Run("direct", func(t *testing.T) {
tc.validateDiff(t, ComputeDiffForAttribute(tc.input, &jsonprovider.Attribute{
AttributeType: unmarshalType(t, tc.attribute),
}, plans.Update))
}))
})

t.Run("map", func(t *testing.T) {
Expand All @@ -1852,7 +1852,7 @@ func TestValue_PrimitiveAttributes(t *testing.T) {
validate := renderers.ValidateMap(map[string]renderers.ValidateDiffFunction{
"element": tc.validateDiff,
}, defaultCollectionsAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})

t.Run("list", func(t *testing.T) {
Expand All @@ -1863,14 +1863,14 @@ func TestValue_PrimitiveAttributes(t *testing.T) {

if tc.validateSliceDiffs != nil {
validate := renderers.ValidateList(tc.validateSliceDiffs, defaultCollectionsAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

validate := renderers.ValidateList([]renderers.ValidateDiffFunction{
tc.validateDiff,
}, defaultCollectionsAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})

t.Run("set", func(t *testing.T) {
Expand All @@ -1881,14 +1881,14 @@ func TestValue_PrimitiveAttributes(t *testing.T) {

if tc.validateSliceDiffs != nil {
validate := renderers.ValidateSet(tc.validateSetDiffs, defaultCollectionsAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
return
}

validate := renderers.ValidateSet([]renderers.ValidateDiffFunction{
tc.validateDiff,
}, defaultCollectionsAction, false)
validate(t, ComputeDiffForAttribute(input, attribute, plans.Update))
validate(t, ComputeDiffForAttribute(input, attribute))
})
})
}
Expand Down Expand Up @@ -2260,7 +2260,7 @@ func TestValue_CollectionAttributes(t *testing.T) {
}

t.Run(name, func(t *testing.T) {
tc.validateDiff(t, ComputeDiffForAttribute(tc.input, tc.attribute, plans.Update))
tc.validateDiff(t, ComputeDiffForAttribute(tc.input, tc.attribute))
})
}
}
Expand Down
Loading

0 comments on commit ffdb679

Please sign in to comment.