Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit f7cd316

Browse files
committed
feat: modify comment annotation detection regex
1 parent 5092116 commit f7cd316

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ package sample
2222

2323
// User is a user model struct.
2424
//
25-
// pgddl: table: "users"
26-
// pgddl: constraint: UNIQUE ("username")
27-
// pgddl: index: "index_users_username" ON "users" ("username")
25+
//pgddl:table "users"
26+
//pgddl:constraint UNIQUE ("username")
27+
//pgddl:index "index_users_username" ON "users" ("username")
2828
type User struct {
2929
UserID string `db:"user_id" pgddl:"TEXT NOT NULL" pk:"true"`
3030
Username string `db:"username" pgddl:"TEXT NOT NULL"`
@@ -33,8 +33,8 @@ type User struct {
3333

3434
// Group is a group model struct.
3535
//
36-
// pgddl: table: CREATE TABLE IF NOT EXISTS "groups"
37-
// pgddl: index: CREATE UNIQUE INDEX "index_groups_group_name" ON "groups" ("group_name")
36+
//pgddl:table CREATE TABLE IF NOT EXISTS "groups"
37+
//pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON "groups" ("group_name")
3838
type Group struct {
3939
GroupID string `db:"group_id" pgddl:"TEXT NOT NULL" pk:"true"`
4040
GroupName string `db:"group_name" pgddl:"TEXT NOT NULL"`
@@ -55,8 +55,8 @@ $ cat /tmp/sample.sql
5555
-- source: tmp/sample.go:5
5656
-- User is a user model struct.
5757
--
58-
-- pgddl: table: "users"
59-
-- pgddl: constraint: UNIQUE ("username")
58+
-- pgddl:table "users"
59+
-- pgddl:constraint UNIQUE ("username")
6060
CREATE TABLE "users" (
6161
"user_id" TEXT NOT NULL,
6262
"username" TEXT NOT NULL,
@@ -66,13 +66,13 @@ CREATE TABLE "users" (
6666
);
6767

6868
-- source: tmp/sample.go:7
69-
-- pgddl: index: "index_users_username" ON "users" ("username")
69+
-- pgddl:index "index_users_username" ON "users" ("username")
7070
CREATE INDEX "index_users_username" ON "users" ("username");
7171

7272
-- source: tmp/sample.go:16
7373
-- Group is a group model struct.
7474
--
75-
-- pgddl: table: CREATE TABLE IF NOT EXISTS "groups"
75+
-- pgddl:table CREATE TABLE IF NOT EXISTS "groups"
7676
CREATE TABLE IF NOT EXISTS "groups" (
7777
"group_id" TEXT NOT NULL,
7878
"group_name" TEXT NOT NULL,
@@ -81,7 +81,7 @@ CREATE TABLE IF NOT EXISTS "groups" (
8181
);
8282

8383
-- source: tmp/sample.go:17
84-
-- pgddl: index: CREATE UNIQUE INDEX "index_groups_group_name" ON "groups" ("group_name")
84+
-- pgddl:index CREATE UNIQUE INDEX "index_groups_group_name" ON "groups" ("group_name")
8585
CREATE UNIQUE INDEX "index_groups_group_name" ON "groups" ("group_name");
8686

8787
```

internal/ddlgen/ddl/dialect/spanner/integrationtest_go_001.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ CREATE TABLE Author (
5959
-- source: spanner/integrationtest_go_001.source:49
6060
-- Book is a book.
6161
--
62-
-- spanddl:table:`Books`
63-
-- spanddl:option: INTERLEAVE IN PARENT `Author` ON DELETE CASCADE
62+
-- spanddl:table `Books`
63+
-- spanddl:option INTERLEAVE IN PARENT `Author` ON DELETE CASCADE
6464
CREATE TABLE `Books` (
6565
-- WARN: the "Book" struct's "AuthorID" field does not have a tag for column name (`dbtest:"<ColumnName>"`), so the field name "AuthorID" is used as the column name.
6666
-- AuthorID is a book author.

internal/ddlgen/ddl/dialect/spanner/integrationtest_go_001.source

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ type (
4646

4747
// Book is a book.
4848
//
49-
// spanddl:table:`Books`
50-
// spanddl:option: INTERLEAVE IN PARENT `Author` ON DELETE CASCADE
49+
//spanddl:table `Books`
50+
//spanddl:option INTERLEAVE IN PARENT `Author` ON DELETE CASCADE
5151
Book struct {
5252
// AuthorID is a book author.
5353
AuthorID string `spanddl:"STRING(255) NOT NULL" pkey:"true"`

internal/ddlgen/lang/go/parse.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212
"reflect"
1313
"sort"
1414
"strings"
15+
"unicode"
1516

1617
errorz "github.com/kunitsucom/util.go/errors"
1718
filepathz "github.com/kunitsucom/util.go/path/filepath"
19+
slicez "github.com/kunitsucom/util.go/slices"
1820

1921
"github.com/kunitsucom/ddlgen/internal/config"
2022
ddlast "github.com/kunitsucom/ddlgen/internal/ddlgen/ddl"
@@ -110,7 +112,9 @@ func parseFile(ctx context.Context, filename string) ([]ddlast.Stmt, error) {
110112
createTableStmt.SourceLine = r.Position.Line
111113

112114
// CREATE TABLE (or INDEX) / CONSTRAINT / OPTIONS (from comments)
113-
comments := strings.Split(strings.Trim(r.CommentGroup.Text(), "\n"), "\n")
115+
comments := slicez.Select(r.CommentGroup.List, func(_ int, comment *ast.Comment) string {
116+
return strings.TrimLeftFunc(strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(comment.Text, "//"), "/*"), "*/"), unicode.IsSpace)
117+
})
114118
for _, comment := range comments {
115119
logs.Debug.Printf("[COMMENT DETECTED]: %s:%d: %s", createTableStmt.SourceFile, createTableStmt.SourceLine, comment)
116120

internal/ddlgen/lang/util/regex.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ type StmtRegex struct {
1010
//nolint:gochecknoglobals
1111
var (
1212
StmtRegexCreateTable = StmtRegex{
13-
Regex: regexp.MustCompile(`^\s*(//+\s*|/\*\s*)?\S+\s*:\s*table(s)?\s*:\s*((CREATE\s+TABLE\s+)?\S+.*)`),
13+
Regex: regexp.MustCompile(`^\s*(//+\s*|/\*\s*)?\S+\s*:\s*table(s)?\s*[: ]\s*((CREATE\s+TABLE\s+)?\S+.*)`),
1414
Index: 3,
1515
}
1616
StmtRegexCreateTableConstraint = StmtRegex{
17-
Regex: regexp.MustCompile(`^\s*(//+\s*|/\*\s*)?\S+\s*:\s*constraint(s)?\s*:\s*(\S+.*)`),
17+
Regex: regexp.MustCompile(`^\s*(//+\s*|/\*\s*)?\S+\s*:\s*constraint(s)?\s*[: ]\s*(\S+.*)`),
1818
Index: 3,
1919
}
2020
StmtRegexCreateTableOptions = StmtRegex{
21-
Regex: regexp.MustCompile(`^\s*(//+\s*|/\*\s*)?\S+\s*:\s*option(s)?\s*:\s*(\S+.*)`),
21+
Regex: regexp.MustCompile(`^\s*(//+\s*|/\*\s*)?\S+\s*:\s*option(s)?\s*[: ]\s*(\S+.*)`),
2222
Index: 3,
2323
}
2424
StmtRegexCreateIndex = StmtRegex{
25-
Regex: regexp.MustCompile(`^\s*(//+\s*|/\*\s*)?\S+\s*:\s*index(es)?\s*:\s*(\S+.*)`),
25+
Regex: regexp.MustCompile(`^\s*(//+\s*|/\*\s*)?\S+\s*:\s*index(es)?\s*[: ]\s*(\S+.*)`),
2626
Index: 3,
2727
}
2828
)

0 commit comments

Comments
 (0)