Skip to content

Commit

Permalink
fix: nil pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong committed Nov 3, 2023
1 parent b3e114d commit 31c4073
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 25 deletions.
21 changes: 13 additions & 8 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,19 @@ func (c Config) Clone() *Config {
if c.Exec.Filename != "" {
newConfig.Exec.Filename = c.Exec.Filename
}
if c.Database.Dir != "" {
newConfig.Database.Dir = c.Database.Dir
}
if c.Database.Package != "" {
newConfig.Database.Package = c.Database.Package
}
if c.Database.Filename != "" {
newConfig.Database.Filename = c.Database.Filename
if c.Database != nil {
if newConfig.Database == nil {
newConfig.Database = new(DatabaseConfig)
}
if c.Database.Dir != "" {
newConfig.Database.Dir = c.Database.Dir
}
if c.Database.Package != "" {
newConfig.Database.Package = c.Database.Package
}
if c.Database.Filename != "" {
newConfig.Database.Filename = c.Database.Filename
}
}
if c.Strict != newConfig.Strict {
newConfig.Strict = c.Strict
Expand Down
1 change: 0 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/sys v0.13.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
Expand Down
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/imported/generated.go.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by sqlgen, version 1.0.0. DO NOT EDIT.
// Code generated by sqlgen, version v1.0.0-beta. DO NOT EDIT.

package imported

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by sqlgen, version 1.0.0. DO NOT EDIT.
// Code generated by sqlgen, version v1.0.0-beta. DO NOT EDIT.

package pkautoincr

Expand Down
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/pk/generated.go.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by sqlgen, version 1.0.0. DO NOT EDIT.
// Code generated by sqlgen, version v1.0.0-beta. DO NOT EDIT.

package pk

Expand Down
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/pk/uuid/generated.go.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by sqlgen, version 1.0.0. DO NOT EDIT.
// Code generated by sqlgen, version v1.0.0-beta. DO NOT EDIT.

package main

Expand Down
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/pointer/generated.go.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by sqlgen, version 1.0.0. DO NOT EDIT.
// Code generated by sqlgen, version v1.0.0-beta. DO NOT EDIT.

package pointer

Expand Down
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/primitive/generated.go.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by sqlgen, version 1.0.0. DO NOT EDIT.
// Code generated by sqlgen, version v1.0.0-beta. DO NOT EDIT.

package primitive

Expand Down
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/unamed/generated.go.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Code generated by sqlgen, version 1.0.0. DO NOT EDIT.
// Code generated by sqlgen, version v1.0.0-beta. DO NOT EDIT.

package unamed
2 changes: 1 addition & 1 deletion examples/testcase/struct-field/valuer/generated.go.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by sqlgen, version 1.0.0. DO NOT EDIT.
// Code generated by sqlgen, version v1.0.0-beta. DO NOT EDIT.

package valuer

Expand Down
75 changes: 69 additions & 6 deletions examples/testdata_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package examples_test

import (
"bufio"
"bytes"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/si3nloong/sqlgen"
"github.com/si3nloong/sqlgen/codegen/config"
_ "github.com/si3nloong/sqlgen/sequel/dialect/mysql"
_ "github.com/si3nloong/sqlgen/sequel/dialect/postgres"
Expand All @@ -18,14 +23,74 @@ import (
)

func TestAll(t *testing.T) {
const rootDir = "./testcase"

if err := codegen.Generate(&config.Config{
Source: []string{"./testcase/**/*.go"},
Source: []string{rootDir + "/**/*.go"},
}); err != nil {
panic(err)
t.Fatal(err)
}

if err := patchVersionInFiles(t, rootDir); err != nil {
t.Fatal(err)
}
panic("")
// Re-generate all files
if err := filepath.Walk("./testcase", func(path string, info fs.FileInfo, e error) error {
if err := generateModel(t, rootDir); err != nil {
t.Fatal(err)
}
}

func patchVersionInFiles(t *testing.T, rootDir string) error {
headerRegex := regexp.MustCompile(`\/\/ Code generated by sqlgen, version (.*)\. DO NOT EDIT\.`)
return filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
return nil
}
log.Println("Path ->", path)

// if fileutil.IsDirEmptyFiles(path) {
// return nil
// }

// Read result file
filename := filepath.Join(path, config.DefaultGeneratedFile+".tpl")
f, err := os.OpenFile(filename, os.O_RDWR, 0o755)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("%w, happened in directory %q", err, path)
}
defer f.Close()

r := bufio.NewReader(f)
line, _, err := r.ReadLine()
if err != nil {
return err
}

matches := headerRegex.FindSubmatch(line)
if len(matches) > 1 {
newVersion := []byte(sqlgen.Version)
if bytes.Equal(matches[1], newVersion) {
return nil
}

f.Seek(int64(len(line)), 0)

log.Println("Patching " + filename)
log.Println(string(bytes.ReplaceAll(line, matches[1], newVersion)))
// log.Println(f.Seek(0, 0))
// f.WriteAt(append(bytes.ReplaceAll(line, matches[1], newVersion), '\n', '\n'), 0)
}

return nil
})
}

func generateModel(t *testing.T, rootDir string) error {
return filepath.Walk(rootDir, func(path string, info fs.FileInfo, e error) error {
if e != nil {
return e
}
Expand Down Expand Up @@ -53,7 +118,5 @@ func TestAll(t *testing.T) {
})

return nil
}); err != nil {
t.Fatal(err)
}
})
}

0 comments on commit 31c4073

Please sign in to comment.