Skip to content

Commit

Permalink
added fail fast on error in code gen
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Jeannopoulos committed Jul 8, 2020
1 parent a07fbce commit 9f7de03
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 82 deletions.
11 changes: 8 additions & 3 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.21 (07/07/2020)"
goopt.Version = "v0.9.22 (07/08/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 '{"customer_id": 24,"billing_address": "rNkjAsJjDKZGnsBxyUdpYvOkx","total": 0.37826635766312366,"billing_postal_code": "rNFjBOckDaCuciKulxAGnQGcj","invoice_id": 93,"invoice_date": "2255-08-11T14:38:49.47073267-05:00","billing_city": "fKvdAmQUufQYkjzsZhXrhWhqP","billing_state": "XdpPhuBIMnWbSRnmstifsKwvI","billing_country": "QdCWUxaOiascxToHVCZxnSPfO"}' | http POST "http://127.0.0.1:8080/invoices" X-Api-User:user123
// echo '{"invoice_id": 57,"customer_id": 9,"billing_address": "rMHppATYFwjWxvjTGASkYSPkf","billing_state": "HIwKVtaMVGnQJHqXTJdibIzeA","billing_country": "ZHahDVUYbQzFAbJQkRbRsAPQy","total": 0.4534626433499875,"invoice_date": "2038-10-09T12:04:09.946815184-05:00","billing_city": "zNFsMoKCBYsOawVubnYRDvcmX","billing_postal_code": "BYylGmiCSWSFgfxRsSogPbKxg"}' | 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} [patch]
// echo '{"customer_id": 24,"billing_address": "rNkjAsJjDKZGnsBxyUdpYvOkx","total": 0.37826635766312366,"billing_postal_code": "rNFjBOckDaCuciKulxAGnQGcj","invoice_id": 93,"invoice_date": "2255-08-11T14:38:49.47073267-05:00","billing_city": "fKvdAmQUufQYkjzsZhXrhWhqP","billing_state": "XdpPhuBIMnWbSRnmstifsKwvI","billing_country": "QdCWUxaOiascxToHVCZxnSPfO"}' | http PUT "http://127.0.0.1:8080/invoices/1" X-Api-User:user123
// echo '{"invoice_id": 57,"customer_id": 9,"billing_address": "rMHppATYFwjWxvjTGASkYSPkf","billing_state": "HIwKVtaMVGnQJHqXTJdibIzeA","billing_country": "ZHahDVUYbQzFAbJQkRbRsAPQy","total": 0.4534626433499875,"invoice_date": "2038-10-09T12:04:09.946815184-05:00","billing_city": "zNFsMoKCBYsOawVubnYRDvcmX","billing_postal_code": "BYylGmiCSWSFgfxRsSogPbKxg"}' | 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
24 changes: 12 additions & 12 deletions dbmeta/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func (c *Config) GenerateTableFile(tableInfos map[string]*ModelInfo, tableName,

outputFile := filepath.Join(fileOutDir, outputFileName)
buf.WriteString(fmt.Sprintf("Writing %s -> %s\n", templateFilename, outputFile))
c.WriteTemplate(tpl, data, outputFile, formatOutput)
err = c.WriteTemplate(tpl, data, outputFile, formatOutput)
return buf.String()
}

Expand Down Expand Up @@ -439,10 +439,10 @@ func (c *Config) CreateContextForTableFile(tableInfo *ModelInfo) map[string]inte
}

// WriteTemplate write a template out
func (c *Config) WriteTemplate(genTemplate *GenTemplate, data map[string]interface{}, outputFile string, formatOutput bool) {
func (c *Config) WriteTemplate(genTemplate *GenTemplate, data map[string]interface{}, outputFile string, formatOutput bool) error{
if !c.Overwrite && Exists(outputFile) {
fmt.Printf("not overwriting %s\n", outputFile)
return
return nil
}

for key, value := range c.ContextMap {
Expand Down Expand Up @@ -471,21 +471,18 @@ func (c *Config) WriteTemplate(genTemplate *GenTemplate, data map[string]interfa

rt, err := c.GetTemplate(genTemplate)
if err != nil {
fmt.Printf("Error in loading %s template, error: %v\n", genTemplate.Name, err)
return
return fmt.Errorf("Error in loading %s template, error: %v\n", genTemplate.Name, err)
}
var buf bytes.Buffer
err = rt.Execute(&buf, data)
if err != nil {
fmt.Printf("Error in rendering %s: %s\n", genTemplate.Name, err.Error())
return
return fmt.Errorf("Error in rendering %s: %s\n", genTemplate.Name, err.Error())
}

if formatOutput {
formattedSource, err := format.Source(buf.Bytes())
if err != nil {
fmt.Printf("Error in formatting template: %s outputfile: %s source: %s\n", genTemplate.Name, outputFile, err.Error())
formattedSource = buf.Bytes()
return fmt.Errorf("Error in formatting template: %s outputfile: %s source: %s\n", genTemplate.Name, outputFile, err.Error())
}

fileContents := NormalizeNewlines(formattedSource)
Expand All @@ -504,13 +501,13 @@ func (c *Config) WriteTemplate(genTemplate *GenTemplate, data map[string]interfa
}

if err != nil {
fmt.Printf("error writing %s - error: %v\n", outputFile, err)
return
return fmt.Errorf("error writing %s - error: %v\n", outputFile, err)
}

if c.Verbose {
fmt.Printf("writing %s\n", outputFile)
}
return nil
}

// NormalizeNewlines normalizes \r\n (windows) and \r (mac)
Expand Down Expand Up @@ -561,7 +558,10 @@ func (c *Config) GenerateFile(templateFilename, outDir, outputDirectory, outputF

outputFile := filepath.Join(fileOutDir, outputFileName)
buf.WriteString(fmt.Sprintf("Writing %s -> %s\n", templateFilename, outputFile))
c.WriteTemplate(tpl, data, outputFile, formatOutput)
err = c.WriteTemplate(tpl, data, outputFile, formatOutput)
if err != nil {
buf.WriteString(fmt.Sprintf("Error calling WriteTemplate %s -> %v\n", templateFilename, err))
}
return buf.String()
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
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 // indirect
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
Expand Down
Loading

0 comments on commit 9f7de03

Please sign in to comment.