Skip to content

Commit af77dd7

Browse files
authored
feat: Add information about relations to generated docs (#142)
This adds a `Relations` section to generated docs, with information about parent- and child relationships. For example, a parent table might look like this: ``` ## Relations The following tables depend on `aws_accessanalyzer_analyzers`: - [`aws_accessanalyzer_analyzer_findings`](aws_accessanalyzer_analyzer_findings.md) - [`aws_accessanalyzer_analyzer_archive_rules`](aws_accessanalyzer_analyzer_archive_rules.md) ``` And a child table will look like this: ``` ## Relations This table depends on [`aws_accessanalyzer_analyzers`](aws_accessanalyzer_analyzers.md). ```
1 parent c384ea0 commit af77dd7

File tree

7 files changed

+61
-37
lines changed

7 files changed

+61
-37
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
21
# Source Plugin: test
32
## Tables
43
| Name | Description |
54
| ------------- | ------------- |
65
|test_table|Description for test table|
7-

plugins/.snapshots/TestGenerateSourcePluginDocs-relation_table.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
21
# Table: relation_table
32
Description for relational table
43

54
The primary key for this table is **_cq_id**.
65

6+
## Relations
7+
This table depends on [`test_table`](test_table.md).
8+
79
## Columns
810
| Name | Type |
911
| ------------- | ------------- |
1012
|string_col|String|
1113
|_cq_id (PK)|UUID|
1214
|_cq_fetch_time|Timestamp|
13-

plugins/.snapshots/TestGenerateSourcePluginDocs-test_table.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
21
# Table: test_table
32
Description for test table
43

54
The composite primary key for this table is (**id_col**, **id_col2**).
65

6+
## Relations
7+
The following tables depend on `test_table`:
8+
- [`relation_table`](relation_table.md)
9+
710
## Columns
811
| Name | Type |
912
| ------------- | ------------- |
@@ -12,4 +15,3 @@ The composite primary key for this table is (**id_col**, **id_col2**).
1215
|id_col2 (PK)|Int|
1316
|_cq_id|UUID|
1417
|_cq_fetch_time|Timestamp|
15-

plugins/source.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ func addInternalColumns(tables []*schema.Table) {
6363
}
6464
}
6565

66+
// Set parent links on relational tables
67+
func setParents(tables []*schema.Table) {
68+
for _, table := range tables {
69+
for i := range table.Relations {
70+
table.Relations[i].Parent = table
71+
}
72+
}
73+
}
74+
6675
// NewSourcePlugin returns a new plugin with a given name, version, tables, newExecutionClient
6776
// and additional options.
6877
func NewSourcePlugin(name string, version string, tables []*schema.Table, newExecutionClient SourceNewExecutionClientFunc, opts ...SourceOption) *SourcePlugin {
@@ -76,6 +85,7 @@ func NewSourcePlugin(name string, version string, tables []*schema.Table, newExe
7685
opt(&p)
7786
}
7887
addInternalColumns(p.tables)
88+
setParents(p.tables)
7989
if err := p.validate(); err != nil {
8090
panic(err)
8191
}

plugins/source_docs.go

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package plugins
22

33
import (
4+
"embed"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -10,35 +11,8 @@ import (
1011
"github.com/cloudquery/plugin-sdk/schema"
1112
)
1213

13-
const tableTmpl = `
14-
# Table: {{.Name}}
15-
{{ $.Description }}
16-
{{ $length := len $.PrimaryKeys -}}
17-
{{ if eq $length 1 }}
18-
The primary key for this table is **{{ index $.PrimaryKeys 0 }}**.
19-
{{ else }}
20-
The composite primary key for this table is ({{ range $index, $pk := $.PrimaryKeys -}}
21-
{{if $index }}, {{end -}}
22-
**{{$pk}}**
23-
{{- end -}}).
24-
{{ end }}
25-
## Columns
26-
| Name | Type |
27-
| ------------- | ------------- |
28-
{{- range $column := $.Columns }}
29-
|{{$column.Name}}{{if $column.CreationOptions.PrimaryKey}} (PK){{end}}|{{$column.Type | formatType}}|
30-
{{- end }}
31-
`
32-
33-
const allTablesTpml = `
34-
# Source Plugin: {{.Name}}
35-
## Tables
36-
| Name | Description |
37-
| ------------- | ------------- |
38-
{{- range $table := $.Tables }}
39-
|{{$table.Name}}|{{$table.Description }}|
40-
{{- end }}
41-
`
14+
//go:embed templates/*.go.tpl
15+
var templatesFS embed.FS
4216

4317
// GenerateSourcePluginDocs creates table documentation for the source plugin based on its list of tables
4418
func (p *SourcePlugin) GenerateSourcePluginDocs(dir string) error {
@@ -48,7 +22,7 @@ func (p *SourcePlugin) GenerateSourcePluginDocs(dir string) error {
4822
return err
4923
}
5024
}
51-
t, err := template.New("").Parse(allTablesTpml)
25+
t, err := template.New("all_tables.go.tpl").ParseFS(templatesFS, "templates/all_tables.go.tpl")
5226
if err != nil {
5327
return fmt.Errorf("failed to parse template for README.md: %v", err)
5428
}
@@ -84,7 +58,7 @@ func renderTable(table *schema.Table, dir string) error {
8458
return strings.ReplaceAll(text, "\n", " ")
8559
},
8660
})
87-
t, err := t.New("").Parse(tableTmpl)
61+
t, err := t.New("table.go.tpl").ParseFS(templatesFS, "templates/table.go.tpl")
8862
if err != nil {
8963
return fmt.Errorf("failed to parse template: %v", err)
9064
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Source Plugin: {{.Name}}
2+
## Tables
3+
| Name | Description |
4+
| ------------- | ------------- |
5+
{{- range $table := $.Tables }}
6+
|{{$table.Name}}|{{$table.Description }}|
7+
{{- end }}

plugins/templates/table.go.tpl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Table: {{.Name}}
2+
{{ $.Description }}
3+
{{ $length := len $.PrimaryKeys -}}
4+
{{ if eq $length 1 }}
5+
The primary key for this table is **{{ index $.PrimaryKeys 0 }}**.
6+
{{ else }}
7+
The composite primary key for this table is ({{ range $index, $pk := $.PrimaryKeys -}}
8+
{{if $index }}, {{end -}}
9+
**{{$pk}}**
10+
{{- end -}}).
11+
{{ end }}
12+
13+
{{- if or ($.Relations) ($.Parent) }}
14+
## Relations
15+
{{- end }}
16+
{{- if $.Parent }}
17+
This table depends on [`{{ $.Parent.Name }}`]({{ $.Parent.Name }}.md).
18+
{{- end}}
19+
20+
{{- if $.Relations }}
21+
The following tables depend on `{{.Name}}`:
22+
{{- range $rel := $.Relations }}
23+
- [`{{ $rel.Name }}`]({{ $rel.Name }}.md)
24+
{{- end }}
25+
{{- end }}
26+
27+
## Columns
28+
| Name | Type |
29+
| ------------- | ------------- |
30+
{{- range $column := $.Columns }}
31+
|{{$column.Name}}{{if $column.CreationOptions.PrimaryKey}} (PK){{end}}|{{$column.Type | formatType}}|
32+
{{- end }}

0 commit comments

Comments
 (0)