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 #532

Merged
merged 8 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 25 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()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure if this is is appropriate to set viewpoints to table.
I could be better to be in config.Analyze but it can effect more files.

Copy link
Owner

Choose a reason for hiding this comment

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

Let's start with the minimum effect range.

I like refactoring 👍

if err != nil {
return errors.WithStack(err)
}

docPath := c.DocPath

fullPath, err := filepath.Abs(docPath)
Expand Down Expand Up @@ -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,
Copy link
Owner

Choose a reason for hiding this comment

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

I would like to change the contents of the display using showOnlyFirstParagraph

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
Alright! I fixed it 👍

}

viewpointsData = append(viewpointsData, data)
}

// Constraints
constraintsData := [][]string{
[]string{
Expand Down Expand Up @@ -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),
Expand All @@ -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,
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
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
Loading