Skip to content

Commit

Permalink
Merge pull request #28 from mackee/feature/table-annotation-for-go1.19
Browse files Browse the repository at this point in the history
add: support new annotation for go1.19
  • Loading branch information
mackee committed Oct 18, 2022
2 parents 183a9ea + e35563e commit eeef792
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion _example/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type UserId uint64

//+table: user
// +table: user
type User struct {
Id UserId `db:"id,primarykey,autoincrement"`
Name string `db:"name"`
Expand Down
2 changes: 1 addition & 1 deletion _example/user_external.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "time"

//go:generate go run ../cmd/sqlla/main.go

//+table: user_external
// +table: user_external
type UserExternal struct {
Id uint64 `db:"id,primarykey"`
UserId uint64 `db:"user_id"`
Expand Down
2 changes: 1 addition & 1 deletion _example/user_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

//go:generate go run ../cmd/sqlla/main.go

//+table: user_item
// +table: user_item
type UserItem struct {
Id uint64 `db:"id,primarykey,autoincrement"`
UserId uint64 `db:"user_id"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/mysql2schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func outputSchema(db *sql.DB, table string, out io.Writer) error {
}

outBuf := new(bytes.Buffer)
outBuf.WriteString("//+table: " + table + "\n")
outBuf.WriteString("// +table: " + table + "\n")
outBuf.WriteString("type ")
outBuf.WriteString(snaker.SnakeToCamel(table))
outBuf.WriteString(" struct {\n")
Expand Down
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Run(from, ext string) {
var hasAnnotation bool
var annotationComment string
for _, comment := range genDecl.Doc.List {
if strings.HasPrefix(comment.Text, "//+table:") {
if trimmed := trimAnnotation(comment.Text); trimmed != comment.Text {
hasAnnotation = true
annotationComment = comment.Text
break
Expand Down Expand Up @@ -90,7 +90,7 @@ func toTable(tablePkg *types.Package, annotationComment string, gd *ast.GenDecl,
table.Package = tablePkg
table.PackageName = tablePkg.Name()

tableName := strings.TrimPrefix(annotationComment, "//+table: ")
tableName := trimAnnotation(annotationComment)
table.Name = tableName

spec := gd.Specs[0]
Expand Down Expand Up @@ -155,3 +155,13 @@ func toTable(tablePkg *types.Package, annotationComment string, gd *ast.GenDecl,

return table, nil
}

func trimAnnotation(comment string) string {
prefixes := []string{"//+table: ", "// +table: ", "//sqlla:table "}
for _, prefix := range prefixes {
if trimmed := strings.TrimPrefix(comment, prefix); trimmed != comment {
return trimmed
}
}
return comment
}

0 comments on commit eeef792

Please sign in to comment.