Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SchemaMerger interface #335

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion module/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type Meta struct {
Path string
Filenames []string

CoreRequirements version.Constraints
Backend *Backend
Cloud *backend.Cloud
ProviderReferences map[ProviderRef]tfaddr.Provider
ProviderRequirements ProviderRequirements
CoreRequirements version.Constraints
Variables map[string]Variable
Outputs map[string]Output
ModuleCalls map[string]DeclaredModuleCall
Expand Down
30 changes: 16 additions & 14 deletions schema/functions_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type FunctionsMerger struct {
coreFunctions map[string]schema.FunctionSignature
terraformVersion *version.Version
schemaReader SchemaReader
stateReader StateReader
}

func NewFunctionsMerger(coreFunctions map[string]schema.FunctionSignature) *FunctionsMerger {
Expand All @@ -23,8 +23,8 @@ func NewFunctionsMerger(coreFunctions map[string]schema.FunctionSignature) *Func
}
}

func (m *FunctionsMerger) SetSchemaReader(sr SchemaReader) {
m.schemaReader = sr
func (m *FunctionsMerger) SetStateReader(mr StateReader) {
m.stateReader = mr
}

func (m *FunctionsMerger) SetTerraformVersion(v *version.Version) {
Expand All @@ -40,6 +40,10 @@ func (m *FunctionsMerger) FunctionsForModule(meta *tfmod.Meta) (map[string]schem
return m.coreFunctions, nil
}

if m.stateReader == nil {
return m.coreFunctions, nil
}

if m.terraformVersion.LessThan(v1_8) {
return m.coreFunctions, nil
}
Expand All @@ -51,19 +55,17 @@ func (m *FunctionsMerger) FunctionsForModule(meta *tfmod.Meta) (map[string]schem

providerRefs := ProviderReferences(meta.ProviderReferences)

if m.schemaReader != nil {
for pAddr, pVersionCons := range meta.ProviderRequirements {
pSchema, err := m.schemaReader.ProviderSchema(meta.Path, pAddr, pVersionCons)
if err != nil {
continue
}
for pAddr, pVersionCons := range meta.ProviderRequirements {
pSchema, err := m.stateReader.ProviderSchema(meta.Path, pAddr, pVersionCons)
if err != nil {
continue
}

refs := providerRefs.ReferencesOfProvider(pAddr)
refs := providerRefs.ReferencesOfProvider(pAddr)

for _, localRef := range refs {
for fName, fSig := range pSchema.Functions {
mergedFunctions[fmt.Sprintf("provider::%s::%s", localRef.LocalName, fName)] = *fSig.Copy()
}
for _, localRef := range refs {
for fName, fSig := range pSchema.Functions {
mergedFunctions[fmt.Sprintf("provider::%s::%s", localRef.LocalName, fName)] = *fSig.Copy()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions schema/functions_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestFunctionsMerger_FunctionsForModule_18(t *testing.T) {
}

fm := NewFunctionsMerger(coreFunctions)
fm.SetSchemaReader(&testJsonSchemaReader{
fm.SetStateReader(&testJsonSchemaReader{
ps: &tfjson.ProviderSchemas{
FormatVersion: "1.0",
Schemas: providerSchemaWithFunctions,
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestFunctionsMerger_FunctionsForModule_18(t *testing.T) {

func TestFunctionsMerger_FunctionsForModule_17(t *testing.T) {
fm := NewFunctionsMerger(map[string]schema.FunctionSignature{})
fm.SetSchemaReader(&testJsonSchemaReader{
fm.SetStateReader(&testJsonSchemaReader{
ps: &tfjson.ProviderSchemas{
FormatVersion: "1.0",
Schemas: providerSchemaWithFunctions,
Expand Down
Loading