Skip to content

Commit

Permalink
feat: support provider defined functions (#1636)
Browse files Browse the repository at this point in the history
* feat: initial provider defined functions support work

* chore: update hashicorp/hcl, hcl-lang, and terraform-schema to unreleased versions

* fix: remove obsolete method call

* Bump hcl to 2.20.1

* Bump hcl-lang to `49d7378`

* Bump terraform-schema to `560df6b`

---------

Co-authored-by: Daniel Banck <daniel@dbanck.de>
  • Loading branch information
ansgarm and dbanck committed Mar 26, 2024
1 parent 1248104 commit 9acbaee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 32 deletions.
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ require (
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hc-install v0.6.3
github.com/hashicorp/hcl-lang v0.0.0-20240307084507-b482690b023d
github.com/hashicorp/hcl/v2 v2.19.1
github.com/hashicorp/hcl-lang v0.0.0-20240326153306-49d737897778
github.com/hashicorp/hcl/v2 v2.20.1
github.com/hashicorp/terraform-exec v0.20.0
github.com/hashicorp/terraform-json v0.21.0
github.com/hashicorp/terraform-registry-address v0.2.3
github.com/hashicorp/terraform-schema v0.0.0-20240212083923-1ed5c2075222
github.com/hashicorp/terraform-schema v0.0.0-20240326154150-560df6bbc4dc
github.com/mcuadros/go-defaults v1.2.0
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.5
Expand Down Expand Up @@ -72,7 +72,6 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/copier v0.3.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,18 @@ github.com/hashicorp/hc-install v0.6.3 h1:yE/r1yJvWbtrJ0STwScgEnCanb0U9v7zp0Gbkm
github.com/hashicorp/hc-install v0.6.3/go.mod h1:KamGdbodYzlufbWh4r9NRo8y6GLHWZP2GBtdnms1Ln0=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20240307084507-b482690b023d h1:Q7JcyMeEWWdUCM48ZwVE6pfnxUcrm7azB9a+Z7aIEbs=
github.com/hashicorp/hcl-lang v0.0.0-20240307084507-b482690b023d/go.mod h1:uCj8V85KJStf2MkhcA6WR1TIjDHwtswOwDCVsXPhPNI=
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/hashicorp/hcl-lang v0.0.0-20240326153306-49d737897778 h1:Yw/5Lno+YiOAEQQ6krRpiJ4lCv+x0k7Q6/SxH5AIsus=
github.com/hashicorp/hcl-lang v0.0.0-20240326153306-49d737897778/go.mod h1:2Vwxuf2nQi/095HYVqo0kWhlBLKhwEi/jTf5TxB61MI=
github.com/hashicorp/hcl/v2 v2.20.1 h1:M6hgdyz7HYt1UN9e61j+qKJBqR3orTWbI1HKBJEdxtc=
github.com/hashicorp/hcl/v2 v2.20.1/go.mod h1:TZDqQ4kNKCbh1iJp99FdPiUaVDDUPivbqxZulxDYqL4=
github.com/hashicorp/terraform-exec v0.20.0 h1:DIZnPsqzPGuUnq6cH8jWcPunBfY+C+M8JyYF3vpnuEo=
github.com/hashicorp/terraform-exec v0.20.0/go.mod h1:ckKGkJWbsNqFKV1itgMnE0hY9IYf1HoiekpuN0eWoDw=
github.com/hashicorp/terraform-json v0.21.0 h1:9NQxbLNqPbEMze+S6+YluEdXgJmhQykRyRNd+zTI05U=
github.com/hashicorp/terraform-json v0.21.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-schema v0.0.0-20240212083923-1ed5c2075222 h1:BE095S2PxCR65CB7XrfRBuc4uU1Lc6hW7Ht8qtf9KIg=
github.com/hashicorp/terraform-schema v0.0.0-20240212083923-1ed5c2075222/go.mod h1:ljuNMgZEQlUzS3HSdaVwkanVQDbYiNmA9UnGvQZqbio=
github.com/hashicorp/terraform-schema v0.0.0-20240326154150-560df6bbc4dc h1:d80NP3l4Y3rgtyljXeFUnoExfppUoqy2bk0fV6bBudA=
github.com/hashicorp/terraform-schema v0.0.0-20240326154150-560df6bbc4dc/go.mod h1:NuD9aPdZD2FFmT+56jkwDMAO/lX9qPPK7pkhB+umAaw=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo=
Expand Down Expand Up @@ -277,8 +277,6 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
6 changes: 5 additions & 1 deletion internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ func modulePathContext(mod *state.Module, schemaReader state.SchemaReader, modRe
if err != nil {
return nil, err
}
functions, err := functionsForModule(mod, schemaReader)
if err != nil {
return nil, err
}

pathCtx := &decoder.PathContext{
Schema: schema,
ReferenceOrigins: make(reference.Origins, 0),
ReferenceTargets: make(reference.Targets, 0),
Files: make(map[string]*hcl.File, 0),
Functions: coreFunctions(mod),
Functions: functions,
Validators: moduleValidators,
}

Expand Down
29 changes: 10 additions & 19 deletions internal/decoder/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,22 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-ls/internal/state"
tfmodule "github.com/hashicorp/terraform-schema/module"
tfschema "github.com/hashicorp/terraform-schema/schema"
)

func coreFunctions(mod *state.Module) map[string]schema.FunctionSignature {
if mod.TerraformVersion != nil {
s, err := tfschema.FunctionsForVersion(mod.TerraformVersion)
if err == nil {
return s
}
if mod.TerraformVersion.LessThan(tfschema.OldestAvailableVersion) {
return mustFunctionsForVersion(tfschema.OldestAvailableVersion)
}
func functionsForModule(mod *state.Module, schemaReader state.SchemaReader) (map[string]schema.FunctionSignature, error) {
resolvedVersion := tfschema.ResolveVersion(mod.TerraformVersion, mod.Meta.CoreRequirements)
sm := tfschema.NewFunctionsMerger(mustFunctionsForVersion(resolvedVersion))
sm.SetSchemaReader(schemaReader)

return mustFunctionsForVersion(tfschema.LatestAvailableVersion)
meta := &tfmodule.Meta{
Path: mod.Path,
ProviderRequirements: mod.Meta.ProviderRequirements,
ProviderReferences: mod.Meta.ProviderReferences,
}

s, err := tfschema.FunctionsForConstraint(mod.Meta.CoreRequirements)
if err == nil {
return s
}
if mod.Meta.CoreRequirements.Check(tfschema.OldestAvailableVersion) {
return mustFunctionsForVersion(tfschema.OldestAvailableVersion)
}

return mustFunctionsForVersion(tfschema.LatestAvailableVersion)
return sm.FunctionsForModule(meta)
}

func mustFunctionsForVersion(v *version.Version) map[string]schema.FunctionSignature {
Expand Down

0 comments on commit 9acbaee

Please sign in to comment.