Skip to content

Commit

Permalink
Merge pull request #532 from macoto1995/feat/refer-viewpoints-from-ta…
Browse files Browse the repository at this point in the history
…bles

feat:refer viewpoints from tables
  • Loading branch information
k1LoW authored Oct 27, 2023
2 parents 5059fd9 + 693d890 commit 2a2e7a9
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
run:
timeout: 5m
linters:
fast: false
enable:
Expand All @@ -7,7 +9,7 @@ linters-settings:
errcheck:
check-type-assertions: true
staticcheck:
go: 1.16
go: "1.21"
misspell:
locale: US
ignore-words: []
Expand Down
30 changes: 30 additions & 0 deletions output/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (m *Md) OutputViewpoint(wr io.Writer, i int, v *schema.Viewpoint) error {

// Output generate markdown files.
func Output(s *schema.Schema, c *config.Config, force bool) (e error) {
s, err := s.SetViewpointsToTables()
if err != nil {
return errors.WithStack(err)
}

docPath := c.DocPath

fullPath, err := filepath.Abs(docPath)
Expand Down Expand Up @@ -533,6 +538,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
number := m.config.Format.Number
adjust := m.config.Format.Adjust
hideColumns := m.config.Format.HideColumnsWithoutValues
showOnlyFirstParagraph := m.config.Format.ShowOnlyFirstParagraph

// Columns
columnsData := [][]string{}
Expand Down Expand Up @@ -588,6 +594,28 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
columnsData = append(columnsData, data)
}

// Viewpoints
viewpointsData := [][]string{
[]string{
m.config.MergedDict.Lookup("Name"),
m.config.MergedDict.Lookup("Definition"),
},
[]string{"----", "----------"},
}

for _, v := range t.Viewpoints {
desc := v.Desc
if showOnlyFirstParagraph {
desc = output.ShowOnlyFirstParagraph(desc)
}
data := []string{
fmt.Sprintf("[%s](viewpoint-%d.md)", v.Name, v.Index),
desc,
}

viewpointsData = append(viewpointsData, data)
}

// Constraints
constraintsData := [][]string{
[]string{
Expand Down Expand Up @@ -698,6 +726,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
return map[string]interface{}{
"Table": t,
"Columns": adjustTable(columnsData),
"Viewpoints": adjustTable(viewpointsData),
"Constraints": adjustTable(constraintsData),
"Indexes": adjustTable(indexesData),
"Triggers": adjustTable(triggersData),
Expand All @@ -708,6 +737,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
return map[string]interface{}{
"Table": t,
"Columns": columnsData,
"Viewpoints": viewpointsData,
"Constraints": constraintsData,
"Indexes": indexesData,
"Triggers": triggersData,
Expand Down
6 changes: 6 additions & 0 deletions output/md/md_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package md
import (
"os"
"path/filepath"
"slices"
"testing"

"github.com/k1LoW/tbls/config"
Expand Down Expand Up @@ -62,6 +63,11 @@ func TestOutput(t *testing.T) {
t.Fatal(err)
}
tb.Name = tt.tableBName
for _, v := range s.Viewpoints {
if vti := slices.Index(v.Tables, "b"); vti != -1 {
v.Tables[vti] = tt.tableBName
}
}
c, err := config.New()
if err != nil {
t.Error(err)
Expand Down
9 changes: 8 additions & 1 deletion output/md/templates/table.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
{{- end }}

{{ $len := len .Constraints }}{{ if ne $len 2 -}}
{{ $len := len .Viewpoints }}{{ if ne $len 2 -}}
## {{ "Viewpoints" | lookup }}
{{ range $l := .Viewpoints }}
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
{{- end }}

{{ end -}}
{{ $len := len .Constraints -}}{{ if ne $len 2 -}}
## {{ "Constraints" | lookup }}
{{ range $l := .Constraints }}
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
Expand Down
47 changes: 36 additions & 11 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,26 @@ type Column struct {
HideForER bool `json:"-"`
}

type TableViewpoint struct {
Index int `json:"index"`
Name string `json:"name"`
Desc string `json:"desc"`
}

// Table is the struct for database table
type Table struct {
Name string `json:"name"`
Type string `json:"type"`
Comment string `json:"comment"`
Columns []*Column `json:"columns"`
Indexes []*Index `json:"indexes"`
Constraints []*Constraint `json:"constraints"`
Triggers []*Trigger `json:"triggers"`
Def string `json:"def"`
Labels Labels `json:"labels,omitempty"`
ReferencedTables []*Table `json:"referenced_tables,omitempty" yaml:"referencedTables,omitempty"`
External bool `json:"-"` // Table external to the schema
Name string `json:"name"`
Type string `json:"type"`
Comment string `json:"comment"`
Columns []*Column `json:"columns"`
Viewpoints []*TableViewpoint `json:"viewpoints"`
Indexes []*Index `json:"indexes"`
Constraints []*Constraint `json:"constraints"`
Triggers []*Trigger `json:"triggers"`
Def string `json:"def"`
Labels Labels `json:"labels,omitempty"`
ReferencedTables []*Table `json:"referenced_tables,omitempty" yaml:"referencedTables,omitempty"`
External bool `json:"-"` // Table external to the schema
}

// Relation is the struct for table relation
Expand Down Expand Up @@ -192,6 +199,24 @@ type Schema struct {
Viewpoints Viewpoints `json:"viewpoints,omitempty"`
}

func (s *Schema) SetViewpointsToTables() (*Schema, error) {
for vi, v := range s.Viewpoints {
// Add viewpoints to table
for _, t := range v.Tables {
table, err := s.FindTableByName(t)
if err != nil {
return s, err
}
table.Viewpoints = append(table.Viewpoints, &TableViewpoint{
Index: vi,
Name: v.Name,
Desc: v.Desc,
})
}
}
return s, nil
}

func (s *Schema) NormalizeTableName(name string) string {
if s.Driver != nil && s.Driver.Meta != nil && s.Driver.Meta.CurrentSchema != "" && (s.Driver.Name == "postgres" || s.Driver.Name == "redshift") && !strings.Contains(name, ".") {
return fmt.Sprintf("%s.%s", s.Driver.Meta.CurrentSchema, name)
Expand Down
52 changes: 50 additions & 2 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,54 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestSetViewpointsToTables(t *testing.T) {
viewpointAName := "va"
viewpointBName := "vb"

tests := []struct {
viewpointATables []string
viewpointBTables []string
wantTableAViewpoints []*TableViewpoint
}{
{[]string{"a"}, []string{"b"}, []*TableViewpoint{{Name: viewpointAName}}},
{[]string{"a", "b"}, []string{"a"}, []*TableViewpoint{{
Index: 0,
Name: viewpointAName,
}, {
Index: 1,
Name: viewpointBName,
}}},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%v", tt.viewpointATables), func(t *testing.T) {
fmt.Println(tt.viewpointATables)
s := newTestSchema(t)
s.Viewpoints = []*Viewpoint{
{
Name: viewpointAName,
Tables: tt.viewpointATables,
},
{
Name: viewpointBName,
Tables: tt.viewpointBTables,
},
}
result, err := s.SetViewpointsToTables()
if err != nil {
t.Error(err)
}
gotTable, _ := result.FindTableByName("a")
got := gotTable.Viewpoints
want := tt.wantTableAViewpoints

if diff := cmp.Diff(got, want, nil); diff != "" {
t.Errorf("%s", diff)
}
})
}
}

func TestNormalizeTableName(t *testing.T) {
tests := []struct {
s *Schema
Expand Down Expand Up @@ -212,11 +260,11 @@ func TestSchema_Sort(t *testing.T) {
},
Functions: []*Function{
&Function{
Name: "b",
Name: "b",
Arguments: "arg b",
},
&Function{
Name: "b",
Name: "b",
Arguments: "arg a",
},
},
Expand Down
7 changes: 7 additions & 0 deletions testdata/md_template_test_a.md.golden
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ THIS IS TABLE A
| a | INTEGER | | false | [b](b.md) | | COLUMN A |
| a2 | TEXT | | false | | | column a2 |

## Viewpoints

| Name | Definition |
| ---- | ---------- |
| [table a b](viewpoint-0.md) | select table a and b |
| [table a label red](viewpoint-3.md) | select table a and label red<br><br>- table a<br>- label red |

## Constraints

| Name | Type | Definition | Comment |
Expand Down
7 changes: 7 additions & 0 deletions testdata/md_test_a.md.first_para.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ TABLE A
| a | INTEGER | | false | [b](b.md) | | COLUMN A |
| a2 | TEXT | | false | | | column a2 |

## Viewpoints

| Name | Definition |
| ---- | ---------- |
| [table a b](viewpoint-0.md) | select table a and b |
| [table a label red](viewpoint-3.md) | select table a and label red |

## Constraints

| Name | Type | Definition | Comment |
Expand Down
7 changes: 7 additions & 0 deletions testdata/md_test_a.md.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ TABLE A
| a | INTEGER | | false | [b](b.md) | | COLUMN A |
| a2 | TEXT | | false | | | column a2 |

## Viewpoints

| Name | Definition |
| ---- | ---------- |
| [table a b](viewpoint-0.md) | select table a and b |
| [table a label red](viewpoint-3.md) | select table a and label red<br><br>- table a<br>- label red |

## Constraints

| Name | Type | Definition | Comment |
Expand Down
7 changes: 7 additions & 0 deletions testdata/md_test_a.md.mermaid.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ TABLE A
| a | INTEGER | | false | [b](b.md) | | COLUMN A |
| a2 | TEXT | | false | | | column a2 |

## Viewpoints

| Name | Definition |
| ---- | ---------- |
| [table a b](viewpoint-0.md) | select table a and b |
| [table a label red](viewpoint-3.md) | select table a and label red<br><br>- table a<br>- label red |

## Constraints

| Name | Type | Definition | Comment |
Expand Down
9 changes: 8 additions & 1 deletion testdata/templates/table.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
{{- end }}

{{ $len := len .Constraints }}{{ if ne $len 2 -}}
{{ $len := len .Viewpoints }}{{ if ne $len 2 -}}
## {{ "Viewpoints" | lookup }}
{{ range $l := .Viewpoints }}
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
{{- end }}

{{ end -}}
{{ $len := len .Constraints -}}{{ if ne $len 2 -}}
## {{ "Constraints" | lookup }}
{{ range $l := .Constraints }}
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
Expand Down

0 comments on commit 2a2e7a9

Please sign in to comment.