-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(sequelie): add `sequelie.Sql` type * feat(sequelie): add golang codegen * feat(readme): update todo * feat(cli): adds the codegen cli * feat(readme): add information about the cli * feat(readme): add table of contents * feat(gitignore): ignore .exe files * feat(gitignore): ignore binaries * feat(examples): update examples * feat(readme): replace `GetAndTransform` for `Get().Interpolate()`
- Loading branch information
1 parent
a1ae0ad
commit 272a752
Showing
12 changed files
with
250 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
.idea/ | ||
.idea/ | ||
.sequelie/ | ||
*.exe | ||
.binaries/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
"log" | ||
"os" | ||
"sequelie" | ||
) | ||
|
||
func main() { | ||
app := &cli.App{ | ||
Name: "sequelie", | ||
Description: "Off-loading your SQL queries from your Golang code, now includes codegen!", | ||
Commands: []*cli.Command{ | ||
{ | ||
Name: "generate", | ||
Aliases: []string{"g"}, | ||
Description: "Generates Golang files containing SQL queries from Sequelie.", | ||
Action: func(context *cli.Context) error { | ||
dir := context.String("directory") | ||
if dir == "" { | ||
dir = "./" | ||
} | ||
if err := sequelie.ReadDirectory(dir); err != nil { | ||
return err | ||
} | ||
if err := sequelie.Generate(); err != nil { | ||
return err | ||
} | ||
log.Println("[SQL] Generated all Golang files under the \".sequelie\" directory.") | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "directory", | ||
Aliases: []string{"d"}, | ||
Usage: "Reads and generates all the Sequelie files from the specific directory (recursive).", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
if err := app.Run(os.Args); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package sequelie | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"github.com/dave/jennifer/jen" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
"os" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var titleCaser = cases.Title(language.AmericanEnglish, cases.NoLower) | ||
var importSequelie = []byte("import \"sequelie\"") | ||
|
||
func renderQuery(key string, q *Query) string { | ||
separatorIndex := strings.Index(key, ".") | ||
if separatorIndex == -1 { | ||
panic("cannot find the separator for the key") | ||
} | ||
token := key[separatorIndex+1:] | ||
token = strings.ReplaceAll(token, "_", " ") | ||
token = titleCaser.String(token) | ||
token = strings.ReplaceAll(token, " ", "") | ||
|
||
return jen.Var(). | ||
Id(token+"Query"). | ||
Op("="). | ||
Qual("sequelie", "Query"). | ||
Call(jen.Lit(q.String())). | ||
GoString() | ||
} | ||
|
||
func getPackage(key string) string { | ||
separatorIndex := strings.Index(key, ".") | ||
if separatorIndex == -1 { | ||
panic("cannot find the separator for the key") | ||
} | ||
return strings.ToLower(key[:separatorIndex]) | ||
} | ||
|
||
func flatten() map[string][][]byte { | ||
var t = make(map[string][][]byte) | ||
for k, v := range store { | ||
pkg := getPackage(k) | ||
t[pkg] = append(t[pkg], []byte(renderQuery(k, v))) | ||
} | ||
return t | ||
} | ||
|
||
func render(pkg string, declarations [][]byte) []byte { | ||
t := [][]byte{ | ||
[]byte("package " + pkg), | ||
{}, | ||
importSequelie, | ||
{}, | ||
} | ||
t = append(t, declarations...) | ||
return bytes.Join(t, newLineBytes) | ||
} | ||
|
||
func Generate() error { | ||
t := flatten() | ||
|
||
var waitGroup sync.WaitGroup | ||
var errs []error | ||
|
||
for pkg, declarations := range t { | ||
pkg := pkg | ||
declarations := declarations | ||
|
||
waitGroup.Add(1) | ||
go func() { | ||
defer waitGroup.Done() | ||
rendered := render(pkg, declarations) | ||
err := os.MkdirAll(".sequelie/"+pkg, os.ModePerm) | ||
if err != nil { | ||
errs = append(errs, err) | ||
return | ||
} | ||
f, err := os.Create(".sequelie/" + pkg + "/queries.go") | ||
if err != nil { | ||
errs = append(errs, err) | ||
return | ||
} | ||
defer func(f *os.File) { | ||
err := f.Close() | ||
if err != nil { | ||
errs = append(errs, err) | ||
return | ||
} | ||
}(f) | ||
_, err = f.Write(rendered) | ||
if err != nil { | ||
errs = append(errs, err) | ||
return | ||
} | ||
}() | ||
} | ||
waitGroup.Wait() | ||
if len(errs) > 0 { | ||
return errors.Join(errs...) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
module sequelie | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect | ||
github.com/dave/jennifer v1.6.1 // indirect | ||
github.com/russross/blackfriday/v2 v2.1.0 // indirect | ||
github.com/urfave/cli/v2 v2.25.6 // indirect | ||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect | ||
golang.org/x/text v0.10.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.