From 3baa6e28eb139ac1db81ed6d7197a4ff6e390b2c Mon Sep 17 00:00:00 2001 From: Makoto Date: Sun, 22 Oct 2023 16:19:40 +0900 Subject: [PATCH] feat:refer viewpoints from tables --- output/config/config.go | 2 +- output/dot/dot.go | 2 +- output/dot/dot_test.go | 4 +-- output/gviz/gviz.go | 6 ++-- output/gviz/gviz_test.go | 2 +- output/json/json.go | 2 +- output/md/md.go | 47 ++++++++++++++++++++----- output/md/templates/table.md.tmpl | 7 ++++ output/mermaid/mermaid.go | 2 +- output/mermaid/mermaid_test.go | 4 +-- output/output.go | 2 +- output/plantuml/plantuml.go | 2 +- output/plantuml/plantuml_test.go | 4 +-- output/xlsx/xlsx.go | 2 +- output/yaml/yaml.go | 2 +- testdata/md_test_a.md.first_para.golden | 7 ++++ testdata/md_test_a.md.golden | 7 ++++ testdata/md_test_a.md.mermaid.golden | 7 ++++ 18 files changed, 84 insertions(+), 27 deletions(-) diff --git a/output/config/config.go b/output/config/config.go index d94bd6c4..037b9579 100644 --- a/output/config/config.go +++ b/output/config/config.go @@ -95,6 +95,6 @@ func (c *Config) OutputSchema(wr io.Writer, s *schema.Schema) error { return nil } -func (c *Config) OutputTable(wr io.Writer, t *schema.Table) error { +func (c *Config) OutputTable(wr io.Writer, t *schema.Table, _v schema.Viewpoints) error { return errors.New("not supported") } diff --git a/output/dot/dot.go b/output/dot/dot.go index bebe5b58..f82d6ec1 100644 --- a/output/dot/dot.go +++ b/output/dot/dot.go @@ -58,7 +58,7 @@ func (d *Dot) OutputSchema(wr io.Writer, s *schema.Schema) error { } // OutputTable output dot format for table. -func (d *Dot) OutputTable(wr io.Writer, t *schema.Table) error { +func (d *Dot) OutputTable(wr io.Writer, t *schema.Table, _v schema.Viewpoints) error { tables, relations, err := t.CollectTablesAndRelations(*d.config.ER.Distance, true) if err != nil { return errors.WithStack(err) diff --git a/output/dot/dot_test.go b/output/dot/dot_test.go index cdb36a2b..b2f32c6d 100644 --- a/output/dot/dot_test.go +++ b/output/dot/dot_test.go @@ -115,7 +115,7 @@ func TestOutputTable(t *testing.T) { o := New(c) got := &bytes.Buffer{} - if err := o.OutputTable(got, ta); err != nil { + if err := o.OutputTable(got, ta, s.Viewpoints); err != nil { t.Error(err) } if os.Getenv("UPDATE_GOLDEN") != "" { @@ -154,7 +154,7 @@ func TestOutputTableTemplate(t *testing.T) { o := New(c) got := &bytes.Buffer{} - if err := o.OutputTable(got, ta); err != nil { + if err := o.OutputTable(got, ta, s.Viewpoints); err != nil { t.Error(err) } if os.Getenv("UPDATE_GOLDEN") != "" { diff --git a/output/gviz/gviz.go b/output/gviz/gviz.go index eff8996e..dfc8c204 100644 --- a/output/gviz/gviz.go +++ b/output/gviz/gviz.go @@ -44,9 +44,9 @@ func (g *Gviz) OutputSchema(wr io.Writer, s *schema.Schema) error { } // OutputTable generage image for table. -func (g *Gviz) OutputTable(wr io.Writer, t *schema.Table) error { +func (g *Gviz) OutputTable(wr io.Writer, t *schema.Table, v schema.Viewpoints) error { buf := &bytes.Buffer{} - if err := g.dot.OutputTable(buf, t); err != nil { + if err := g.dot.OutputTable(buf, t, v); err != nil { return errors.WithStack(err) } return g.render(wr, buf.Bytes()) @@ -127,7 +127,7 @@ func Output(s *schema.Schema, c *config.Config, force bool) (e error) { if err != nil { return errors.WithStack(err) } - if err := g.OutputTable(f, t); err != nil { + if err := g.OutputTable(f, t, s.Viewpoints); err != nil { return errors.WithStack(err) } } diff --git a/output/gviz/gviz_test.go b/output/gviz/gviz_test.go index f16745cf..26fa7ea5 100644 --- a/output/gviz/gviz_test.go +++ b/output/gviz/gviz_test.go @@ -79,7 +79,7 @@ func TestOutputTable(t *testing.T) { o := New(c) got := &bytes.Buffer{} - if err := o.OutputTable(got, ta); err != nil { + if err := o.OutputTable(got, ta, s.Viewpoints); err != nil { t.Error(err) } if os.Getenv("UPDATE_GOLDEN") != "" { diff --git a/output/json/json.go b/output/json/json.go index 7b919736..fedb4fa1 100644 --- a/output/json/json.go +++ b/output/json/json.go @@ -32,7 +32,7 @@ func (j *JSON) OutputSchema(wr io.Writer, s *schema.Schema) error { } // OutputTable output JSON format for table. -func (j *JSON) OutputTable(wr io.Writer, t *schema.Table) error { +func (j *JSON) OutputTable(wr io.Writer, t *schema.Table, _v schema.Viewpoints) error { encoder := json.NewEncoder(wr) if !j.inline { encoder.SetIndent("", " ") diff --git a/output/md/md.go b/output/md/md.go index 362dcfd7..e692249f 100644 --- a/output/md/md.go +++ b/output/md/md.go @@ -71,19 +71,19 @@ func (m *Md) OutputSchema(wr io.Writer, s *schema.Schema) error { } // OutputTable output md format for table. -func (m *Md) OutputTable(wr io.Writer, t *schema.Table) error { +func (m *Md) OutputTable(wr io.Writer, t *schema.Table, viewpoints schema.Viewpoints) error { ts, err := m.tableTemplate() if err != nil { return errors.WithStack(err) } tmpl := template.Must(template.New(t.Name).Funcs(output.Funcs(&m.config.MergedDict)).Parse(ts)) - templateData := m.makeTableTemplateData(t) + templateData := m.makeTableTemplateData(t, viewpoints) templateData["er"] = !m.config.ER.Skip switch m.config.ER.Format { case "mermaid": buf := new(bytes.Buffer) mmd := mermaid.New(m.config) - if err := mmd.OutputTable(buf, t); err != nil { + if err := mmd.OutputTable(buf, t, viewpoints); err != nil { return err } templateData["erDiagram"] = fmt.Sprintf("```mermaid\n%s```", buf.String()) @@ -169,7 +169,7 @@ func Output(s *schema.Schema, c *config.Config, force bool) (e error) { _ = f.Close() return errors.WithStack(err) } - if err := md.OutputTable(f, t); err != nil { + if err := md.OutputTable(f, t, s.Viewpoints); err != nil { _ = f.Close() return errors.WithStack(err) } @@ -250,7 +250,7 @@ func DiffSchemas(s, s2 *schema.Schema, c, c2 *config.Config) (string, error) { diffed[tName] = struct{}{} a := new(bytes.Buffer) - if err := md.OutputTable(a, t); err != nil { + if err := md.OutputTable(a, t, s.Viewpoints); err != nil { return "", errors.WithStack(err) } from := fmt.Sprintf("%s %s", mdsnA, tName) @@ -258,7 +258,7 @@ func DiffSchemas(s, s2 *schema.Schema, c, c2 *config.Config) (string, error) { b := new(bytes.Buffer) t2, err := s2.FindTableByName(tName) if err == nil { - if err := md.OutputTable(b, t2); err != nil { + if err := md.OutputTable(b, t2, s.Viewpoints); err != nil { return "", errors.WithStack(err) } } @@ -287,7 +287,7 @@ func DiffSchemas(s, s2 *schema.Schema, c, c2 *config.Config) (string, error) { from := fmt.Sprintf("%s %s", mdsnA, tName) b := new(bytes.Buffer) - if err := md.OutputTable(b, t); err != nil { + if err := md.OutputTable(b, t, s.Viewpoints); err != nil { return "", errors.WithStack(err) } to := fmt.Sprintf("%s %s", mdsnB, tName) @@ -359,7 +359,7 @@ func DiffSchemaAndDocs(docPath string, s *schema.Schema, c *config.Config) (stri for _, t := range s.Tables { buf := new(bytes.Buffer) to := fmt.Sprintf("%s %s", mdsn, t.Name) - if err := md.OutputTable(buf, t); err != nil { + if err := md.OutputTable(buf, t, s.Viewpoints); err != nil { return "", errors.WithStack(err) } fn := fmt.Sprintf("%s.md", t.Name) @@ -529,7 +529,7 @@ func (m *Md) makeSchemaTemplateData(s *schema.Schema) map[string]interface{} { } } -func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} { +func (m *Md) makeTableTemplateData(t *schema.Table, viewpoints schema.Viewpoints) map[string]interface{} { number := m.config.Format.Number adjust := m.config.Format.Adjust hideColumns := m.config.Format.HideColumnsWithoutValues @@ -588,6 +588,32 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} { columnsData = append(columnsData, data) } + // Viewpoints + viewpointsData := [][]string{ + { + m.config.MergedDict.Lookup("Name"), + + m.config.MergedDict.Lookup("Definition"), + }, + {"----", "----------"}, + } + + fmt.Println(m.config.Viewpoints) + for vi, v := range viewpoints { + for _, vt := range v.Tables { + if vt == t.Name { + data := []string{ + fmt.Sprintf("[%s](%sviewpoint-%d.md)", v.Name, m.config.BaseUrl, vi), + v.Desc, + } + + // output data to standard output + fmt.Println(data) + viewpointsData = append(viewpointsData, data) + } + } + } + // Constraints constraintsData := [][]string{ []string{ @@ -689,6 +715,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} { if number { columnsData = m.addNumberToTable(columnsData) + viewpointsData = m.addNumberToTable(viewpointsData) constraintsData = m.addNumberToTable(constraintsData) indexesData = m.addNumberToTable(indexesData) triggersData = m.addNumberToTable(triggersData) @@ -698,6 +725,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 +736,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/output/md/templates/table.md.tmpl b/output/md/templates/table.md.tmpl index 25be0ebc..56250262 100644 --- a/output/md/templates/table.md.tmpl +++ b/output/md/templates/table.md.tmpl @@ -34,6 +34,13 @@ |{{ range $d := $l }} {{ $d | nl2br }} |{{ end }} {{- end }} +{{ $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 }} diff --git a/output/mermaid/mermaid.go b/output/mermaid/mermaid.go index 818b8302..e4070693 100644 --- a/output/mermaid/mermaid.go +++ b/output/mermaid/mermaid.go @@ -82,7 +82,7 @@ func (m *Mermaid) OutputSchema(wr io.Writer, s *schema.Schema) error { } // OutputTable output dot format for table. -func (m *Mermaid) OutputTable(wr io.Writer, t *schema.Table) error { +func (m *Mermaid) OutputTable(wr io.Writer, t *schema.Table, _v schema.Viewpoints) error { tables, relations, err := t.CollectTablesAndRelations(*m.config.ER.Distance, true) if err != nil { return errors.WithStack(err) diff --git a/output/mermaid/mermaid_test.go b/output/mermaid/mermaid_test.go index aec60e57..25526a08 100644 --- a/output/mermaid/mermaid_test.go +++ b/output/mermaid/mermaid_test.go @@ -98,7 +98,7 @@ func TestOutputTable(t *testing.T) { o := New(c) got := &bytes.Buffer{} - if err := o.OutputTable(got, ta); err != nil { + if err := o.OutputTable(got, ta, s.Viewpoints); err != nil { t.Error(err) } f := fmt.Sprintf("mermaid_test_a") @@ -130,7 +130,7 @@ func TestOutputTableTemplate(t *testing.T) { o := New(c) got := &bytes.Buffer{} - if err := o.OutputTable(got, ta); err != nil { + if err := o.OutputTable(got, ta, s.Viewpoints); err != nil { t.Error(err) } f := fmt.Sprintf("mermaid_template_test_a") diff --git a/output/output.go b/output/output.go index b2bca671..570b7d5e 100644 --- a/output/output.go +++ b/output/output.go @@ -15,7 +15,7 @@ import ( // Output is interface for output type Output interface { OutputSchema(wr io.Writer, s *schema.Schema) error - OutputTable(wr io.Writer, s *schema.Table) error + OutputTable(wr io.Writer, s *schema.Table, viewpoints schema.Viewpoints) error } var escapeMermaidRe = regexp.MustCompile(`[^a-zA-Z0-9_\-]`) diff --git a/output/plantuml/plantuml.go b/output/plantuml/plantuml.go index 43594c3a..382fa089 100644 --- a/output/plantuml/plantuml.go +++ b/output/plantuml/plantuml.go @@ -82,7 +82,7 @@ func (p *PlantUML) OutputSchema(wr io.Writer, s *schema.Schema) error { } // OutputTable output dot format for table. -func (p *PlantUML) OutputTable(wr io.Writer, t *schema.Table) error { +func (p *PlantUML) OutputTable(wr io.Writer, t *schema.Table, _v schema.Viewpoints) error { tables, relations, err := t.CollectTablesAndRelations(*p.config.ER.Distance, true) if err != nil { return errors.WithStack(err) diff --git a/output/plantuml/plantuml_test.go b/output/plantuml/plantuml_test.go index 524879e5..f725b927 100644 --- a/output/plantuml/plantuml_test.go +++ b/output/plantuml/plantuml_test.go @@ -99,7 +99,7 @@ func TestOutputTable(t *testing.T) { o := New(c) got := &bytes.Buffer{} - if err := o.OutputTable(got, ta); err != nil { + if err := o.OutputTable(got, ta, s.Viewpoints); err != nil { t.Error(err) } f := fmt.Sprintf("plantuml_test_a.puml") @@ -130,7 +130,7 @@ func TestOutputTableTemplate(t *testing.T) { o := New(c) got := &bytes.Buffer{} - if err := o.OutputTable(got, ta); err != nil { + if err := o.OutputTable(got, ta, s.Viewpoints); err != nil { t.Error(err) } f := fmt.Sprintf("plantuml_template_test_a.puml") diff --git a/output/xlsx/xlsx.go b/output/xlsx/xlsx.go index 5514c8e2..58fbb781 100644 --- a/output/xlsx/xlsx.go +++ b/output/xlsx/xlsx.go @@ -66,7 +66,7 @@ func (x *Xlsx) OutputSchema(wr io.Writer, s *schema.Schema) (e error) { } // OutputTable output Xlsx format for table. -func (x *Xlsx) OutputTable(wr io.Writer, t *schema.Table) (e error) { +func (x *Xlsx) OutputTable(wr io.Writer, t *schema.Table, _v schema.Viewpoints) (e error) { w, err := excl.Create() if err != nil { return err diff --git a/output/yaml/yaml.go b/output/yaml/yaml.go index 4a60fb12..094a617a 100644 --- a/output/yaml/yaml.go +++ b/output/yaml/yaml.go @@ -21,7 +21,7 @@ func (j *YAML) OutputSchema(wr io.Writer, s *schema.Schema) error { } // OutputTable output YAML format for table. -func (j *YAML) OutputTable(wr io.Writer, t *schema.Table) error { +func (j *YAML) OutputTable(wr io.Writer, t *schema.Table, _v schema.Viewpoints) error { encoder := yaml.NewEncoder(wr) err := encoder.Encode(t) if err != nil { 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 |