Skip to content

Commit

Permalink
refactor: Fix misc (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Aug 14, 2024
2 parents c87297b + 9b9410e commit d1187d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions pkg/internal/lang/go/dump_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ddlctlgo

import (
"bytes"
goast "go/ast"
"go/ast"
"go/token"
"io"

Expand All @@ -16,19 +16,19 @@ func dumpDDLSource(fset *token.FileSet, ddlSrc []*ddlSource) {
logs.Trace.Print("-- CommentGroup --------------------------------------------------------------------------------------------------------------------------------")
{
commentGroupAST := bytes.NewBuffer(nil)
goast.Fprint(commentGroupAST, fset, r.CommentGroup, goast.NotNilFilter)
ast.Fprint(commentGroupAST, fset, r.CommentGroup, ast.NotNilFilter)
_, _ = logs.Trace.LineWriter("").Write(commentGroupAST.Bytes())
}
logs.Trace.Print("-- TypeSpec --------------------------------------------------------------------------------------------------------------------------------")
{
typeSpecAST := bytes.NewBuffer(nil)
goast.Fprint(typeSpecAST, fset, r.TypeSpec, goast.NotNilFilter)
ast.Fprint(typeSpecAST, fset, r.TypeSpec, ast.NotNilFilter)
_, _ = logs.Trace.LineWriter("").Write(typeSpecAST.Bytes())
}
logs.Trace.Print("-- StructType --------------------------------------------------------------------------------------------------------------------------------")
{
structTypeAST := bytes.NewBuffer(nil)
goast.Fprint(structTypeAST, fset, r.StructType, goast.NotNilFilter)
ast.Fprint(structTypeAST, fset, r.StructType, ast.NotNilFilter)
_, _ = logs.Trace.LineWriter("").Write(structTypeAST.Bytes())
}
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/internal/lang/go/extract_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ddlctlgo
import (
"context"
"fmt"
goast "go/ast"
"go/ast"
"go/token"
"regexp"
"sync"
Expand All @@ -18,10 +18,10 @@ import (
type ddlSource struct {
Position token.Position
// TypeSpec is used to guess the table name if the CREATE TABLE annotation is not found.
TypeSpec *goast.TypeSpec
TypeSpec *ast.TypeSpec
// StructType is used to determine the column name. If the tag specified by --go-column-tag is not found, the field name is used.
StructType *goast.StructType
CommentGroup *goast.CommentGroup
StructType *ast.StructType
CommentGroup *ast.CommentGroup
}

//nolint:gochecknoglobals
Expand All @@ -47,10 +47,10 @@ func DDLTagGoCommentLineRegex() *regexp.Regexp {

//
//nolint:cyclop
func extractDDLSourceFromDDLTagGo(_ context.Context, fset *token.FileSet, f *goast.File) ([]*ddlSource, error) {
func extractDDLSourceFromDDLTagGo(_ context.Context, fset *token.FileSet, f *ast.File) ([]*ddlSource, error) {
ddlSrc := make([]*ddlSource, 0)

for commentedNode, commentGroups := range goast.NewCommentMap(fset, f, f.Comments) {
for commentedNode, commentGroups := range ast.NewCommentMap(fset, f, f.Comments) {
for _, commentGroup := range commentGroups {
CommentGroupLoop:
for _, commentLine := range commentGroup.List {
Expand All @@ -61,12 +61,12 @@ func extractDDLSourceFromDDLTagGo(_ context.Context, fset *token.FileSet, f *goa
Position: fset.Position(commentLine.Pos()),
CommentGroup: commentGroup,
}
goast.Inspect(commentedNode, func(node goast.Node) bool {
ast.Inspect(commentedNode, func(node ast.Node) bool {
switch n := node.(type) {
case *goast.TypeSpec:
case *ast.TypeSpec:
s.TypeSpec = n
switch t := n.Type.(type) {
case *goast.StructType:
case *ast.StructType:
s.StructType = t
return false
default: // noop
Expand Down
22 changes: 11 additions & 11 deletions pkg/internal/lang/go/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

apperr "github.com/kunitsucom/ddlctl/pkg/apperr"
"github.com/kunitsucom/ddlctl/pkg/internal/config"
ddlast "github.com/kunitsucom/ddlctl/pkg/internal/generator"
"github.com/kunitsucom/ddlctl/pkg/internal/generator"
langutil "github.com/kunitsucom/ddlctl/pkg/internal/lang/util"
"github.com/kunitsucom/ddlctl/pkg/internal/util"
"github.com/kunitsucom/ddlctl/pkg/logs"
Expand All @@ -31,7 +31,7 @@ const (
)

//nolint:cyclop
func Parse(ctx context.Context, src string) (*ddlast.DDL, error) {
func Parse(ctx context.Context, src string) (*generator.DDL, error) {
// MEMO: get absolute path for parser.ParseFile()
sourceAbs := util.Abs(src)

Expand All @@ -40,7 +40,7 @@ func Parse(ctx context.Context, src string) (*ddlast.DDL, error) {
return nil, apperr.Errorf("os.Stat: %w", err)
}

ddl := ddlast.NewDDL(ctx)
ddl := generator.NewDDL(ctx)

if info.IsDir() {
if err := filepath.WalkDir(sourceAbs, walkDirFn(ctx, ddl)); err != nil {
Expand All @@ -62,7 +62,7 @@ func Parse(ctx context.Context, src string) (*ddlast.DDL, error) {
//nolint:gochecknoglobals
var fileSuffix = ".go"

func walkDirFn(ctx context.Context, ddl *ddlast.DDL) func(path string, d os.DirEntry, err error) error {
func walkDirFn(ctx context.Context, ddl *generator.DDL) func(path string, d os.DirEntry, err error) error {
return func(path string, d os.DirEntry, err error) error {
if err != nil {
return err //nolint:wrapcheck
Expand All @@ -88,7 +88,7 @@ func walkDirFn(ctx context.Context, ddl *ddlast.DDL) func(path string, d os.DirE
}

//nolint:cyclop,funlen,gocognit
func parseFile(ctx context.Context, filename string) ([]ddlast.Stmt, error) {
func parseFile(ctx context.Context, filename string) ([]generator.Stmt, error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
Expand All @@ -102,9 +102,9 @@ func parseFile(ctx context.Context, filename string) ([]ddlast.Stmt, error) {

dumpDDLSource(fset, ddlSrc)

stmts := make([]ddlast.Stmt, 0)
stmts := make([]generator.Stmt, 0)
for _, r := range ddlSrc {
createTableStmt := &ddlast.CreateTableStmt{}
createTableStmt := &generator.CreateTableStmt{}

// source
createTableStmt.SourceFile = r.Position.Filename
Expand All @@ -121,7 +121,7 @@ func parseFile(ctx context.Context, filename string) ([]ddlast.Stmt, error) {
if /* CREATE INDEX */ matches := langutil.StmtRegexCreateIndex.Regex.FindStringSubmatch(comment); len(matches) > langutil.StmtRegexCreateIndex.Index {
commentMatchedCreateIndex := comment
source := fset.Position(extractContainingCommentFromCommentGroup(r.CommentGroup, commentMatchedCreateIndex).Pos())
createIndexStmt := &ddlast.CreateIndexStmt{
createIndexStmt := &generator.CreateIndexStmt{
Comments: []string{commentMatchedCreateIndex},
SourceFile: source.Filename,
SourceLine: source.Line,
Expand All @@ -134,11 +134,11 @@ func parseFile(ctx context.Context, filename string) ([]ddlast.Stmt, error) {
if /* CREATE TABLE */ matches := langutil.StmtRegexCreateTable.Regex.FindStringSubmatch(comment); len(matches) > langutil.StmtRegexCreateTable.Index {
createTableStmt.SetCreateTable(matches[langutil.StmtRegexCreateTable.Index])
} else if /* CONSTRAINT */ matches := langutil.StmtRegexCreateTableConstraint.Regex.FindStringSubmatch(comment); len(matches) > langutil.StmtRegexCreateTableConstraint.Index {
createTableStmt.Constraints = append(createTableStmt.Constraints, &ddlast.CreateTableConstraint{
createTableStmt.Constraints = append(createTableStmt.Constraints, &generator.CreateTableConstraint{
Constraint: matches[langutil.StmtRegexCreateTableConstraint.Index],
})
} else if /* OPTIONS */ matches := langutil.StmtRegexCreateTableOptions.Regex.FindStringSubmatch(comment); len(matches) > langutil.StmtRegexCreateTableOptions.Index {
createTableStmt.Options = append(createTableStmt.Options, &ddlast.CreateTableOption{
createTableStmt.Options = append(createTableStmt.Options, &generator.CreateTableOption{
Option: matches[langutil.StmtRegexCreateTableOptions.Index],
})
}
Expand All @@ -157,7 +157,7 @@ func parseFile(ctx context.Context, filename string) ([]ddlast.Stmt, error) {
// columns
if r.StructType != nil {
for _, field := range r.StructType.Fields.List {
column := &ddlast.CreateTableColumn{}
column := &generator.CreateTableColumn{}

tag := reflect.StructTag(strings.Trim(field.Tag.Value, "`"))

Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/lang/go/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/kunitsucom/ddlctl/pkg/apperr"
"github.com/kunitsucom/ddlctl/pkg/internal/config"
"github.com/kunitsucom/ddlctl/pkg/internal/fixture"
ddlast "github.com/kunitsucom/ddlctl/pkg/internal/generator"
"github.com/kunitsucom/ddlctl/pkg/internal/generator"
)

//nolint:paralleltest
Expand Down Expand Up @@ -226,7 +226,7 @@ func Test_walkDirFn(t *testing.T) {
require.NoError(t, err)
}

ddl := ddlast.NewDDL(ctx)
ddl := generator.NewDDL(ctx)
fn := walkDirFn(ctx, ddl)
{
err := fn("", nil, os.ErrPermission)
Expand Down

0 comments on commit d1187d1

Please sign in to comment.