Skip to content

Commit

Permalink
Merge pull request #34 from k1LoW/fix-multiline-comments
Browse files Browse the repository at this point in the history
Support multi-line comment
  • Loading branch information
k1LoW authored Jul 9, 2018
2 parents 6e717a2 + 42a66f6 commit da158f1
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 33 deletions.
20 changes: 16 additions & 4 deletions output/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/k1LoW/tbls/schema"
"github.com/sergi/go-diff/diffmatchpatch"
"strings"
)

// Output generate markdown files.
Expand All @@ -31,7 +32,7 @@ func Output(s *schema.Schema, path string, force bool) error {
}
f, _ := Assets.Open(filepath.Join("/", "index.md.tmpl"))
bs, _ := ioutil.ReadAll(f)
tmpl, err := template.New("index").Parse(string(bs))
tmpl, err := template.New("index").Funcs(funcMap()).Parse(string(bs))
if err != nil {
return err
}
Expand All @@ -57,7 +58,7 @@ func Output(s *schema.Schema, path string, force bool) error {
}
f, _ := Assets.Open(filepath.Join("/", "table.md.tmpl"))
bs, _ := ioutil.ReadAll(f)
tmpl, err := template.New(t.Name).Parse(string(bs))
tmpl, err := template.New(t.Name).Funcs(funcMap()).Parse(string(bs))
if err != nil {
file.Close()
return err
Expand Down Expand Up @@ -97,7 +98,7 @@ func Diff(s *schema.Schema, path string) error {
a := new(bytes.Buffer)
f, _ := Assets.Open(filepath.Join("/", "index.md.tmpl"))
bs, _ := ioutil.ReadAll(f)
tmpl, err := template.New("index").Parse(string(bs))
tmpl, err := template.New("index").Funcs(funcMap()).Parse(string(bs))
if err != nil {
return err
}
Expand Down Expand Up @@ -132,7 +133,7 @@ func Diff(s *schema.Schema, path string) error {
a := new(bytes.Buffer)
f, _ := Assets.Open(filepath.Join("/", "table.md.tmpl"))
bs, _ := ioutil.ReadAll(f)
tmpl, err := template.New(t.Name).Parse(string(bs))
tmpl, err := template.New(t.Name).Funcs(funcMap()).Parse(string(bs))
if err != nil {
return err
}
Expand Down Expand Up @@ -177,3 +178,14 @@ func outputExists(s *schema.Schema, path string) bool {
}
return false
}

func funcMap() map[string]interface{} {
return template.FuncMap{
"nl2br": func(text string) string {
return strings.Replace(text, "\n", "<br>", -1)
},
"nl2mdnl": func(text string) string {
return strings.Replace(text, "\n", " \n", -1)
},
}
}
10 changes: 5 additions & 5 deletions output/md/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import (
"github.com/jessevdk/go-assets"
)

var _Assets43889384df1c6f74d764c29d91b9d5637eb46061 = "# {{ .Schema.Name }}\n\n## Tables\n\n| Name | Columns | Comment | Type |\n| ---- | ------- | ------- | ---- |\n{{- range $i, $t := .Schema.Tables }}\n| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment }} | {{ $t.Type }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er](schema.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)"
var _Assetsac44302fb6150a621aa9d04a0350aac972bf7e18 = "# {{ .Table.Name }}\n\n## Description\n\n{{ .Table.Comment -}}\n{{ if .Table.Def }}\n<details>\n<summary><strong>Table Definition</strong></summary>\n\n```sql\n{{ .Table.Def }}\n```\n\n</details>\n{{ end }}\n\n## Columns\n\n| Name | Type | Default | Nullable | Children | Parents | Comment |\n| ---- | ---- | ------- | -------- | -------- | ------- | ------- |\n{{- range $i, $c := .Table.Columns }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment }} |\n{{- end }}\n\n## Constraints\n\n| Name | Type | Definition |\n| ---- | ---- | --- |\n{{- range $i, $c := .Table.Constraints }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Def }} |\n{{- end }}\n\n## Indexes\n\n| Name | Definition |\n| ---- | --- |\n{{- range $i, $idx := .Table.Indexes }}\n| {{ $idx.Name }} | {{ $idx.Def }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er]({{ .Table.Name }}.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)"
var _Assets43889384df1c6f74d764c29d91b9d5637eb46061 = "# {{ .Schema.Name }}\n\n## Tables\n\n| Name | Columns | Comment | Type |\n| ---- | ------- | ------- | ---- |\n{{- range $i, $t := .Schema.Tables }}\n| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment | nl2br }} | {{ $t.Type }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er](schema.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)"
var _Assetsac44302fb6150a621aa9d04a0350aac972bf7e18 = "# {{ .Table.Name }}\n\n## Description\n\n{{ .Table.Comment | nl2mdnl -}}\n{{ if .Table.Def }}\n<details>\n<summary><strong>Table Definition</strong></summary>\n\n```sql\n{{ .Table.Def }}\n```\n\n</details>\n{{ end }}\n\n## Columns\n\n| Name | Type | Default | Nullable | Children | Parents | Comment |\n| ---- | ---- | ------- | -------- | -------- | ------- | ------- |\n{{- range $i, $c := .Table.Columns }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment | nl2br }} |\n{{- end }}\n\n## Constraints\n\n| Name | Type | Definition |\n| ---- | ---- | --- |\n{{- range $i, $c := .Table.Constraints }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Def }} |\n{{- end }}\n\n## Indexes\n\n| Name | Definition |\n| ---- | --- |\n{{- range $i, $idx := .Table.Indexes }}\n| {{ $idx.Name }} | {{ $idx.Def }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er]({{ .Table.Name }}.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)"

// Assets returns go-assets FileSystem
var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"index.md.tmpl", "table.md.tmpl"}}, map[string]*assets.File{
"/": &assets.File{
Path: "/",
FileMode: 0x800001ed,
Mtime: time.Unix(1528211857, 1528211857000000000),
Mtime: time.Unix(1531144283, 1531144283000000000),
Data: nil,
}, "/index.md.tmpl": &assets.File{
Path: "/index.md.tmpl",
FileMode: 0x1a4,
Mtime: time.Unix(1527684406, 1527684406000000000),
Mtime: time.Unix(1531143680, 1531143680000000000),
Data: []byte(_Assets43889384df1c6f74d764c29d91b9d5637eb46061),
}, "/table.md.tmpl": &assets.File{
Path: "/table.md.tmpl",
FileMode: 0x1a4,
Mtime: time.Unix(1528211857, 1528211857000000000),
Mtime: time.Unix(1531144283, 1531144283000000000),
Data: []byte(_Assetsac44302fb6150a621aa9d04a0350aac972bf7e18),
}}, "")
2 changes: 1 addition & 1 deletion output/md/templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Name | Columns | Comment | Type |
| ---- | ------- | ------- | ---- |
{{- range $i, $t := .Schema.Tables }}
| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment }} | {{ $t.Type }} |
| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment | nl2br }} | {{ $t.Type }} |
{{- end }}

