Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:refer viewpoints from tables #530

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion output/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion output/dot/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions output/dot/dot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") != "" {
Expand Down Expand Up @@ -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") != "" {
Expand Down
6 changes: 3 additions & 3 deletions output/gviz/gviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion output/gviz/gviz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") != "" {
Expand Down
2 changes: 1 addition & 1 deletion output/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("", " ")
Expand Down
47 changes: 38 additions & 9 deletions output/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -250,15 +250,15 @@ 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)

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)
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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),
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions output/md/templates/table.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion output/mermaid/mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions output/mermaid/mermaid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k1LoW
To refer viewpoints from tables, I need to chenge the Output interface and that affects other unrelated files like dot.go.
If you have better idea to implement this feature, kindly let me know.
I appletiate it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to add a Viewpoints field to the Table.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@macoto1995 You may have it implemented as is. I will correct it on my end!

Copy link
Contributor Author

@majimaccho majimaccho Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k1LoW
Oh..Sorry I missed your new comment for a few days.

It may be better to add a Viewpoints field to the Table.

I have some concern about this solution.
Since Viewpoint has Tables as children, it would be infinite loop.
Plus, to render link to viewpoints from the markdown, the OutputTable function has to know the index of the viewpoints, which is not included in Viewpoint type.

So I suppose that it's better to have TableViewpoint type like below.

struct TableViewpoint {
    Index Int
    Name String
    Desc String
}

Kindly, let me know what you think.
I'd work on this way for now.
Thank you.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Viewpoint has Tables as children, it would be infinite loop.

Yes. In fact, recursive relationships are everywhere (e.g. Table.ReferencedTables and Table.Column.ParentRelations ).

Plus, to render link to viewpoints from the markdown, the OutputTable function has to know the index of the viewpoints, which is not included in Viewpoint type.

fmfm, indeed. Good point.

So I suppose that it's better to have TableViewpoint type like below.

LGTM. So far I have no better ideas either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll work on this way!

}

var escapeMermaidRe = regexp.MustCompile(`[^a-zA-Z0-9_\-]`)
Expand Down
2 changes: 1 addition & 1 deletion output/plantuml/plantuml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions output/plantuml/plantuml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion output/xlsx/xlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion output/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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<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.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