Skip to content

Commit

Permalink
Merge pull request #133 from alexj212/master
Browse files Browse the repository at this point in the history
- v0.9.26 (07/31/2020)
  • Loading branch information
alexj212 authored Jul 31, 2020
2 parents b0b5bcf + 8791243 commit a35317d
Show file tree
Hide file tree
Showing 16 changed files with 373 additions and 133 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,21 @@ test_sqlite3: ## test sqlite3 code generation



set_release: ## populate release info
./release.sh

gen_readme: ## generate readme file
gen_readme: set_release ## generate readme file
go run github.com/smallnest/gen/readme \
--sqltype=sqlite3 \
--connstr "./example/sample.db" \
--database main \
--table invoices


release: fmt gen install example gen_readme ## prepare release
release: gen_readme fmt gen install example ## prepare release
$(info ************ Release completed)

git_sync: ## git sync upstream
git fetch upstream
git checkout master
git merge upstream/maste
78 changes: 69 additions & 9 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _test/dbmeta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
goopt.Description = func() string {
return "ORM and RESTful meta data viewer for SQl databases"
}
goopt.Version = "v0.9.25 (07/26/2020)"
goopt.Version = "v0.9.26 (07/31/2020)"
goopt.Summary = `dbmeta [-v] --sqltype=mysql --connstr "user:password@/dbname" --database <databaseName>
sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]
Expand Down
4 changes: 2 additions & 2 deletions code_http.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func GetInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoices [post]
// echo '{"invoice_date": "2311-01-24T01:05:44.420640458-05:00","billing_postal_code": "EOINbzuauKmMHiaDymdYsvXDx","billing_country": "cqtEUexVoitsOvqknKNnhShPI","total": 0.932417421427433,"invoice_id": 50,"customer_id": 37,"billing_address": "UgIfqiLZTewHKFZZuTZDwdHpf","billing_city": "mWRUstvZAmHtOuvtsJzocfFFw","billing_state": "gInXWdcekISCnmaDBcsDRWcyB"}' | http POST "http://127.0.0.1:8080/invoices" X-Api-User:user123
// echo '{"billing_state": "USFzllBypZZJDjtbuDWBnWcrU","billing_country": "KgrvVzwkLcFjnchqPwVoHWkRT","billing_postal_code": "ScJIRKwUTRltasmLeQNMLxJYS","total": 0.03391519763659607,"customer_id": 66,"invoice_date": "2123-11-18T21:39:12.15674111-08:00","billing_address": "nPoieNSaqVDVMBQvfwSXcYdtM","billing_city": "ObTQBQeAscRjOcAkHOOKmkHXU","invoice_id": 7}' | http POST "http://127.0.0.1:8080/invoices" X-Api-User:user123
func AddInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx := initializeContext(r)
invoices := &model.Invoices{}
Expand Down Expand Up @@ -192,7 +192,7 @@ func AddInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoices/{argInvoiceID} [put]
// echo '{"invoice_date": "2311-01-24T01:05:44.420640458-05:00","billing_postal_code": "EOINbzuauKmMHiaDymdYsvXDx","billing_country": "cqtEUexVoitsOvqknKNnhShPI","total": 0.932417421427433,"invoice_id": 50,"customer_id": 37,"billing_address": "UgIfqiLZTewHKFZZuTZDwdHpf","billing_city": "mWRUstvZAmHtOuvtsJzocfFFw","billing_state": "gInXWdcekISCnmaDBcsDRWcyB"}' | http PUT "http://127.0.0.1:8080/invoices/1" X-Api-User:user123
// echo '{"billing_state": "USFzllBypZZJDjtbuDWBnWcrU","billing_country": "KgrvVzwkLcFjnchqPwVoHWkRT","billing_postal_code": "ScJIRKwUTRltasmLeQNMLxJYS","total": 0.03391519763659607,"customer_id": 66,"invoice_date": "2123-11-18T21:39:12.15674111-08:00","billing_address": "nPoieNSaqVDVMBQvfwSXcYdtM","billing_city": "ObTQBQeAscRjOcAkHOOKmkHXU","invoice_id": 7}' | http PUT "http://127.0.0.1:8080/invoices/1" X-Api-User:user123
func UpdateInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx := initializeContext(r)

Expand Down
11 changes: 11 additions & 0 deletions custom/custom.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
custom.md

{{ range $key, $value := . }}
{{ $desc := spew $value }}
{{ printf "%#-20v" $key }} {{ printf "[%T] %#v" $value $value }}{{ end }}






33 changes: 25 additions & 8 deletions custom/sample.gen
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"tables" [[]string] []string{"albums", "artists", "customers", "employees", "genres", "invoices", "invoice_items", "media_types", "playlists", "playlist_track", "tracks"}


GenerateTableFile(tableName, templateFilename, outputDirectory, outputFileName string, formatOutput bool)
GenerateFile(templateFilename, outputDirectory, outputFileName string, formatOutput bool) string
GenerateTableFile(tableInfos map[string]*ModelInfo, tableName, templateFilename, outputDirectory, outputFileName string, formatOutput bool)

"FmtFieldName": dbmeta.FmtFieldName,
"singular": inflection.Singular,
Expand All @@ -37,7 +36,13 @@ GenerateFile(templateFilename, outputDirectory, outputFileName string, formatOut


{{ range $key, $value := . }}
{{ printf "%#-20v" $key }} {{ printf "[%T] %#v" $value $value }}{{ end }}
{{ printf "%#-20v [%T] %#v" $key $value $value }}{{ end }}



func (c *Config) GenerateFile(templateFilename, outDir, outputDirectory, outputFileName string, formatOutput bool, overwrite bool) string {


*/}}


Expand All @@ -50,9 +55,21 @@ GenerateFile(templateFilename, outputDirectory, outputFileName string, formatOut
{{$snakeCase := toSnakeCase $table -}}
{{ printf "[%-2d] %-20s %-20s %-20s %-20s %-20s %-20s %-20s" $i $table $singular $plural $title $lower $lowerCamel $snakeCase}}{{- end }}


{{ range $i, $table := .tables }}
{{$name := toUpper $table -}}
{{ $i := 0 }}
{{ range $tableName , $table := .tableInfos }}
{{$i := inc }}
{{$name := toUpper $table.TableName -}}
{{$filename := printf "My%s.go" $name -}}
{{ printf "[%-2d] %-20s %-20s" $i $table $filename}}
{{ GenerateTableFile $table "custom.go.tmpl" "test" $filename true}}{{- end }}
{{ printf "[%-2d] %-20s %-20s" $i $name $filename}}
{{ GenerateTableFile $.tableInfos $table.TableName "custom.go.tmpl" "test" $filename true}}{{- end }}

{{ GenerateFile "custom.md.tmpl" "test" "custom.md" false false }}


{{ mkdir "test/alex/test/mkdir" }}
{{ touch "test/alex/test/mkdir/alex.txt" }}
{{ pwd }}
{{ copy "../_test" "test" }}



8 changes: 6 additions & 2 deletions dbmeta/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (c *Config) GetTemplate(genTemplate *GenTemplate) (*template.Template, erro
"replace": replace,
"hasField": hasField,
"FmtFieldName": FmtFieldName,
"copy": FileSystemCopy,
"mkdir": Mkdir,
"touch": Touch,
"pwd": Pwd,
}

baseName := filepath.Base(genTemplate.Name)
Expand Down Expand Up @@ -555,10 +559,10 @@ func Exists(name string) bool {
}

// GenerateFile generate file from template, non table used within templates
func (c *Config) GenerateFile(templateFilename, outDir, outputDirectory, outputFileName string, formatOutput bool, overwrite bool) string {
func (c *Config) GenerateFile(templateFilename, outputDirectory, outputFileName string, formatOutput bool, overwrite bool) string {
buf := bytes.Buffer{}
buf.WriteString(fmt.Sprintf("GenerateFile( %s, %s, %s)\n", templateFilename, outputDirectory, outputFileName))
fileOutDir := filepath.Join(outDir, outputDirectory)
fileOutDir := outputDirectory
err := os.MkdirAll(fileOutDir, 0777)
if err != nil && !overwrite {
buf.WriteString(fmt.Sprintf("unable to create fileOutDir: %s error: %v\n", fileOutDir, err))
Expand Down
47 changes: 47 additions & 0 deletions dbmeta/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package dbmeta
import (
"errors"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
"unicode"

filecopy "github.com/otiai10/copy"
)

// commonInitialisms is a set of common initialisms.
Expand Down Expand Up @@ -243,3 +247,46 @@ func Copy(dst interface{}, src interface{}) error {
func isZeroOfUnderlyingType(x interface{}) bool {
return x == nil || reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}

func FileSystemCopy(src, dst string) string {
err := filecopy.Copy(src, dst)
if err != nil {
return fmt.Sprintf("copy returned an error %v", err)

}
return fmt.Sprintf("copy %s %s", src, dst)
}
func Mkdir(dst string) string {
err := os.MkdirAll(dst, os.ModePerm)
if err != nil {
return fmt.Sprintf("mkdir returned an error %v", err)

}
return fmt.Sprintf("mkdir %s", dst)
}

func Touch(dst string) string {
_, err := os.Stat(dst)
if os.IsNotExist(err) {
file, err := os.Create(dst)
if err != nil {
return fmt.Sprintf("mkdir returned an error %v", err)
}
defer file.Close()
} else {
currentTime := time.Now().Local()
err = os.Chtimes(dst, currentTime, currentTime)
if err != nil {
return fmt.Sprintf("mkdir returned an error %v", err)
}
}
return fmt.Sprintf("touch %s", dst)
}

func Pwd() string {
currentWorkingDirectory, err := os.Getwd()
if err != nil {
return fmt.Sprintf("pwd returned an error %v", err)
}
return currentWorkingDirectory
}
16 changes: 6 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,35 @@ require (
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73
github.com/droundy/goopt v0.0.0-20170604162106-0b8effe182da
github.com/fzipp/gocyclo v0.0.0-20150627053110-6acd4345c835 // indirect
github.com/gin-gonic/gin v1.6.2 // indirect
github.com/gobuffalo/packd v1.0.0
github.com/gobuffalo/packr/v2 v2.8.0
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20200309095847-7953dde2c7bf // indirect
github.com/grpc-ecosystem/grpc-gateway v1.14.6 // indirect
github.com/guregu/null v3.4.0+incompatible // indirect
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
github.com/jimsmart/schema v0.0.4
github.com/jinzhu/gorm v1.9.11
github.com/jinzhu/inflection v1.0.0
github.com/julienschmidt/httprouter v1.2.0
github.com/karrick/godirwalk v1.15.6 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/lib/pq v1.3.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mwitkow/go-proto-validators v0.3.0 // indirect
github.com/ompluscator/dynamic-struct v1.2.0
github.com/otiai10/copy v1.2.0
github.com/rogpeppe/go-internal v1.6.0 // indirect
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/spf13/cobra v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 // indirect
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/mod v0.3.0 // indirect
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
golang.org/x/tools v0.0.0-20200711155855-7342f9734a7d // indirect
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1 // indirect
golang.org/x/tools v0.0.0-20200730200120-fe6bb45d2132 // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/genproto v0.0.0-20200608115520-7c474a2e3482 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
)
File renamed without changes.
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func init() {
goopt.Description = func() string {
return "ORM and RESTful API generator for SQl databases"
}
goopt.Version = "v0.9.25 (07/26/2020)"
goopt.Version = "v0.9.26 (07/31/2020)"
goopt.Summary = `gen [-v] --sqltype=mysql --connstr "user:password@/dbname" --database <databaseName> --module=example.com/example [--json] [--gorm] [--guregu] [--generate-dao] [--generate-proj]
git fetch up
sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]
Expand Down Expand Up @@ -453,6 +453,13 @@ func execTemplate(conf *dbmeta.Config, genTemplate *dbmeta.GenTemplate, data map
data["outDir"] = *outDir
data["Config"] = conf

tables := make([]string, 0, len(tableInfos))
for k := range tableInfos {
tables = append(tables, k)
}

data["tables"] = tables

rt, err := conf.GetTemplate(genTemplate)
if err != nil {
fmt.Print(au.Red(fmt.Sprintf("Error in loading %s template, error: %v\n", genTemplate.Name, err)))
Expand Down
4 changes: 2 additions & 2 deletions packrd/packed-packr.go

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion readme/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
goopt.Description = func() string {
return "ORM and RESTful meta data viewer for SQl databases"
}
goopt.Version = "v0.9.25 (07/26/2020)"
goopt.Version = "v0.9.26 (07/31/2020)"
goopt.Summary = `dbmeta [-v] --sqltype=mysql --connstr "user:password@/dbname" --database <databaseName>
sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]
Expand Down Expand Up @@ -134,9 +134,25 @@ func genreadme(conf *dbmeta.Config, templateName, outputFile string, ctx map[str
{{- end }}
`
ctx["AdvancesSample"] = sample

releaseHistory := loadFile("release.history")
ctx["ReleaseHistory"] = releaseHistory
conf.WriteTemplate(template, ctx, outputFile, false)
}

func loadFile(src string) string {
// Read entire file content, giving us little control but
// making it very simple. No need to close the file.
content, err := ioutil.ReadFile(src)
if err != nil {
return fmt.Sprintf("error loading %s error: %v", src, err)
}

// Convert []byte to string and print to screen
text := string(content)
return text
}

func initialize(conf *dbmeta.Config) {
outDir := "."
module := "github.com/alexj212/test"
Expand Down
93 changes: 93 additions & 0 deletions release.history
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
- v0.9.26 (07/31/2020)
- Release scripting
- Added custom script functions to copy, mkdir, touch, pwd
- Fixed custom script exec example
- v0.9.25 (07/26/2020)
- Adhere json-fmt flag for all JSON response so when camel or lower_camel is specified, fields name in GetAll variant and DDL info will also have the same name format
- Fix: Build information embedded through linker in Makefile is not consistent with the variable defined in main file.
- Added --scheme and --listen options. This allows compiled binary to be used behind reverse proxy.
- In addition, template for generating URL was fixed, i.e. when PORT is 80, then PORT is omitted from URL segment.
- v0.9.24 (07/13/2020)
- Fixed array bounds issue parsing mysql db meta
- v0.9.23 (07/10/2020)
- Added postgres types: bigserial, serial, smallserial, bigserial, float4 to mapping.json
- v0.9.22 (07/08/2020)
- Modified gogo.proto check to use GOPATH not hardcoded.
- Updated gen to error exit on first error encountered
- Added color output for error
- Added --no-color option for non colorized output
- v0.9.21 (07/07/2020)
- Repacking templates, update version number in info.
- v0.9.20 (07/07/2020)
- Fixed render error in router.go.tmpl
- upgraded project to use go.mod 1.14
- v0.9.19 (07/07/2020)
- Added --windows flag to write files with CRLF windows line endings, otherwise they are all unix based LF line endings
- v0.9.18 (06/30/2020)
- Fixed naming in templates away from hard coded model package.
- v0.9.17 (06/30/2020)
- Refactored template loading, to better report error in template
- Added option to run gofmt on output directory
- v0.9.16 (06/29/2020)
- Fixes to router.go.tmpl from calvinchengx
- Added postgres db support for inet and timestamptz
- v0.9.15 (06/23/2020)
- Code cleanup using gofmt name suggestions.
- Template updates for generated code cleanup using gofmt name suggestions.
- v0.9.14 (06/23/2020)
- Added model comment on field line if available from database.
- Added exposing TableInfo via api call.
- v0.9.13 (06/22/2020)
- fixed closing of connections via defer
- bug fixes in sqlx generated code
- v0.9.12 (06/14/2020)
- SQLX changed MustExec to Exec and checking/returning error
- Updated field renaming if duplicated, need more elegant renaming solution.
- Added exclude to test.sh
- v0.9.11 (06/13/2020)
- Added ability to pass field, model and file naming format
- updated test scripts
- Fixed sqlx sql query placeholders
- v0.9.10 (06/11/2020)
- Bug fix with retrieving varchar length from mysql
- Added support for mysql unsigned decimal - maps to float
- v0.9.9 (06/11/2020)
- Fixed issue with mysql and table named `order`
- Fixed internals in GetAll generation in gorm and sqlx.
- v0.9.8 (06/10/2020)
- Added ability to set file naming convention for models, dao, apis and grpc `--file_naming={{.}}`
- Added ability to set struct naming convention `--model_naming={{.}}`
- Fixed bug with Makefile generation removing quoted conn string in `make regen`
- v0.9.7 (06/09/2020)
- Added grpc server generation - WIP (looking for code improvements)
- Added ability to exclude tables
- Added support for unsigned from mysql ddl.
- v0.9.6 (06/08/2020)
- Updated SQLX codegen
- Updated templates to split code gen functions into seperate files
- Added code_dao_gorm, code_dao_sqlx to be generated from templates
- v0.9.5 (05/16/2020)
- Added SQLX codegen by default, split dao templates.
- Renamed templates
- v0.9.4 (05/15/2020)
- Documentation updates, samples etc.
- v0.9.3 (05/14/2020)
- Template bug fixes, when using custom api, dao and model package.
- Set primary key if not set to the first column
- Skip code gen if primary key column is not int or string
- validated codegen for mysql, mssql, postgres and sqlite3
- Fixed file naming if table ends with _test.go renames to _tst.go
- Fix for duplicate field names in struct due to renaming
- Added Notes for columns and tables for situations where a primary key is set since not defined in db
- Fixed issue when model contained field that had were named the same as funcs within model.
- v0.9.2 (05/12/2020)
- Code cleanup gofmt, etc.
- v0.9.1 (05/12/2020)
- v0.9 (05/12/2020)
- updated db meta data loading fetching default values
- added default value to GORM tags
- Added protobuf .proto generation
- Added test app to display meta data
- Cleanup DDL generation
- Added support for varchar2, datetime2, float8, USER_DEFINED
- v0.5
Loading

0 comments on commit a35317d

Please sign in to comment.