{{ if .er -}}
Expand Down
4 changes: 2 additions & 2 deletions output/md/templates/table.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description

{{ .Table.Comment -}}
{{ .Table.Comment | nl2mdnl -}}
{{ if .Table.Def }}
<details>
<summary><strong>Table Definition</strong></summary>
Expand All @@ -19,7 +19,7 @@
| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
{{- range $i, $c := .Table.Columns }}
| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment }} |
| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment | nl2br }} |
{{- end }}

## Constraints
Expand Down
2 changes: 1 addition & 1 deletion sample/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| ---- | ------- | ------- | ---- |
| [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE |
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
| [comments](comments.md) | 6 | | BASE TABLE |
| [comments](comments.md) | 6 | Comments<br>Multi-line table comment | BASE TABLE |
| [logs](logs.md) | 7 | audit log table | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| [posts](posts.md) | 7 | Posts table | BASE TABLE |
Expand Down
9 changes: 5 additions & 4 deletions sample/mysql/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Description


Comments
Multi-line table comment
<details>
<summary><strong>Table Definition</strong></summary>

Expand All @@ -11,7 +12,7 @@ CREATE TABLE `comments` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`comment` text NOT NULL,
`comment` text NOT NULL COMMENT 'Comment\nMulti-line column comment',
`created` datetime NOT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
Expand All @@ -20,7 +21,7 @@ CREATE TABLE `comments` (
KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`) USING HASH,
CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Comments\nMulti-line table comment'
```

</details>
Expand All @@ -33,7 +34,7 @@ CREATE TABLE `comments` (
| id | bigint(20) | | false | [logs](logs.md) | | |
| post_id | bigint(20) | | false | [comment_stars](comment_stars.md) | [posts](posts.md) | |
| user_id | int(11) | | false | [comment_stars](comment_stars.md) | [users](users.md) | |
| comment | text | | false | | | |
| comment | text | | false | | | Comment<br>Multi-line column comment |
| created | datetime | | false | | | |
| updated | datetime | | true | | | |

Expand Down
2 changes: 1 addition & 1 deletion sample/mysql/post_comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CREATE VIEW post_comments AS ((select `c`.`id` AS `id`,`p`.`title` AS `title`,`u
| id | bigint(20) | 0 | true | | | comments.id |
| title | varchar(255) | | false | | | posts.title |
| post_user | varchar(50) | | true | | | posts.users.username |
| comment | text | | true | | | |
| comment | text | | true | | | Comment<br>Multi-line column comment |
| comment_user | varchar(50) | | true | | | comments.users.username |
| created | datetime | | true | | | comments.created |
| updated | datetime | | true | | | comments.updated |
Expand Down
2 changes: 1 addition & 1 deletion sample/mysql8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| ---- | ------- | ------- | ---- |
| [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE |
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
| [comments](comments.md) | 6 | | BASE TABLE |
| [comments](comments.md) | 6 | Comments<br>Multi-line table comment | BASE TABLE |
| [logs](logs.md) | 7 | audit log table | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| [posts](posts.md) | 7 | Posts table | BASE TABLE |
Expand Down
9 changes: 5 additions & 4 deletions sample/mysql8/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Description


Comments
Multi-line table comment
<details>
<summary><strong>Table Definition</strong></summary>

Expand All @@ -11,7 +12,7 @@ CREATE TABLE `comments` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`comment` text NOT NULL,
`comment` text NOT NULL COMMENT 'Comment\nMulti-line column comment',
`created` datetime NOT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
Expand All @@ -20,7 +21,7 @@ CREATE TABLE `comments` (
KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`),
CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Comments\nMulti-line table comment'
```

</details>
Expand All @@ -33,7 +34,7 @@ CREATE TABLE `comments` (
| id | bigint(20) | | false | [logs](logs.md) | | |
| post_id | bigint(20) | | false | [comment_stars](comment_stars.md) | [posts](posts.md) | |
| user_id | int(11) | | false | [comment_stars](comment_stars.md) | [users](users.md) | |
| comment | text | | false | | | |
| comment | text | | false | | | Comment<br>Multi-line column comment |
| created | datetime | | false | | | |
| updated | datetime | | true | | | |

Expand Down
2 changes: 1 addition & 1 deletion sample/mysql8/post_comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CREATE VIEW post_comments AS (select `c`.`id` AS `id`,`p`.`title` AS `title`,`u2
| id | bigint(20) | 0 | true | | | comments.id |
| title | varchar(255) | | false | | | posts.title |
| post_user | varchar(50) | | true | | | posts.users.username |
| comment | text | | true | | | |
| comment | text | | true | | | Comment<br>Multi-line column comment |
| comment_user | varchar(50) | | true | | | comments.users.username |
| created | datetime | | true | | | comments.created |
| updated | datetime | | true | | | comments.updated |
Expand Down
2 changes: 1 addition & 1 deletion sample/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| ---- | ------- | ------- | ---- |
| [users](users.md) | 6 | Users table | BASE TABLE |
| [posts](posts.md) | 8 | Posts table | BASE TABLE |
| [comments](comments.md) | 6 | | BASE TABLE |
| [comments](comments.md) | 6 | Comments<br>Multi-line table comment | BASE TABLE |
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
| [logs](logs.md) | 7 | audit log table | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
Expand Down
5 changes: 3 additions & 2 deletions sample/postgres/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Description


Comments
Multi-line table comment

## Columns

Expand All @@ -11,7 +12,7 @@
| id | bigint | nextval('comments_id_seq'::regclass) | false | [logs](logs.md) | | |
| post_id | bigint | | false | [comment_stars](comment_stars.md) | [posts](posts.md) | |
| user_id | integer | | false | [comment_stars](comment_stars.md) | [users](users.md) | |
| comment | text | | false | | | |
| comment | text | | false | | | Comment<br>Multi-line column comment |
| created | timestamp without time zone | | false | | | |
| updated | timestamp without time zone | | true | | | |

Expand Down
4 changes: 2 additions & 2 deletions test/my.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ CREATE TABLE comments (
id bigint AUTO_INCREMENT,
post_id bigint NOT NULL,
user_id int NOT NULL,
comment text NOT NULL,
comment text NOT NULL COMMENT 'Comment\nMulti-line column comment',
created datetime NOT NULL,
updated datetime,
CONSTRAINT comments_id_pk PRIMARY KEY(id),
CONSTRAINT comments_post_id_fk FOREIGN KEY(post_id) REFERENCES posts(id) MATCH SIMPLE,
CONSTRAINT comments_user_id_fk FOREIGN KEY(user_id) REFERENCES users(id) MATCH SIMPLE,
UNIQUE(post_id, user_id)
);
) COMMENT = 'Comments\nMulti-line table comment';
CREATE INDEX comments_post_id_user_id_idx ON comments(post_id, user_id) USING HASH;

CREATE TABLE comment_stars (
Expand Down
11 changes: 7 additions & 4 deletions test/pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ CREATE TABLE users (
created timestamp NOT NULL,
updated timestamp
);
COMMENT ON TABLE users is 'Users table';
COMMENT ON COLUMN users.email is 'ex. [email protected]';
COMMENT ON TABLE users IS 'Users table';
COMMENT ON COLUMN users.email IS 'ex. [email protected]';

CREATE TABLE posts (
id bigserial NOT NULL,
Expand All @@ -40,8 +40,8 @@ CREATE TABLE posts (
CONSTRAINT posts_user_id_fk FOREIGN KEY(user_id) REFERENCES users(id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE,
UNIQUE(user_id, title)
);
COMMENT ON TABLE posts is 'Posts table';
COMMENT ON COLUMN posts.post_type is 'public/private/draft';
COMMENT ON TABLE posts IS 'Posts table';
COMMENT ON COLUMN posts.post_type IS 'public/private/draft';

CREATE INDEX posts_user_id_idx ON posts USING btree(user_id);

Expand All @@ -57,6 +57,9 @@ CREATE TABLE comments (
CONSTRAINT comments_user_id_fk FOREIGN KEY(user_id) REFERENCES users(id) MATCH SIMPLE,
UNIQUE(post_id, user_id)
);
COMMENT ON TABLE comments IS E'Comments\nMulti-line table comment';
COMMENT ON COLUMN comments.comment IS E'Comment\nMulti-line column comment';

CREATE INDEX comments_post_id_user_id_idx ON comments USING btree(post_id, user_id);

CREATE TABLE comment_stars (
Expand Down

0 comments on commit da158f1

Please sign in to comment.