Skip to content

Commit

Permalink
Merge pull request #40 from swiftcarrot/remove-goimports
Browse files Browse the repository at this point in the history
remove goimports
  • Loading branch information
wangzuo authored Aug 21, 2023
2 parents 91c5784 + 3c10997 commit a0ec0d0
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 111 deletions.
44 changes: 22 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@ clean:

test-postgresql: install
rm -rf internal/integration/db
cd internal/integration && QUERYX_ENV=test queryx db:drop --schema postgresql.hcl
cd internal/integration && QUERYX_ENV=test queryx db:create --schema postgresql.hcl
cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema postgresql.hcl
cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema postgresql.hcl
cd internal/integration && QUERYX_ENV=test queryx generate --schema postgresql.hcl
cd internal/integration && queryx db:drop --schema postgresql.hcl
cd internal/integration && queryx db:create --schema postgresql.hcl
cd internal/integration && queryx db:migrate --schema postgresql.hcl
cd internal/integration && queryx db:migrate --schema postgresql.hcl
cd internal/integration && queryx generate --schema postgresql.hcl
cd internal/integration && yarn tsc
cd internal/integration && yarn test
# cd internal/integration && go test ./...
# cd internal/integration && QUERYX_ENV=test queryx db:drop --schema postgresql.hcl
# cd internal/integration && queryx db:drop --schema postgresql.hcl

test-mysql: install
rm -rf internal/integration/db
cd internal/integration && QUERYX_ENV=test queryx db:drop --schema mysql.hcl
cd internal/integration && QUERYX_ENV=test queryx db:create --schema mysql.hcl
cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema mysql.hcl
cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema mysql.hcl
cd internal/integration && QUERYX_ENV=test queryx generate --schema mysql.hcl
cd internal/integration && yarn tsc
cd internal/integration && yarn test
# cd internal/integration && go test ./...
cd internal/integration && queryx db:drop --schema mysql.hcl
cd internal/integration && queryx db:create --schema mysql.hcl
cd internal/integration && queryx db:migrate --schema mysql.hcl
cd internal/integration && queryx db:migrate --schema mysql.hcl
cd internal/integration && queryx generate --schema mysql.hcl
# cd internal/integration && yarn tsc
# cd internal/integration && yarn test
cd internal/integration && go test ./...

test-sqlite: install
rm -rf internal/integration/db
cd internal/integration && QUERYX_ENV=test queryx db:drop --schema sqlite.hcl
cd internal/integration && QUERYX_ENV=test queryx db:create --schema sqlite.hcl
cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema sqlite.hcl
cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema sqlite.hcl
cd internal/integration && QUERYX_ENV=test queryx generate --schema sqlite.hcl
cd internal/integration && yarn tsc
cd internal/integration && yarn test
# cd internal/integration && go test ./...
cd internal/integration && queryx db:drop --schema sqlite.hcl
cd internal/integration && queryx db:create --schema sqlite.hcl
cd internal/integration && queryx db:migrate --schema sqlite.hcl
cd internal/integration && queryx db:migrate --schema sqlite.hcl
cd internal/integration && queryx generate --schema sqlite.hcl
# cd internal/integration && yarn tsc
# cd internal/integration && yarn test
cd internal/integration && go test ./...

test: test-postgresql test-sqlite test-mysql
1 change: 1 addition & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ func NewAdapter(cfg *schema.Config) (Adapter, error) {
return NewSQLiteAdapter(config), nil
}

// TODO: list supported adapters
return nil, fmt.Errorf("unsupported adapter: %q", config.Adapter)
}
95 changes: 50 additions & 45 deletions generator/client/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package golang

import (
"embed"
"fmt"
"go/format"
"log"
"os"
"path"
"path/filepath"
"strings"
"text/template"

"github.com/swiftcarrot/queryx/generator"
"github.com/swiftcarrot/queryx/schema"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/imports"
"golang.org/x/mod/modfile"
)

