Skip to content

Commit 54c3d1b

Browse files
authored
Merge pull request #25 from kanmu/michiomochi/replace-to-withstack
Replace errors.Wrap to errors.WithStack
2 parents 7d2e598 + 71537b9 commit 54c3d1b

File tree

5 files changed

+39
-42
lines changed

5 files changed

+39
-42
lines changed

README.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@
88

99
`dgw` generates Golang struct, and simple Table/Row Data Gateway functions from PostgreSQL table metadata. Heavily inspired by [xo](https://github.com/knq/xo).
1010

11-
1211
## Why created
1312

1413
Personally, I prefer Table/Row Data Gateway over ORM/Query Builder approach when using Go with RDBMS. However, it is very time consuming, tedious, and error-prone to write a lot of columns, query place holders, and struct fields that all have to be exactly in order even for a simple select/insert statement. `dgw` generate Go struct, and simple functions to get/create row from PostgreSQL table definitions to avoid manually writing simple but tedious SQL.
1514

1615
- `dgw` can properly detect autogenerated column (e.g. serial, bigserial), and composit primary key to create appropriate SQL.
1716
- `dgw` has ability to easily customize PostgreSQL column type <-> Go type mapping using toml config file.
1817

19-
2018
## Installation
2119

2220
```
2321
brew install kanmu/tools/dgw
2422
```
2523

26-
2724
## How to use
2825

2926
```
@@ -108,7 +105,7 @@ func (r *T1) Create(db Queryer) error {
108105
`INSERT INTO t1 (i, str, num_float, nullable_str, t_with_tz, t_without_tz, nullable_tz, json_data, xml_data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING id`,
109106
&r.I, &r.Str, &r.NumFloat, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.NullableTz, &r.JSONData, &r.XMLData).Scan(&r.ID)
110107
if err != nil {
111-
return errors.Wrap(err, "failed to insert t1")
108+
return errors.WithStack(err)
112109
}
113110
return nil
114111
}
@@ -120,7 +117,7 @@ func GetT1ByPk(db Queryer, pk0 int64) (*T1, error) {
120117
`SELECT id, i, str, num_float, nullable_str, t_with_tz, t_without_tz, nullable_tz, json_data, xml_data FROM t1 WHERE id = $1`,
121118
pk0).Scan(&r.ID, &r.I, &r.Str, &r.NumFloat, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.NullableTz, &r.JSONData, &r.XMLData)
122119
if err != nil {
123-
return nil, errors.Wrap(err, "failed to select t1")
120+
return nil, errors.WithStack(err)
124121
}
125122
return &r, nil
126123
}
@@ -140,7 +137,7 @@ func (r *T2) Create(db Queryer) error {
140137
`INSERT INTO t2 (str, t_with_tz, t_without_tz) VALUES ($1, $2, $3) RETURNING id, i`,
141138
&r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID, &r.I)
142139
if err != nil {
143-
return errors.Wrap(err, "failed to insert t2")
140+
return errors.WithStack(err)
144141
}
145142
return nil
146143
}
@@ -152,7 +149,7 @@ func GetT2ByPk(db Queryer, pk0 int64, pk1 int) (*T2, error) {
152149
`SELECT id, i, str, t_with_tz, t_without_tz FROM t2 WHERE id = $1 AND i = $2`,
153150
pk0, pk1).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz)
154151
if err != nil {
155-
return nil, errors.Wrap(err, "failed to select t2")
152+
return nil, errors.WithStack(err)
156153
}
157154
return &r, nil
158155
}
@@ -169,7 +166,7 @@ func (r *T3) Create(db Queryer) error {
169166
`INSERT INTO t3 (id, i) VALUES ($1, $2)`,
170167
&r.ID, &r.I)
171168
if err != nil {
172-
return errors.Wrap(err, "failed to insert t3")
169+
return errors.WithStack(err)
173170
}
174171
return nil
175172
}
@@ -181,7 +178,7 @@ func GetT3ByPk(db Queryer, pk0 int, pk1 int) (*T3, error) {
181178
`SELECT id, i FROM t3 WHERE id = $1 AND i = $2`,
182179
pk0, pk1).Scan(&r.ID, &r.I)
183180
if err != nil {
184-
return nil, errors.Wrap(err, "failed to select t3")
181+
return nil, errors.WithStack(err)
185182
}
186183
return &r, nil
187184
}

dgw.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Queryer interface {
3232
func OpenDB(connStr string) (*sql.DB, error) {
3333
conn, err := sql.Open("postgres", connStr)
3434
if err != nil {
35-
return nil, errors.Wrap(err, "failed to connect to database")
35+
return nil, errors.WithStack(err)
3636
}
3737
return conn, nil
3838
}
@@ -180,7 +180,7 @@ type StructField struct {
180180
func PgLoadTypeMapFromFile(filePath string) (*PgTypeMapConfig, error) {
181181
var conf PgTypeMapConfig
182182
if _, err := toml.DecodeFile(filePath, &conf); err != nil {
183-
return nil, errors.Wrap(err, "faild to parse config file")
183+
return nil, errors.WithStack(err)
184184
}
185185
return &conf, nil
186186
}
@@ -189,7 +189,7 @@ func PgLoadTypeMapFromFile(filePath string) (*PgTypeMapConfig, error) {
189189
func PgLoadColumnDef(db Queryer, schema string, table string) ([]*PgColumn, error) {
190190
colDefs, err := db.Query(pgLoadColumnDef, schema, table)
191191
if err != nil {
192-
return nil, errors.Wrap(err, "failed to load table def")
192+
return nil, errors.WithStack(err)
193193
}
194194

195195
cols := []*PgColumn{}
@@ -205,7 +205,7 @@ func PgLoadColumnDef(db Queryer, schema string, table string) ([]*PgColumn, erro
205205
&c.DDLType,
206206
)
207207
if err != nil {
208-
return nil, errors.Wrap(err, "failed to scan")
208+
return nil, errors.WithStack(err)
209209
}
210210

211211
// Some data types have an extra part e.g, "character varying(16)" and
@@ -223,7 +223,7 @@ func PgLoadColumnDef(db Queryer, schema string, table string) ([]*PgColumn, erro
223223
func PgLoadTableDef(db Queryer, schema string) ([]*PgTable, error) {
224224
tbDefs, err := db.Query(pgLoadTableDef, schema)
225225
if err != nil {
226-
return nil, errors.Wrap(err, "failed to load table def")
226+
return nil, errors.WithStack(err)
227227
}
228228
tbs := []*PgTable{}
229229
for tbDefs.Next() {
@@ -233,7 +233,7 @@ func PgLoadTableDef(db Queryer, schema string) ([]*PgTable, error) {
233233
&t.Name,
234234
)
235235
if err != nil {
236-
return nil, errors.Wrap(err, "failed to scan")
236+
return nil, errors.WithStack(err)
237237
}
238238
cols, err := PgLoadColumnDef(db, schema, t.Name)
239239
if err != nil {
@@ -291,7 +291,7 @@ func PgTableToStruct(t *PgTable, typeCfg *PgTypeMapConfig, keyConfig *AutoKeyMap
291291
for _, c := range t.Columns {
292292
f, err := PgColToField(c, typeCfg)
293293
if err != nil {
294-
return nil, errors.Wrap(err, "faield to convert col to field")
294+
return nil, errors.WithStack(err)
295295
}
296296
fs = append(fs, f)
297297
}
@@ -304,11 +304,11 @@ func PgExecuteDefaultTmpl(st *StructTmpl, path string) ([]byte, error) {
304304
var src []byte
305305
d, err := Asset(path)
306306
if err != nil {
307-
return src, errors.Wrap(err, "failed to load asset")
307+
return src, errors.WithStack(err)
308308
}
309309
tpl, err := template.New("struct").Funcs(tmplFuncMap).Parse(string(d))
310310
if err != nil {
311-
return src, errors.Wrap(err, "failed to parse template")
311+
return src, errors.WithStack(err)
312312
}
313313
buf := new(bytes.Buffer)
314314
if err := tpl.Execute(buf, st); err != nil {
@@ -326,7 +326,7 @@ func PgExecuteCustomTmpl(st *StructTmpl, customTmpl string) ([]byte, error) {
326326
var src []byte
327327
tpl, err := template.New("struct").Funcs(tmplFuncMap).Parse(customTmpl)
328328
if err != nil {
329-
return src, errors.Wrap(err, "failed to parse template")
329+
return src, errors.WithStack(err)
330330
}
331331
buf := new(bytes.Buffer)
332332
if err := tpl.Execute(buf, st); err != nil {
@@ -348,12 +348,12 @@ func PgCreateStruct(
348348

349349
tbls, err := PgLoadTableDef(db, schema)
350350
if err != nil {
351-
return src, errors.Wrap(err, "faield to load table definitions")
351+
return src, errors.WithStack(err)
352352
}
353353
cfg := &PgTypeMapConfig{}
354354
if typeMapPath == "" {
355355
if _, err := toml.Decode(typeMap, cfg); err != nil {
356-
return src, errors.Wrap(err, "faield to read type map")
356+
return src, errors.WithStack(err)
357357
}
358358
} else {
359359
if _, err := toml.DecodeFile(typeMapPath, cfg); err != nil {
@@ -366,7 +366,7 @@ func PgCreateStruct(
366366
}
367367
st, err := PgTableToStruct(tbl, cfg, autoGenKeyCfg)
368368
if err != nil {
369-
return src, errors.Wrap(err, "faield to convert table definition to struct")
369+
return src, errors.WithStack(err)
370370
}
371371
if customTmpl != "" {
372372
tmpl, err := ioutil.ReadFile(customTmpl)
@@ -375,17 +375,17 @@ func PgCreateStruct(
375375
}
376376
s, err := PgExecuteCustomTmpl(&StructTmpl{Struct: st}, string(tmpl))
377377
if err != nil {
378-
return nil, errors.Wrap(err, "PgExecuteCustomTmpl failed")
378+
return nil, errors.WithStack(err)
379379
}
380380
src = append(src, s...)
381381
} else {
382382
s, err := PgExecuteDefaultTmpl(&StructTmpl{Struct: st}, "template/struct.tmpl")
383383
if err != nil {
384-
return src, errors.Wrap(err, "faield to execute template")
384+
return src, errors.WithStack(err)
385385
}
386386
m, err := PgExecuteDefaultTmpl(&StructTmpl{Struct: st}, "template/method.tmpl")
387387
if err != nil {
388-
return src, errors.Wrap(err, "faield to execute template")
388+
return src, errors.WithStack(err)
389389
}
390390
src = append(src, s...)
391391
src = append(src, m...)

example/conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func OpenDB(connStr string) (*sql.DB, error) {
1212
conn, err := sql.Open("postgres", connStr)
1313
if err != nil {
14-
return nil, errors.Wrap(err, "failed to connect to database")
14+
return nil, errors.WithStack(err)
1515
}
1616
return conn, nil
1717
}

example/defaultstruct.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (r *T1) Create(db Queryer) error {
2727
`INSERT INTO t1 (i, str, num_float, nullable_str, t_with_tz, t_without_tz, nullable_tz, json_data, xml_data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING id`,
2828
&r.I, &r.Str, &r.NumFloat, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.NullableTz, &r.JSONData, &r.XMLData).Scan(&r.ID)
2929
if err != nil {
30-
return errors.Wrap(err, "failed to insert t1")
30+
return errors.WithStack(err)
3131
}
3232
return nil
3333
}
@@ -39,7 +39,7 @@ func GetT1ByPk(db Queryer, pk0 int64) (*T1, error) {
3939
`SELECT id, i, str, num_float, nullable_str, t_with_tz, t_without_tz, nullable_tz, json_data, xml_data FROM t1 WHERE id = $1`,
4040
pk0).Scan(&r.ID, &r.I, &r.Str, &r.NumFloat, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.NullableTz, &r.JSONData, &r.XMLData)
4141
if err != nil {
42-
return nil, errors.Wrap(err, "failed to select t1")
42+
return nil, errors.WithStack(err)
4343
}
4444
return &r, nil
4545
}
@@ -59,7 +59,7 @@ func (r *T2) Create(db Queryer) error {
5959
`INSERT INTO t2 (str, t_with_tz, t_without_tz) VALUES ($1, $2, $3) RETURNING id, i`,
6060
&r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID, &r.I)
6161
if err != nil {
62-
return errors.Wrap(err, "failed to insert t2")
62+
return errors.WithStack(err)
6363
}
6464
return nil
6565
}
@@ -71,7 +71,7 @@ func GetT2ByPk(db Queryer, pk0 int64, pk1 int) (*T2, error) {
7171
`SELECT id, i, str, t_with_tz, t_without_tz FROM t2 WHERE id = $1 AND i = $2`,
7272
pk0, pk1).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz)
7373
if err != nil {
74-
return nil, errors.Wrap(err, "failed to select t2")
74+
return nil, errors.WithStack(err)
7575
}
7676
return &r, nil
7777
}
@@ -88,7 +88,7 @@ func (r *T3) Create(db Queryer) error {
8888
`INSERT INTO t3 (id, i) VALUES ($1, $2)`,
8989
&r.ID, &r.I)
9090
if err != nil {
91-
return errors.Wrap(err, "failed to insert t3")
91+
return errors.WithStack(err)
9292
}
9393
return nil
9494
}
@@ -100,7 +100,7 @@ func GetT3ByPk(db Queryer, pk0 int, pk1 int) (*T3, error) {
100100
`SELECT id, i FROM t3 WHERE id = $1 AND i = $2`,
101101
pk0, pk1).Scan(&r.ID, &r.I)
102102
if err != nil {
103-
return nil, errors.Wrap(err, "failed to select t3")
103+
return nil, errors.WithStack(err)
104104
}
105105
return &r, nil
106106
}
@@ -117,7 +117,7 @@ func (r *T4) Create(db Queryer) error {
117117
`INSERT INTO t4 (id, i) VALUES ($1, $2)`,
118118
&r.ID, &r.I)
119119
if err != nil {
120-
return errors.Wrap(err, "failed to insert t4")
120+
return errors.WithStack(err)
121121
}
122122
return nil
123123
}
@@ -129,7 +129,7 @@ func GetT4ByPk(db Queryer, pk0 int, pk1 int) (*T4, error) {
129129
`SELECT id, i FROM t4 WHERE id = $1 AND i = $2`,
130130
pk0, pk1).Scan(&r.ID, &r.I)
131131
if err != nil {
132-
return nil, errors.Wrap(err, "failed to select t4")
132+
return nil, errors.WithStack(err)
133133
}
134134
return &r, nil
135135
}
@@ -148,7 +148,7 @@ func (r *UserAccount) Create(db Queryer) error {
148148
`INSERT INTO user_account (email, last_name, first_name) VALUES ($1, $2, $3) RETURNING id`,
149149
&r.Email, &r.LastName, &r.FirstName).Scan(&r.ID)
150150
if err != nil {
151-
return errors.Wrap(err, "failed to insert user_account")
151+
return errors.WithStack(err)
152152
}
153153
return nil
154154
}
@@ -160,7 +160,7 @@ func GetUserAccountByPk(db Queryer, pk0 int64) (*UserAccount, error) {
160160
`SELECT id, email, last_name, first_name FROM user_account WHERE id = $1`,
161161
pk0).Scan(&r.ID, &r.Email, &r.LastName, &r.FirstName)
162162
if err != nil {
163-
return nil, errors.Wrap(err, "failed to select user_account")
163+
return nil, errors.WithStack(err)
164164
}
165165
return &r, nil
166166
}
@@ -179,7 +179,7 @@ func (r *UserAccountCompositePk) Create(db Queryer) error {
179179
`INSERT INTO user_account_composite_pk (id, email, last_name, first_name) VALUES ($1, $2, $3, $4)`,
180180
&r.ID, &r.Email, &r.LastName, &r.FirstName)
181181
if err != nil {
182-
return errors.Wrap(err, "failed to insert user_account_composite_pk")
182+
return errors.WithStack(err)
183183
}
184184
return nil
185185
}
@@ -191,7 +191,7 @@ func GetUserAccountCompositePkByPk(db Queryer, pk0 int64, pk1 string) (*UserAcco
191191
`SELECT id, email, last_name, first_name FROM user_account_composite_pk WHERE id = $1 AND email = $2`,
192192
pk0, pk1).Scan(&r.ID, &r.Email, &r.LastName, &r.FirstName)
193193
if err != nil {
194-
return nil, errors.Wrap(err, "failed to select user_account_composite_pk")
194+
return nil, errors.WithStack(err)
195195
}
196196
return &r, nil
197197
}
@@ -210,7 +210,7 @@ func (r *UserAccountUUID) Create(db Queryer) error {
210210
`INSERT INTO user_account_uuid (email, last_name, first_name) VALUES ($1, $2, $3) RETURNING uuid`,
211211
&r.Email, &r.LastName, &r.FirstName).Scan(&r.UUID)
212212
if err != nil {
213-
return errors.Wrap(err, "failed to insert user_account_uuid")
213+
return errors.WithStack(err)
214214
}
215215
return nil
216216
}
@@ -222,7 +222,7 @@ func GetUserAccountUUIDByPk(db Queryer, pk0 string) (*UserAccountUUID, error) {
222222
`SELECT uuid, email, last_name, first_name FROM user_account_uuid WHERE uuid = $1`,
223223
pk0).Scan(&r.UUID, &r.Email, &r.LastName, &r.FirstName)
224224
if err != nil {
225-
return nil, errors.Wrap(err, "failed to select user_account_uuid")
225+
return nil, errors.WithStack(err)
226226
}
227227
return &r, nil
228228
}

template/method.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (r *{{ .Struct.Name }}) CreateContext(ctx context.Context, db Queryer) erro
2020
{{ createInsertParams .Struct }})
2121
{{- end }}
2222
if err != nil {
23-
return errors.Wrap(err, "failed to insert {{ .Struct.Table.Name }}")
23+
return errors.WithStack(err)
2424
}
2525
return nil
2626
}
@@ -32,7 +32,7 @@ func Get{{ .Struct.Name }}ByPkContext(ctx context.Context, db Queryer, {{ create
3232
`{{ createSelectByPkSQL .Struct }}`,
3333
{{ createSelectByPkSQLParams .Struct }}).Scan({{ createSelectByPkScan .Struct }})
3434
if err != nil {
35-
return nil, errors.Wrap(err, "failed to select {{ .Struct.Table.Name }}")
35+
return nil, errors.WithStack(err)
3636
}
3737
return &r, nil
3838
}

0 commit comments

Comments
 (0)