Files
opentf/internal/modules/input_variable.go
Martin Atkins 4397d5bb72 modules: Start of decoding the declarations in a module
For now this is only for input variables, and only far enough to get their
names and detect duplicates. More to come in future commits.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-08-01 12:15:50 -07:00

42 lines
1.0 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package modules
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/opentofu/opentofu/internal/tfdiags"
)
type InputVariable struct {
Name string
DeclRange tfdiags.SourceRange
}
func decodeInputVariableBlock(block *hcl.Block) (*InputVariable, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
ret := &InputVariable{
Name: block.Labels[0],
DeclRange: tfdiags.SourceRangeFromHCL(block.DefRange),
}
if !hclsyntax.ValidIdentifier(ret.Name) {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid name for input variable",
Detail: fmt.Sprintf("Cannot use %q as the name of an input variable. Name must contain only letters, digits, underscores, and dashes.", ret.Name),
Subject: block.LabelRanges[0].Ptr(),
})
}
// TODO: Shallowly decode the arguments inside the block
return ret, diags
}