Skip to content

Commit

Permalink
Get module inputs for tfvars schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Apr 23, 2024
1 parent f09b92c commit c154cfa
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 31 deletions.
9 changes: 9 additions & 0 deletions internal/features/modules/modules_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,12 @@ func (f *ModulesFeature) CoreRequirements(modPath string) (version.Constraints,

return mod.Meta.CoreRequirements, nil
}

func (f *ModulesFeature) ModuleInputs(modPath string) (map[string]tfmod.Variable, error) {
mod, err := f.store.ModuleRecordByPath(modPath)
if err != nil {
return nil, err
}

return mod.Meta.Variables, nil
}
11 changes: 8 additions & 3 deletions internal/features/variables/decoder/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/features/variables/state"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
tfmod "github.com/hashicorp/terraform-schema/module"
)

type StateReader interface {
List() ([]*state.VariableRecord, error)
VariableRecordByPath(path string) (*state.VariableRecord, error)
}

type ModuleReader interface {
ModuleInputs(modPath string) (map[string]tfmod.Variable, error)
}

type PathReader struct {
StateReader StateReader
StateReader StateReader
ModuleReader ModuleReader
}

var _ decoder.PathReader = &PathReader{}
Expand All @@ -45,6 +51,5 @@ func (pr *PathReader) PathContext(path lang.Path) (*decoder.PathContext, error)
if err != nil {
return nil, err
}
return variablePathContext(mod, pr.StateReader)

return variablePathContext(mod, pr.ModuleReader)
}
11 changes: 2 additions & 9 deletions internal/features/variables/decoder/variable_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/features/variables/ast"
"github.com/hashicorp/terraform-ls/internal/features/variables/state"
tfmod "github.com/hashicorp/terraform-schema/module"
tfschema "github.com/hashicorp/terraform-schema/schema"
)

func variablePathContext(mod *state.VariableRecord, stateReader StateReader) (*decoder.PathContext, error) {
variables := make(map[string]tfmod.Variable)
// TODO: GET SCHEMA FROM MODULE
// meta, err := stateReader.LocalModuleMeta(mod.Path())
// if err == nil {
// variables = meta.Variables
// }

func variablePathContext(mod *state.VariableRecord, moduleReader ModuleReader) (*decoder.PathContext, error) {
variables, _ := moduleReader.ModuleInputs(mod.Path())
schema, err := tfschema.SchemaForVariables(variables, mod.Path())
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions internal/features/variables/jobs/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
//
// This is useful in hovering over those variable names,
// go-to-definition and go-to-references.
func DecodeVarsReferences(ctx context.Context, varStore *state.VariableStore, modPath string) error {
func DecodeVarsReferences(ctx context.Context, varStore *state.VariableStore, moduleFeature fdecoder.ModuleReader, modPath string) error {
mod, err := varStore.VariableRecordByPath(modPath)
if err != nil {
return err
Expand All @@ -43,7 +43,8 @@ func DecodeVarsReferences(ctx context.Context, varStore *state.VariableStore, mo
}

d := decoder.NewDecoder(&fdecoder.PathReader{
StateReader: varStore,
StateReader: varStore,
ModuleReader: moduleFeature,
})
d.SetContext(idecoder.DecoderContext(ctx))

Expand Down
5 changes: 3 additions & 2 deletions internal/features/variables/jobs/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
//
// It relies on previously parsed AST (via [ParseVariables])
// and schema, as provided via [LoadModuleMetadata]).
func SchemaVariablesValidation(ctx context.Context, varStore *state.VariableStore, modPath string) error {
func SchemaVariablesValidation(ctx context.Context, varStore *state.VariableStore, moduleFeature fdecoder.ModuleReader, modPath string) error {
mod, err := varStore.VariableRecordByPath(modPath)
if err != nil {
return err
Expand All @@ -45,7 +45,8 @@ func SchemaVariablesValidation(ctx context.Context, varStore *state.VariableStor
}

d := decoder.NewDecoder(&fdecoder.PathReader{
StateReader: varStore,
StateReader: varStore,
ModuleReader: moduleFeature,
})
d.SetContext(idecoder.DecoderContext(ctx))

Expand Down
25 changes: 14 additions & 11 deletions internal/features/variables/variables_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ type VariablesFeature struct {
stopFunc context.CancelFunc
logger *log.Logger

jobStore *globalState.JobStore
fs jobs.ReadOnlyFS
moduleFeature fdecoder.ModuleReader
jobStore *globalState.JobStore
fs jobs.ReadOnlyFS
}

func NewVariablesFeature(eventbus *eventbus.EventBus, jobStore *globalState.JobStore, fs jobs.ReadOnlyFS) (*VariablesFeature, error) {
func NewVariablesFeature(eventbus *eventbus.EventBus, jobStore *globalState.JobStore, fs jobs.ReadOnlyFS, moduleFeature fdecoder.ModuleReader) (*VariablesFeature, error) {
store, err := state.NewVariableStore()
if err != nil {
return nil, err
}
discardLogger := log.New(io.Discard, "", 0)

return &VariablesFeature{
store: store,
eventbus: eventbus,
stopFunc: func() {},
logger: discardLogger,
jobStore: jobStore,
fs: fs,
store: store,
eventbus: eventbus,
stopFunc: func() {},
logger: discardLogger,
moduleFeature: moduleFeature,
jobStore: jobStore,
fs: fs,
}, nil
}

Expand Down Expand Up @@ -113,7 +115,7 @@ func (f *VariablesFeature) DidOpen(ctx context.Context, dir document.DirHandle,
varsRefsId, err := f.jobStore.EnqueueJob(ctx, job.Job{
Dir: dir,
Func: func(ctx context.Context) error {
return jobs.DecodeVarsReferences(ctx, f.store, path)
return jobs.DecodeVarsReferences(ctx, f.store, f.moduleFeature, path)
},
Type: op.OpTypeDecodeVarsReferences.String(),
DependsOn: job.IDs{parseVarsId},
Expand All @@ -128,7 +130,8 @@ func (f *VariablesFeature) DidOpen(ctx context.Context, dir document.DirHandle,

func (f *VariablesFeature) PathContext(path lang.Path) (*decoder.PathContext, error) {
pathReader := &fdecoder.PathReader{
StateReader: f.store,
StateReader: f.store,
ModuleReader: f.moduleFeature,
}

return pathReader.PathContext(path)
Expand Down
8 changes: 4 additions & 4 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,16 @@ func (svc *service) configureSessionDependencies(ctx context.Context, cfgOpts *s
rootModulesFeature.SetLogger(svc.logger)
rootModulesFeature.Start(svc.sessCtx)

modulesFeature, err := fmodules.NewModulesFeature(svc.eventBus,
svc.stateStore.JobStore, svc.stateStore.ProviderSchemas, svc.stateStore.RegistryModules,
svc.fs, rootModulesFeature)
modulesFeature, err := fmodules.NewModulesFeature(svc.eventBus, svc.stateStore.JobStore,
svc.stateStore.ProviderSchemas, svc.stateStore.RegistryModules, svc.fs, rootModulesFeature)
if err != nil {
return err
}
modulesFeature.SetLogger(svc.logger)
modulesFeature.Start(svc.sessCtx)

variablesFeature, err := fvariables.NewVariablesFeature(svc.eventBus, svc.stateStore.JobStore, svc.fs)
variablesFeature, err := fvariables.NewVariablesFeature(svc.eventBus, svc.stateStore.JobStore,
svc.fs, modulesFeature)
if err != nil {
return err
}
Expand Down

0 comments on commit c154cfa

Please sign in to comment.