mirror of https://github.com/pulumi/pulumi.git
84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
// Copyright 2020-2024, Pulumi Corporation.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package pcl
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func RewritePropertyReferences(expr model.Expression) model.Expression {
|
|
rewriter := func(expr model.Expression) (model.Expression, hcl.Diagnostics) {
|
|
traversal, ok := expr.(*model.ScopeTraversalExpression)
|
|
if !ok {
|
|
return expr, nil
|
|
}
|
|
|
|
p, ok := traversal.Parts[len(traversal.Parts)-1].(*ResourceProperty)
|
|
if !ok {
|
|
return expr, nil
|
|
}
|
|
|
|
var buffer bytes.Buffer
|
|
for _, t := range p.Path {
|
|
var err error
|
|
switch t := t.(type) {
|
|
case hcl.TraverseRoot:
|
|
_, err = fmt.Fprint(&buffer, t.Name)
|
|
case hcl.TraverseAttr:
|
|
_, err = fmt.Fprintf(&buffer, ".%s", t.Name)
|
|
case hcl.TraverseIndex:
|
|
switch t.Key.Type() {
|
|
case cty.String:
|
|
_, err = fmt.Fprintf(&buffer, ".%s", t.Key.AsString())
|
|
case cty.Number:
|
|
idx, _ := t.Key.AsBigFloat().Int64()
|
|
_, err = fmt.Fprintf(&buffer, "[%d]", idx)
|
|
default:
|
|
contract.Failf("unexpected traversal index of type %v", t.Key.Type())
|
|
}
|
|
}
|
|
contract.IgnoreError(err)
|
|
}
|
|
|
|
// TODO: transfer internal trivia
|
|
|
|
propertyPath := cty.StringVal(buffer.String())
|
|
value := &model.TemplateExpression{
|
|
Parts: []model.Expression{
|
|
&model.LiteralValueExpression{
|
|
Tokens: syntax.NewLiteralValueTokens(propertyPath),
|
|
Value: propertyPath,
|
|
},
|
|
},
|
|
}
|
|
value.SetLeadingTrivia(expr.GetLeadingTrivia())
|
|
value.SetTrailingTrivia(expr.GetTrailingTrivia())
|
|
diags := value.Typecheck(false)
|
|
contract.Assertf(len(diags) == 0, "error typechecking template expression: %v", diags)
|
|
return value, nil
|
|
}
|
|
|
|
expr, diags := model.VisitExpression(expr, model.IdentityVisitor, rewriter)
|
|
contract.Assertf(len(diags) == 0, "error rewriting property references: %v", diags)
|
|
return expr
|
|
}
|