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

Enable DocsLink for attributes #115

Merged
merged 4 commits into from
Apr 21, 2022
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
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