Skip to content

Commit 480fdfd

Browse files
dbanckradeksimko
andauthored
Enable DocsLink for attributes (#115)
* 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 <[email protected]> Co-authored-by: Radek Simko <[email protected]>
1 parent f24c834 commit 480fdfd

File tree

3 files changed

+105
-10
lines changed

3 files changed

+105
-10
lines changed

decoder/decoder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func mergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*
7171
}
7272

7373
mergedSchema.Targets = depSchema.Targets.Copy()
74+
mergedSchema.DocsLink = depSchema.DocsLink.Copy()
7475
}
7576

7677
return mergedSchema, nil

decoder/links.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,24 @@ func (d *PathDecoder) linksInBody(body *hclsyntax.Body, bodySchema *schema.BodyS
4444
if block.Body != nil {
4545
depSchema, dk, ok := NewBlockSchema(blockSchema).DependentBodySchema(block.AsHCLBlock())
4646
if ok && depSchema.DocsLink != nil {
47+
link := depSchema.DocsLink
48+
u, err := d.docsURL(link.URL, "documentLink")
49+
if err != nil {
50+
continue
51+
}
4752
for _, labelDep := range dk.Labels {
48-
link := depSchema.DocsLink
49-
u, err := d.docsURL(link.URL, "documentLink")
50-
if err == nil {
51-
links = append(links, lang.Link{
52-
URI: u.String(),
53-
Tooltip: link.Tooltip,
54-
Range: block.LabelRanges[labelDep.Index],
55-
})
56-
}
53+
links = append(links, lang.Link{
54+
URI: u.String(),
55+
Tooltip: link.Tooltip,
56+
Range: block.LabelRanges[labelDep.Index],
57+
})
58+
}
59+
for _, attrDep := range dk.Attributes {
60+
links = append(links, lang.Link{
61+
URI: u.String(),
62+
Tooltip: link.Tooltip,
63+
Range: block.Body.Attributes[attrDep.Name].Expr.Range(),
64+
})
5765
}
5866
}
5967
}

decoder/links_test.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/zclconf/go-cty/cty"
1414
)
1515

16-
func TestLinksInFile(t *testing.T) {
16+
func TestLinksInFileBlock(t *testing.T) {
1717
resourceLabelSchema := []*schema.LabelSchema{
1818
{Name: "type", IsDepKey: true},
1919
{Name: "name"},
@@ -101,6 +101,91 @@ func TestLinksInFile(t *testing.T) {
101101
}
102102
}
103103

104+
func TestLinksInFileAttribute(t *testing.T) {
105+
resourceLabelSchema := []*schema.LabelSchema{
106+
{Name: "name"},
107+
}
108+
blockSchema := &schema.BlockSchema{
109+
Labels: resourceLabelSchema,
110+
Description: lang.Markdown("My special block"),
111+
Body: &schema.BodySchema{
112+
Attributes: map[string]*schema.AttributeSchema{
113+
"num_attr": {Expr: schema.LiteralTypeOnly(cty.Number)},
114+
"source": {
115+
Expr: schema.LiteralTypeOnly(cty.String),
116+
Description: lang.PlainText("Special attribute"),
117+
IsDepKey: true,
118+
},
119+
},
120+
},
121+
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
122+
schema.NewSchemaKey(schema.DependencyKeys{
123+
Attributes: []schema.AttributeDependent{
124+
{
125+
Name: "source",
126+
Expr: schema.ExpressionValue{
127+
Static: cty.StringVal("example.com/source"),
128+
},
129+
},
130+
},
131+
}): {
132+
DocsLink: &schema.DocsLink{URL: "https://example.com/some/source"},
133+
},
134+
},
135+
}
136+
bodySchema := &schema.BodySchema{
137+
Blocks: map[string]*schema.BlockSchema{
138+
"myblock": blockSchema,
139+
},
140+
}
141+
testConfig := []byte(`myblock "example" {
142+
source = "example.com/source"
143+
num_attr = 4
144+
}
145+
`)
146+
147+
f, pDiags := hclsyntax.ParseConfig(testConfig, "test.tf", hcl.InitialPos)
148+
if len(pDiags) > 0 {
149+
t.Fatal(pDiags)
150+
}
151+
152+
d := testPathDecoder(t, &PathContext{
153+
Schema: bodySchema,
154+
Files: map[string]*hcl.File{
155+
"test.tf": f,
156+
},
157+
})
158+
159+
links, err := d.LinksInFile("test.tf")
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
164+
expectedLinks := []lang.Link{
165+
{
166+
URI: "https://example.com/some/source",
167+
Range: hcl.Range{
168+
Filename: "test.tf",
169+
Start: hcl.Pos{
170+
Line: 2,
171+
Column: 12,
172+
Byte: 31,
173+
},
174+
End: hcl.Pos{
175+
Line: 2,
176+
Column: 32,
177+
Byte: 51,
178+
},
179+
},
180+
},
181+
}
182+
183+
diff := cmp.Diff(expectedLinks, links)
184+
if diff != "" {
185+
t.Fatalf("unexpected links: %s", diff)
186+
}
187+
}
188+
104189
func TestLinksInFile_json(t *testing.T) {
105190
f, pDiags := json.Parse([]byte(`{
106191
"customblock": {
@@ -117,6 +202,7 @@ func TestLinksInFile_json(t *testing.T) {
117202
},
118203
})
119204

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

0 commit comments

Comments
 (0)