Skip to content

Commit

Permalink
Enable DocsLink for attributes (#115)
Browse files Browse the repository at this point in the history
* add links for dependecy key attribute expressions

* copy docs link on schema merge

* add test case for attribute links

* Update decoder/links_test.go

Co-authored-by: Radek Simko <radek.simko@gmail.com>

Co-authored-by: Radek Simko <radek.simko@gmail.com>
  • Loading branch information
dbanck and radeksimko authored Apr 21, 2022
1 parent f24c834 commit 480fdfd
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 10 deletions.
1 change: 1 addition & 0 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func mergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*
}

mergedSchema.Targets = depSchema.Targets.Copy()
mergedSchema.DocsLink = depSchema.DocsLink.Copy()
}

return mergedSchema, nil
Expand Down
26 changes: 17 additions & 9 deletions decoder/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,24 @@ func (d *PathDecoder) linksInBody(body *hclsyntax.Body, bodySchema *schema.BodyS
if block.Body != nil {
depSchema, dk, ok := NewBlockSchema(blockSchema).DependentBodySchema(block.AsHCLBlock())
if ok && depSchema.DocsLink != nil {
link := depSchema.DocsLink
u, err := d.docsURL(link.URL, "documentLink")
if err != nil {
continue
}
for _, labelDep := range dk.Labels {
link := depSchema.DocsLink
u, err := d.docsURL(link.URL, "documentLink")
if err == nil {
links = append(links, lang.Link{
URI: u.String(),
Tooltip: link.Tooltip,
Range: block.LabelRanges[labelDep.Index],
})
}
links = append(links, lang.Link{
URI: u.String(),
Tooltip: link.Tooltip,
Range: block.LabelRanges[labelDep.Index],
})
}
for _, attrDep := range dk.Attributes {
links = append(links, lang.Link{
URI: u.String(),
Tooltip: link.Tooltip,
Range: block.Body.Attributes[attrDep.Name].Expr.Range(),
})
}
}
}
Expand Down
88 changes: 87 additions & 1 deletion decoder/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/zclconf/go-cty/cty"
)

func TestLinksInFile(t *testing.T) {
func TestLinksInFileBlock(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "name"},
Expand Down Expand Up @@ -101,6 +101,91 @@ func TestLinksInFile(t *testing.T) {
}
}

func TestLinksInFileAttribute(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "name"},
}
blockSchema := &schema.BlockSchema{
Labels: resourceLabelSchema,
Description: lang.Markdown("My special block"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"num_attr": {Expr: schema.LiteralTypeOnly(cty.Number)},
"source": {
Expr: schema.LiteralTypeOnly(cty.String),
Description: lang.PlainText("Special attribute"),
IsDepKey: true,
},
},
},
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
schema.NewSchemaKey(schema.DependencyKeys{
Attributes: []schema.AttributeDependent{
{
Name: "source",
Expr: schema.ExpressionValue{
Static: cty.StringVal("example.com/source"),
},
},
},
}): {
DocsLink: &schema.DocsLink{URL: "https://example.com/some/source"},
},
},
}
bodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"myblock": blockSchema,
},
}
testConfig := []byte(`myblock "example" {
source = "example.com/source"
num_attr = 4
}
`)

f, pDiags := hclsyntax.ParseConfig(testConfig, "test.tf", hcl.InitialPos)
if len(pDiags) > 0 {
t.Fatal(pDiags)
}

d := testPathDecoder(t, &PathContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
})

links, err := d.LinksInFile("test.tf")
if err != nil {
t.Fatal(err)
}

expectedLinks := []lang.Link{
{
URI: "https://example.com/some/source",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 2,
Column: 12,
Byte: 31,
},
End: hcl.Pos{
Line: 2,
Column: 32,
Byte: 51,
},
},
},
}

diff := cmp.Diff(expectedLinks, links)
if diff != "" {
t.Fatalf("unexpected links: %s", diff)
}
}

func TestLinksInFile_json(t *testing.T) {
f, pDiags := json.Parse([]byte(`{
"customblock": {
Expand All @@ -117,6 +202,7 @@ func TestLinksInFile_json(t *testing.T) {
},
})

// We never want to provide links in JSON configs
_, err := d.LinksInFile("test.tf.json")
unknownFormatErr := &UnknownFileFormatError{}
if !errors.As(err, &unknownFormatErr) {
Expand Down

0 comments on commit 480fdfd

Please sign in to comment.