pulumi/pkg/codegen/go/gen_program_read_dir.go

84 lines
2.1 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 gen
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
)
type readDirTemp struct {
Name string
Value *model.FunctionCallExpression
}
func (rt *readDirTemp) Type() model.Type {
return rt.Value.Type()
}
func (rt *readDirTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) {
return rt.Type().Traverse(traverser)
}
func (rt *readDirTemp) SyntaxNode() hclsyntax.Node {
return syntax.None
}
type readDirSpiller struct {
temps []*readDirTemp
count int
}
func (rs *readDirSpiller) spillExpression(x model.Expression) (model.Expression, hcl.Diagnostics) {
var temp *readDirTemp
scopeName := ""
switch x := x.(type) {
case *model.FunctionCallExpression:
switch x.Name {
case "readDir":
scopeName = fmt.Sprintf("fileNames%d", rs.count)
temp = &readDirTemp{
Name: fmt.Sprintf("files%d", rs.count),
Value: x,
}
rs.temps = append(rs.temps, temp)
rs.count++
default:
return x, nil
}
default:
return x, nil
}
return &model.ScopeTraversalExpression{
RootName: scopeName,
Traversal: hcl.Traversal{hcl.TraverseRoot{Name: ""}},
Parts: []model.Traversable{temp},
}, nil
}
func (g *generator) rewriteReadDir(
x model.Expression,
spiller *readDirSpiller,
) (model.Expression, []*readDirTemp, hcl.Diagnostics) {
spiller.temps = nil
x, diags := model.VisitExpression(x, spiller.spillExpression, nil)
return x, spiller.temps, diags
}