//go:embed templates
Expand All @@ -20,7 +21,6 @@ var templates embed.FS
func Run(g *generator.Generator, generatorConfig *schema.Generator, args []string) error {
schema := g.Schema
database := schema.Databases[0]
dir := database.Name

if err := g.LoadTemplates(templates, database.Adapter); err != nil {
return err
Expand Down Expand Up @@ -52,20 +52,46 @@ func Run(g *generator.Generator, generatorConfig *schema.Generator, args []strin
}
g.Templates = templates

if err := g.Generate(); err != nil {
// TODO: wrap this in a function
cwd, err := os.Getwd()
if err != nil {
return err
}
roots := findModuleRoot(cwd)
f := filepath.Join(roots, "go.mod")
content, err := os.ReadFile(f)
if err != nil {
return err
}
mf, err := modfile.Parse("go.mod", content, nil)
if err != nil {
return err
}
rel, err := filepath.Rel(roots, cwd)
if err != nil {
return err
}
goModPath := path.Join(mf.Module.Mod.Path, rel)
goModPath = strings.ReplaceAll(goModPath, "\\", "/")

fmt.Println("Running goimports...")
if err := goimports(dir); err != nil {
if err := g.Generate(transform, goModPath); err != nil {
return err
}

return nil
}

func transform(b []byte) []byte {
b, err := format.Source(b)
if err != nil {
log.Fatal(err)
}
return b
}

func typesFromSchema(sch *schema.Schema) map[string]bool {
m := map[string]bool{}
// TODO: move to schema package
typs := []string{"string", "text", "boolean",
"date", "time", "datetime", "float",
"integer", "bigint", "json", "uuid"}
Expand All @@ -88,45 +114,24 @@ func typesFromSchema(sch *schema.Schema) map[string]bool {
return m
}

// run goimports for all go files in target directory
func goimports(dir string) error {
g := new(errgroup.Group)
g.SetLimit(20)

targets := []string{}
err := filepath.Walk(dir, func(target string, info os.FileInfo, err error) error {
if strings.HasSuffix(target, ".go") {
targets = append(targets, target)
}
return nil
})
if err != nil {
return err
}

for _, target := range targets {
func(target string) {
g.Go(func() error {
content, err := os.ReadFile(target)
if err != nil {
return err
}
src, err := imports.Process(target, content, nil)
if err != nil {
return err
}
if err := os.WriteFile(target, src, 0644); err != nil {
return err
}

return nil
})
}(target)
// findModuleRoot finds the module root by looking for go.mod file in the current directory and its parents.
// This function is copied from https://github.com/golang/go/blob/master/src/cmd/go/internal/modload/init.go
func findModuleRoot(dir string) (roots string) {
if dir == "" {
// TODO: add go mod init in docs
panic("dir not set") // TODO: improve this error message
}
dir = filepath.Clean(dir)

if err := g.Wait(); err != nil {
return err
for {
if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
return dir
}
d := filepath.Dir(dir)
if d == dir {
break
}
dir = d
}

return nil
return ""
}
13 changes: 7 additions & 6 deletions generator/client/golang/templates/[model].gotmpl
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
// Code generated by queryx, DO NOT EDIT.

package {{ .packageName }}
package {{ $.packageName }}

import (
"database/sql"
"time"
"strings"
"fmt"

"{{ $.goModPath }}/{{ $.packageName }}/queryx"
)

type {{ $.model.Name }} struct {
{{- range $c := .model.Columns }}
{{ pascal $c.Name }} {{ goModelType $c.Type $c.Null }} `json:"{{ camel $c.Name }}" db:"{{ $c.Name }}"`
{{- end }}
{{- range $b := $.model.BelongsTo}}
{{ $b.Name | pascal }} *{{ $b.ModelName }} `json:"{{ camel $b.Name }}"`
{{ $b.Name | pascal }} *{{ $b.ModelName }} `json:"{{ camel $b.Name }}"`
{{- end }}
{{- range $h := $.model.HasMany }}
{{ $h.Name | pascal }} []*{{ $h.ModelName }} `json:"{{ $h.Name | camel }}"`
{{ $h.Name | pascal }} []*{{ $h.ModelName }} `json:"{{ $h.Name | camel }}"`
{{- end }}
{{- range $h := $.model.HasOne }}
{{ $h.Name | pascal }} *{{ $h.ModelName }} `json:"{{ $h.Name | camel }}"`
{{ $h.Name | pascal }} *{{ $h.ModelName }} `json:"{{ $h.Name | camel }}"`
{{- end }}

schema *queryx.Schema
Expand Down
3 changes: 2 additions & 1 deletion generator/client/golang/templates/[model]_query.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"database/sql"
"errors"
"fmt"
"time"

"{{ $.goModPath }}/{{ $.packageName }}/queryx"
)

{{- define "primary_key_params" }}
Expand Down
4 changes: 4 additions & 0 deletions generator/client/golang/templates/queryx.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
package {{ $.packageName }}

import (
"context"
"database/sql"
"fmt"
"log"
"os"

"{{ $.goModPath }}/{{ $.packageName }}/queryx"
)

type Queries interface {
Expand Down
7 changes: 5 additions & 2 deletions generator/client/golang/templates/queryx/config.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
package queryx

import (
{{- if eq $.client.Adapter "mysql" }}
"net/url"
"strings"
"fmt"
{{- end }}
{{- if eq $.client.Adapter "sqlite" }}"strings"{{- end }}
)

type Config struct {
Expand All @@ -16,7 +19,7 @@ func NewConfig(env string) *Config {
{{- range $c := $.client.Configs }}
case "{{ $c.Environment }}":
return &Config{
URL: fixURL({{ if $c.URL.EnvKey }}os.Getenv("{{ $c.URL.EnvKey }}"){{ else }}"{{ $c.URL.Value }}"{{ end }}),
URL: fixURL({{ if $c.URL.EnvKey }}getenv("{{ $c.URL.EnvKey }}"){{ else }}"{{ $c.URL.Value }}"{{ end }}),
}
{{- end }}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package queryx

import (
"fmt"
"time"
)

type DateColumn struct {
Expand Down
2 changes: 1 addition & 1 deletion generator/client/golang/templates/queryx/date_test.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package queryx

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package queryx

import (
"fmt"
"time"
)

type DatetimeColumn struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package queryx

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down
7 changes: 7 additions & 0 deletions generator/client/golang/templates/queryx/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package queryx

import "os"

func getenv(k string) string {
return os.Getenv(k)
}
1 change: 1 addition & 0 deletions generator/client/golang/templates/queryx/time.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"database/sql/driver"
"encoding/json"
"time"
{{ if eq $.client.Adapter "mysql" }}"fmt"{{ end }}
)

type Time struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package queryx

import (
"fmt"
"time"
)


Expand Down
2 changes: 1 addition & 1 deletion generator/client/golang/templates/queryx/time_test.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package queryx

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down
6 changes: 5 additions & 1 deletion generator/client/typescript/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ func Run(g *generator.Generator, generatorConfig *schema.Generator, args []strin
return err
}

if err := g.Generate(); err != nil {
if err := g.Generate(transform, ""); err != nil {
return err
}

return nil
}

func transform(b []byte) []byte {
return b
}
Loading

0 comments on commit a0ec0d0

Please sign in to comment.