From 4235f6dd1ebceae67ddcd1a9433c7246ec24b268 Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 09:20:37 +0900 Subject: [PATCH 1/8] feat:func SetViewpointsToTables --- schema/schema.go | 47 +++++++++++++++++++++++++++++--------- schema/schema_test.go | 52 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index 6097e76d..7f9a352b 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -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 @@ -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) diff --git a/schema/schema_test.go b/schema/schema_test.go index 6678e6df..bc023f36 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -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 @@ -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", }, }, From af1e90ba68fc83acc28134b4ea9bfd31aa19c0b3 Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 09:21:05 +0900 Subject: [PATCH 2/8] viewpoints data of tables in md --- output/md/md.go | 25 +++++++++++++++++++++++++ testdata/templates/table.md.tmpl | 9 ++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/output/md/md.go b/output/md/md.go index 362dcfd7..c4a25489 100644 --- a/output/md/md.go +++ b/output/md/md.go @@ -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) @@ -588,6 +593,24 @@ 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 { + data := []string{ + fmt.Sprintf("[%s](viewpoint-%d.md)", v.Name, v.Index), + v.Desc, + } + + viewpointsData = append(viewpointsData, data) + } + // Constraints constraintsData := [][]string{ []string{ @@ -698,6 +721,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), @@ -708,6 +732,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, diff --git a/testdata/templates/table.md.tmpl b/testdata/templates/table.md.tmpl index 36291399..e54c4ca6 100644 --- a/testdata/templates/table.md.tmpl +++ b/testdata/templates/table.md.tmpl @@ -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 }} From 208035da6cb4407449cb2c20d7632ecfed4177bd Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 09:32:52 +0900 Subject: [PATCH 3/8] update default template --- output/md/templates/table.md.tmpl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/output/md/templates/table.md.tmpl b/output/md/templates/table.md.tmpl index 25be0ebc..1fd95de1 100644 --- a/output/md/templates/table.md.tmpl +++ b/output/md/templates/table.md.tmpl @@ -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 }} From 8d26480fea85cadff3f6866373ff761419f1316c Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 09:33:00 +0900 Subject: [PATCH 4/8] golden file --- testdata/md_template_test_a.md.golden | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/testdata/md_template_test_a.md.golden b/testdata/md_template_test_a.md.golden index 63a8fe1c..10f668f9 100644 --- a/testdata/md_template_test_a.md.golden +++ b/testdata/md_template_test_a.md.golden @@ -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

- table a
- label red | + ## Constraints | Name | Type | Definition | Comment | From adc687d538f635a7e9147ef848a3719f73a2c34e Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 10:55:56 +0900 Subject: [PATCH 5/8] golangci-lint timeout 5m --- .golangci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 007af930..ff919bbe 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,3 +1,5 @@ +run: + timeout: 5m linters: fast: false enable: @@ -7,7 +9,7 @@ linters-settings: errcheck: check-type-assertions: true staticcheck: - go: 1.16 + go: "1.21" misspell: locale: US ignore-words: [] From fec8d6853757895f7b255ca2ace778676100ac1f Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 11:11:19 +0900 Subject: [PATCH 6/8] golden files --- testdata/md_test_a.md.first_para.golden | 7 +++++++ testdata/md_test_a.md.golden | 7 +++++++ testdata/md_test_a.md.mermaid.golden | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/testdata/md_test_a.md.first_para.golden b/testdata/md_test_a.md.first_para.golden index 84d6a073..2abb701c 100644 --- a/testdata/md_test_a.md.first_para.golden +++ b/testdata/md_test_a.md.first_para.golden @@ -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

- table a
- label red | + ## Constraints | Name | Type | Definition | Comment | diff --git a/testdata/md_test_a.md.golden b/testdata/md_test_a.md.golden index f86eb5f5..5696e690 100644 --- a/testdata/md_test_a.md.golden +++ b/testdata/md_test_a.md.golden @@ -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

- table a
- label red | + ## Constraints | Name | Type | Definition | Comment | diff --git a/testdata/md_test_a.md.mermaid.golden b/testdata/md_test_a.md.mermaid.golden index 30eae61d..453fc53f 100644 --- a/testdata/md_test_a.md.mermaid.golden +++ b/testdata/md_test_a.md.mermaid.golden @@ -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

- table a
- label red | + ## Constraints | Name | Type | Definition | Comment | From 730b29dedfdccee13c3c6f3ffae92c2bebf168a6 Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 16:19:55 +0900 Subject: [PATCH 7/8] fix: test --- output/md/md_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/output/md/md_test.go b/output/md/md_test.go index e58c38b9..2030c0e9 100644 --- a/output/md/md_test.go +++ b/output/md/md_test.go @@ -3,6 +3,7 @@ package md import ( "os" "path/filepath" + "slices" "testing" "github.com/k1LoW/tbls/config" @@ -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) From 693d890545dbeaaa261260b976d6d298ef6e3af7 Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 17:22:57 +0900 Subject: [PATCH 8/8] showOnlyFirstParagraph --- output/md/md.go | 7 ++++++- testdata/md_test_a.md.first_para.golden | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/output/md/md.go b/output/md/md.go index c4a25489..3d5b09ce 100644 --- a/output/md/md.go +++ b/output/md/md.go @@ -538,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{} @@ -603,9 +604,13 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} { } 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), - v.Desc, + desc, } viewpointsData = append(viewpointsData, data) diff --git a/testdata/md_test_a.md.first_para.golden b/testdata/md_test_a.md.first_para.golden index 2abb701c..a680945b 100644 --- a/testdata/md_test_a.md.first_para.golden +++ b/testdata/md_test_a.md.first_para.golden @@ -20,7 +20,7 @@ TABLE A | 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

- table a
- label red | +| [table a label red](viewpoint-3.md) | select table a and label red | ## Constraints